#include <iostream>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1):nume(nu),deno(de){}
void simplify();
void display();
friend ostream& operator<<(ostream &, CFraction &);
friend istream& operator>>(istream &, CFraction &);
CFraction operator+(const CFraction &c);
CFraction operator-(const CFraction &c);
CFraction operator*(const CFraction &c);
CFraction operator/(const CFraction &c);
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);
};
// 分数化简
void CFraction::simplify()
{
for ( int i = nume; i > 0; i-- )
{
if ( nume % i == 0)
if ( deno % i == 0 )
{
nume /= i;
deno /= i;
break;
}
}
}
ostream& operator<<(ostream &output, CFraction &t)
{
output << t.nume << "/" << t.deno << endl;
return output;
}
istream& operator>>(istream &input, CFraction &t)
{
char a,b,c;
input >> a >> t.nume >> b >> t.deno >> c;
while(1)
{
if ( a == '(' && b== ',' && c == ')' )
break;
else
cout << " 输入错误,请重新输入" << endl;
}
return input;
}
CFraction CFraction::operator+(const CFraction &c)
{
CFraction t;
t.deno = c.deno * deno;
t.nume = deno * c.nume + c.deno * nume;
t.simplify();
return t;
}
CFraction CFraction::operator-(const CFraction &c)
{
CFraction t;
t.deno = c.deno * deno;
t.nume = c.deno * nume - deno * c.nume;
t.simplify();
return t;
}
CFraction CFraction::operator*(const CFraction &c)
{
CFraction t;
t.deno = c.deno * deno;
t.nume = nume * c.nume;
t.simplify();
return t;
}
CFraction CFraction::operator/(const CFraction &c)
{
CFraction t;
t.deno = c.nume * deno;
t.nume = nume * c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator+()
{
CFraction t;
t.deno = deno;
t.nume = nume;
t.simplify();
return t;
}
CFraction CFraction::operator-()
{
CFraction t;
t.deno = deno;
t.nume = -nume;
t.simplify();
return t;
}
bool CFraction::operator>(const CFraction &c)
{
int t1_nume,t2_nume;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if(t1_nume < t2_nume)
return true;
else
return false;
}
bool CFraction::operator<(const CFraction &c)
{
int t1_nume ,t2_nume ;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if(t1_nume > t2_nume)
return true;
else
return false;
}
bool CFraction::operator==(const CFraction &c)
{
int t1_nume ,t2_nume ;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if(t1_nume == t2_nume)
return true;
else
return false;
}
bool CFraction::operator>=(const CFraction &c)
{
int t1_nume ,t2_nume ;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if(t1_nume < t2_nume )
return true;
else
return false;
}
bool CFraction::operator<=(const CFraction &c)
{
int t1_nume ,t2_nume ;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if( t1_nume >= t2_nume)
return true;
else
return false;
}
int main()
{
CFraction c1(1,2),c2,t;
cout <<"please input c2 (X,X)"<< endl;
cin >> c2;
t = c1 + c2;
cout<<"c1 + c2 = "<<t;
t = c1 - c2;
cout<<"c1 - c2 = "<<t;
t = c1 * c2;
cout<<"c1 * c2 = "<<t;
t = c1 / c2;
cout<<"c1 / c2 = "<<t;
cout << "取反" << endl;
t = c1;
cout<<"t = "<<t;
t = -c1;
cout<<"-t = "<<t;
cout << "比较大小" << 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;
system("pause");
return 0;
}