/*
* 作 者: 霍雨佳
* 完成日期:2014 年4月15日
* 版 本 号:v1.0
* 问题描述:实现分数类中的运算符重载
* 样例输入:
* 样例输出:
* 项目要求:实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。可以以第5周项目2的代码为基础开始工作。
*/
#include <iostream>
#include <cstdlib>
using namespace std;
class CFraction
{
private:
double nume; // 分子
double deno; // 分母
public:
//构造函数及运算符重载的函数声明
CFraction(double nu=1,double de=1); //构造函数,初始化用
void set(double nu,double de); //置值,改变值时用
void simplify(); //化简(使分子分母没有公因子)
void display();
//二目的比较运算符重载
bool operator > (CFraction &t);
bool operator < (CFraction &t);
bool operator >= (CFraction &t);
bool operator <= (CFraction &t);
bool operator == (CFraction &t);
bool operator != (CFraction &t);
//二目的加减运算符的重载
CFraction operator+(CFraction &t);
CFraction operator-(CFraction &t);//对照+理解
CFraction operator*(CFraction &t);
CFraction operator/(CFraction &t);//对照+理解
};
int gcd(int x,int y);
CFraction::CFraction(double nu,double de) //构造函数,初始化用
{
if(de!=0)
{
nume=nu;
deno=de;
}
else
{
cout<<"初始化有误,退出程序"<<endl;
exit(0);
}
}
void CFraction::set(double nu,double de) //置值,改变值时用
{
if(de!=0)
{
nume=nu;
deno=de;
}
}
void CFraction::display()
{
cout<<"="<<nume<<"/"<<deno<<endl;
}
bool CFraction::operator > (CFraction &t)
{
double m1=nume/deno;
double m2=t.nume/t.deno;
if (m1>m2) return true;
return false;
}
bool CFraction::operator < (CFraction &t)
{
double m1=nume/deno;
double m2=t.nume/t.deno;
if (m1<m2) return true;
return false;
}
bool CFraction::operator == (CFraction &t)
{
if (*this <t||*this>t) return false;
return true;
}
bool CFraction::operator != (CFraction &t)
{
if (*this == t) return false;//可以直接使用已经重载了的运算实现新运算
return true;
}
bool CFraction::operator >= (CFraction &t)
{
if (*this < t) return false;
return true;
}
bool CFraction::operator <= (CFraction &t)
{
if (*this > t) return false;
return true;
}
CFraction CFraction::operator+(CFraction &c)
{
CFraction t;
t.nume=nume*c.deno+c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator-(CFraction &c)//对照+理解
{
CFraction t;
t.nume=nume*c.deno-c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator*(CFraction &c)
{
CFraction t;
t.nume=nume*c.nume;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator/(CFraction &c)//对照+理解
{
CFraction t;
if (!c.nume) return *this; //除法无效
t.nume=nume*c.deno;
t.deno=deno*c.nume;
t.simplify();
return t;
}
void CFraction::simplify() //化简(使分子分母没有公因子)
{
int m=gcd(nume,deno);
nume=nume/m;
deno/=m;
}
int gcd(int x,int y)//求两数最大公约数
{
int r;
while(y>0)
{
r=x%y;
x=y;
y=r;
}
return x;
}
int main()
{
CFraction t1(7,8),t2(5,6),t;
cout<<"t1";
t1.display();
cout<<"t2";
t2.display();
cout<<"比较分数的大小:\n";
if (t1>t2) cout<<"t1>t2"<<endl;
if (t1<t2) cout<<"t1<t2"<<endl;
if (t1==t2) cout<<"t1=t2"<<endl;
if (t1!=t2) cout<<"t1≠t2"<<endl;
if (t1>=t2) cout<<"t1≥t2"<<endl;
if (t1<=t2) cout<<"t1≤t2"<<endl;
cout<<"t1与t2的计算 \n";
t=t1+t2;
cout<<"t1+t2=";
t.display();
t=t1-t2;
cout<<"t=t1-t2=";
t.display();
t=t1*t2;
cout<<"t=t1*t2=";
t.display();
t=t1/t2;
cout<<"t=t1/t2=";
t.display();
return 0;
}
运行结果:
保持激情,不懈努力~~~