/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称 :
*作 者 : 徐聪
*完成日期 : 2016年5月26号
*版 本 号 : v6.0
*
*问题描述 :
*/
#include<iostream>
using namespace std;
class CFraction
{
private:
int nume; //分子
int deno; //分母
public:
CFraction(int n=1,int d=1):nume(n),deno(d){}
CFraction operator+(CFraction &c1);
CFraction operator-(CFraction &c1);
CFraction operator*(CFraction &c1);
CFraction operator/(CFraction &c1);
friend int compare(CFraction &c1,CFraction &c2);
void qualify();
void show()const;
};
CFraction CFraction::operator+(CFraction &c1)
{
return CFraction(c1.deno*nume+c1.nume*deno,deno*c1.deno);
}
CFraction CFraction::operator-(CFraction &c1)
{
return CFraction(c1.deno*nume-c1.nume*deno,deno*c1.deno);
}
CFraction CFraction::operator*(CFraction &c1)
{
return CFraction(nume*c1.nume,deno*c1.deno);
}
CFraction CFraction::operator/(CFraction &c1)
{
return CFraction(nume*c1.deno,deno*c1.nume);
}
int compare(CFraction &c1,CFraction &c2)
{
if(c1.deno!=c2.deno)
{
if(c1.deno>c2.deno)
return 0;
else
return 1;
}
if(c1.deno==c2.deno)
{
if(c1.nume>c2.nume)
return 1;
else
return 0;
}
}
void CFraction::qualify()
{
int k=1,n=nume,d=deno;
while(deno!=0)
{
k=nume%deno;
nume=deno;
deno=k;
}
n=n/nume;
d=d/nume;
nume=n;
deno=d;
}
void CFraction::show()const
{
cout<<nume<<'/'<<deno<<endl;
}
int main()
{
CFraction c1(1,2),c2(3,4),c3;
c3=c1+c2;
c3.qualify();
c3.show();
c3=c1-c2;
c3.qualify();
c3.show();
c3=c1*c2;
c3.qualify();
c3.show();
c3=c1/c2;
c3.qualify();
c3.show();
return 0;
}
运行结果