#include <iostream>
#include <cmath>
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();
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);
friend CFraction operator+( CFraction &c,int i);
friend CFraction operator-( CFraction &c,int i);
friend CFraction operator*( CFraction &c,int i);
friend CFraction operator/( CFraction &c,int i);
};
// 分数化简
void CFraction::simplify() //借鉴的老师的方法;
{
int m,n,r;
m=abs(deno);
n=abs(nume);
while(r=m%n) // 求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.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,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if(t1_nume > t2_nume)
return true;
else
return false;
}
bool CFraction::operator<(const CFraction &c)
{
int t1_nume,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if(t1_nume < t2_nume)
return true;
else
return false;
}
bool CFraction::operator==(const CFraction &c)
{
int t1_nume,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if(t1_nume == t2_nume)
return true;
else
return false;
}
bool CFraction::operator>=(const CFraction &c)
{
int t1_nume,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if(t1_nume >= t2_nume)
return true;
else
return false;
}
bool CFraction::operator<=(const CFraction &c)
{
int t1_nume,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if( t1_nume <= t2_nume)
return true;
else
return false;
}
CFraction operator+( CFraction &c,int i)
{
CFraction c1(c.nume + c.deno * i ,c.deno );
c1.simplify();
return c1;
}
CFraction operator-( CFraction &c,int i)
{
CFraction c1(c.nume - c.deno * i , c.deno);
c1.simplify();
return c1;
}
CFraction operator*( CFraction &c,int i)
{
CFraction c1(c.nume * i,c.deno);
c1.simplify();
return c1;
}
CFraction operator/( CFraction &c,int i)
{
CFraction c1(c.nume ,c.deno * i);
c1.simplify();
return c1;
}
int main()
{
CFraction c1(3,5),c2(4,8),t,c3;
t = c1 + c2;
cout<<"c1 + c2 = ";
t.display();
t = c1 - c2;
cout<<"c1 - c2 = ";
t.display();
t = c1 * c2;
cout<<"c1 * c2 = ";
t.display();
t = c1 / c2;
cout<<"c1 / c2 = ";
t.display();
t = c1;
cout<<"t = ";
t.display();
t = -c1;
cout<<"-t = ";
t.display();
c1.display();
if(c1 > c2) cout<< " > 并且 ";
if(c1 < c2) cout<<" < 并且";
if(c1 == c2) cout<<" == ";
if(c1 >= c2) cout<<" >= ";
if(c1 <= c2) cout<<" <= ";
c2.display();
c3 = c1 + 3;
cout<<"c1+3= ";
c3.display();
c3 = c1 - 3;
cout<<"c1-3= ";
c3.display();
c3 = c1 * 3;
cout<<"c1*3= ";
c3.display();
c3 = c1 / 3;
cout<<"c1/3= ";
c3.display();
system("pause");
return 0;
}
第八周任务4
最新推荐文章于 2024-01-18 15:20:35 发布