/*copyright(c)2016.烟台大学计算机学院
* All rights reserved,
* 文件名称:text.Cpp
* 作者:舒文超
* 完成日期:2016年5月22日
* 版本号:vc++6.0
* 问题描述:定义分数的一目运算+和-,分别代
表分数取正和求反,将“按位取反
运算符”~重载为分数的求倒数运算。
*/
#include<iostream>
#include<cmath>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
//构造函数及运算符重载的函数声明
CFraction(int nu=1,int de=1):nume(nu),deno(de) {}
void simplify();
void display();
CFraction operator+(const CFraction &c); //两个分数相加,结果要化简
CFraction operator-(const CFraction &c); //两个分数相减,结果要化简
CFraction operator*(const CFraction &c); //两个分数相乘,结果要化简
CFraction operator/(const CFraction &c); //两个分数相除,结果要化简
CFraction operator+(int a); //分数加整数,结果要化简
CFraction operator-(int a); //分数减整数,结果要化简
CFraction operator*(int a); //分数乘整数,结果要化简
CFraction operator/(int a); //分数除整数,结果要化简
CFraction operator+(); //取正一目运算
CFraction operator-(); //取反一目运算
CFraction operator~(); //取倒数一目运算
bool operator>(const CFraction &c);
bool operator<(const CFraction &c);
bool operator==(const CFraction &c);
bool operator!=(const CFraction &c);
bool operator>=(const CFraction &c);
bool operator<=(const CFraction &c);
};
void CFraction::simplify()
{
int m,n,r;
n=fabs(deno);
m=fabs(nume);
while(r=m%n)
{
m=n;
n=r;
}
deno/=n;
nume/=n;
if (deno<0)
{
deno=-deno;
nume=-nume;
}
}
void CFraction::display()
{
cout<<nume<<"/"<<deno<<endl;
}
CFraction CFraction::operator+(const CFraction &c)
{
CFraction t;
t.nume=nume*c.deno+c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator-(const CFraction &c) //两个分数相减,结果要化简
{
CFraction c1;
c1.nume=nume*c.deno-c.nume*deno;
c1.deno=deno*c.deno;
c1.simplify();
return c1;
}
CFraction CFraction::operator*(const CFraction &c) //两个分数相乘,结果要化简
{
CFraction c1;
c1.nume=nume*c.nume;
c1.deno=deno*c.deno;
c1.simplify();
return c1;
}
CFraction CFraction::operator/(const CFraction &c)
{
CFraction c1;
c1.nume=nume*c.deno;
c1.deno=deno*c.nume;
c1.simplify();
return c1;
}
bool CFraction::operator>(const CFraction &c)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno;
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if ((this_nume-c_nume)*common_deno>0) return true;
return false;
}
bool CFraction::operator<(const CFraction &c)
{
int nume1,nume2,deno1;
nume1=nume*c.deno;
nume2=c.nume*deno;
deno1=deno*c.deno;
if ((nume1-nume2)*deno1<0)
return true;
return false;
}
bool CFraction::operator==(const CFraction &c)
{
if (*this!=c)
return false;
return true;
}
bool CFraction::operator!=(const CFraction &c)
{
if (*this!=c)
return true;
return false;
}
bool CFraction::operator>=(const CFraction &c)
{
if (*this<c)
return false;
return true;
}
bool CFraction::operator<=(const CFraction &c)
{
if (*this>c)
return false;
return true;
}
CFraction CFraction::operator+(int a)
{
CFraction C;
int c;
c=a*deno;
C.deno=deno;
C.nume=nume+c;
C.simplify();
return C;
}
CFraction CFraction::operator-(int a)
{
CFraction C;
int c;
c=a*deno;
C.deno=deno;
C.nume=nume-c;
C.simplify();
return C;
}
CFraction CFraction::operator*(int a)
{
CFraction C;
C.deno=deno;
C.nume=nume*a;
C.simplify();
return C;
}
CFraction CFraction::operator/(int a)
{
CFraction C;
C.deno=deno*a;
C.nume=nume;
C.simplify();
return C;
}
CFraction CFraction::operator+() //取正一目运算
{
return *this;
}
CFraction CFraction::operator-() //取反一目运算
{
CFraction C;
C.nume=-nume;
C.deno=deno;
return C;
}
CFraction CFraction::operator~()
{
CFraction C;
C.nume=deno;
C.deno=nume; //未对原分子为0的情况进行处理
if(C.deno<0) //保证负分数的负号在分子上
{
C.deno=-C.deno;
C.nume=-C.nume;
}
return C;
}
//重载函数的实现及用于测试的main()函数
int main()
{
char c;
int a,b,d;
cout<<"输入第一个分数(样例:x/y): ";
cin>>a>>c>>b;
CFraction x(a,b);
cout<<"输入第二个分数(样例:x/y): ";
cin>>a>>c>>b;
CFraction y(a,b);
CFraction s;
cout<<"第一个分数为:"<<endl;
x.display();
cout<<"第二个分数为:"<<endl;
y.display();
cout<<"请输入要进行运算的整数d"<<endl;
cin>>d;
s=x+d;
cout<<"x+d=";
s.display();
cout<<endl;
s=x-d;
cout<<"x-d=";
s.display();
cout<<endl;
s=x*d;
cout<<"x*d=";
s.display();
cout<<endl;
s=x/d;
cout<<"x/d=";
s.display();
cout<<endl;
s=x+y;
cout<<"x+y=";
s.display();
cout<<endl;
s=x-y;
cout<<"x-y=";
s.display();
cout<<endl;
s=x*y;
cout<<"x*y=";
s.display();
cout<<endl;
s=x/y;
cout<<"x/y=";
s.display();
cout<<endl;
if(x>y)
cout<<"大于"<<endl;
if(x<y)
cout<<"小于"<<endl;
if(x==y)
cout<<"等于"<<endl;
cout<<"-x=";
s=-x;
s.display();
cout<<endl;
cout<<"+x=";
s=+x;
cout<<endl;
cout<<"x的倒数: ";
s=~x;
cout<<endl;
return 0;
}