#include <iostream>
#include <cmath>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //构造函数,初始化用
void output(); //输出:以8/6为例,style为0时,原样输出8/6;
void simplify();
//分数大小的比较
bool operator > (CFraction &c);
bool operator < (CFraction &c);
bool operator >= (CFraction &c);
bool operator <= (CFraction &c);
bool operator == (CFraction &c);
bool operator != (CFraction &c);
//分数的运算
CFraction operator+(CFraction &c);
CFraction operator-(CFraction &c);
CFraction operator*(CFraction &c);
CFraction operator/(CFraction &c);
//新增:取反取正求倒数
CFraction operator+();
CFraction operator-();
CFraction operator~();
//新增:输入输出流的重载
friend istream &operator>>(istream &,CFraction &);
friend ostream &operator<<(ostream &,CFraction );
};
int main()
{
cout<<"分数c1=8/6"<<endl<<"————————————————"<<endl<<"将c1化简输出:";
CFraction C1(8,6),C2,C;
C1.simplify();
C1.output();
cout<<"————————————————"<<endl<<"输入分数c2(按a/b形式输入):";
cin>>C2;
cout<<"将c2化简后输出:";
C2.simplify();
C2.output();
//进行大小比较与加减乘除
cout<<"————————————————"<<endl;
cout<<"比较c1与c2大小:"<<endl;
if (C1>C2)
cout<<"C1>C2"<<endl;
if (C1<C2)
cout<<"C1<C2"<<endl;
if (C1==C2)
cout<<"C1=C2"<<endl;
if (C1!=C2)
cout<<"C1≠C2"<<endl;
if (C1>=C2)
cout<<"C1≥C2"<<endl;
if (C1<=C2)
cout<<"C1≤C2"<<endl;
cout<<"————————————————"<<endl;
cout<<"进行c1与c2的加减乘除:"<<endl;
cout<<"c1+c2=";
C=C1+C2;
C.simplify();
C.output();
cout<<"将c1与c2的和取正:"<<+C;
cout<<"c1-c2=";
C=C1-C2;
C.simplify();
C.output();
cout<<"将c1与c2的差取反:"<<-C;
cout<<"c1*c2=";
C=C1*C2;
C.simplify();
C.output();
cout<<"将c1与c2的积取倒数:"<<~C;
cout<<"c1/c2=";
C=C1/C2;
C.simplify();
C.output();
return 0;
}
istream &operator>>(istream &in,CFraction &c)
{
char d;
int nu,de;
while (1)//注意while(1)的使用
{
cin>>nu>>d>>de;
if (d=='/'&&de!=0)
{
c.nume=nu;
c.deno=de;
break;
}
else
{
cout<<"格式错误,请重新输入!"<<endl;
}
}
return cin;
}
ostream &operator<<(ostream &out,CFraction c)//!!!!!!!千万不能写CFraction &c!!!!!!
{
cout<<c.nume<<"/"<<c.deno<<endl;
return cout;
}
CFraction CFraction::operator+()
{
return *this;
}
CFraction CFraction::operator-()
{
CFraction c;
c.deno=-deno;
c.nume=-nume;
return c;
}
CFraction CFraction::operator~()
{
CFraction c;
c.deno=nume;
c.nume=deno;
if (c.deno<0)//如果分母为正,分子是负数,需要把分母转化为正数。当分子分母均为负或均为正时,除法运算后都为正,不用再考虑。
{
c.deno=-c.deno;
c.nume=-c.nume;
}
return c;
}
//分数的运算
CFraction CFraction::operator+(CFraction &c)
{
CFraction a;
a.nume=deno*c.nume+nume*c.deno;
a.deno=deno*c.deno;
return a;
}
CFraction CFraction::operator-(CFraction &c)
{
CFraction a;
a.nume=nume*c.deno-deno*c.nume;
a.deno=deno*c.deno;
return a;
}
CFraction CFraction::operator*(CFraction &c)
{
CFraction a;
a.nume=nume*c.nume;
a.deno=deno*c.deno;
return a;
}
CFraction CFraction::operator/(CFraction &c)
{
CFraction a;
a.nume=nume*c.deno;
a.deno=deno*c.nume;
return a;
}
//实现分数大小的比较
bool CFraction::operator > (CFraction &c)
{
if ((nume/deno)>(c.nume/c.deno))return true;
else return false;
}
bool CFraction::operator < (CFraction &c)
{
if ((nume/deno)<(c.nume/c.deno))return true;
else return false;
}
bool CFraction::operator >= (CFraction &c)
{
if (*this<c)return false;
else return true;
}
bool CFraction::operator <= (CFraction &c)
{
if (*this>c)return false;
else return true;
}
bool CFraction::operator == (CFraction &c)
{
if (*this<c||*this>c)return false;
else return true;
}
bool CFraction::operator != (CFraction &c)
{
if (*this==c)return false;
else return true;
}
//实现分数格式的判断
CFraction::CFraction(int nu,int de)
{
while (1)//注意while(1)的使用
{
if (de!=0)
{
nume=nu;
deno=de;
break;
}
else
cout<<"分子不能为0,请重新输入!"<<endl;
}
}
//化简和输出
void CFraction::simplify()
{
int r,x=nume,y=deno;
while (y!=0)
{
r=x%y;
x=y;
y=r;
}
nume/=x;
deno/=x;
return;
}
void CFraction::output()
{
if (deno<0)//如果分母为正,分子是负数,需要把分母转化为正数。当分子分母均为负或均为正时,除法运算后都为正,不用再考虑。
{
deno=-deno;
nume=-nume;
}
cout<<nume<<"/"<<deno<<endl;
return;
}