/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称 :
*作 者 : 刘云
*完成日期 : 2016年5月25号
*版 本 号 : v6.0
*
*问题描述 : 在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。
*输入描述 : 无
*程序输出 :
*/
//(2)
#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);
//类与数的加、减、乘、除运算
CFraction operator+(int a);
CFraction operator-(int a);
CFraction operator*(int a);
CFraction operator/(int a);
friend CFraction operator+(int a,CFraction &c1);
friend CFraction operator-(int a,CFraction &c1);
friend CFraction operator*(int a,CFraction &c1);
friend CFraction operator/(int a,CFraction &c1);
friend int operator>(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);
}
//类与数的加、减、乘、除运算
CFraction CFraction::operator+(int a)
{
return CFraction(a*deno+nume,deno);
}
CFraction CFraction::operator-(int a)
{
return CFraction(nume-a*deno,deno);
}
CFraction CFraction::operator*(int a)
{
return CFraction(nume*a,deno);
}
CFraction CFraction::operator/(int a)
{
return CFraction(nume,deno*a);
}
//类与数的加、减、乘、除运算,运算符合交换律
CFraction operator+(int a,CFraction &c1)
{
return CFraction(a*c1.deno+c1.nume,c1.deno);
}
CFraction operator-(int a,CFraction &c1)
{
return CFraction(a*c1.deno-c1.nume,c1.deno);
}
CFraction operator*(int a,CFraction &c1)
{
return CFraction(c1.nume*a,c1.deno);
}
CFraction operator/(int a,CFraction &c1)
{
return CFraction(c1.deno*a,c1.nume);
}
int operator>(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
{
if(deno==1)
cout<<nume<<endl;
else
cout<<nume<<'/'<<deno<<endl;
}
int main()
{
CFraction c1(1,2),c2(1,4),c3,a(1,3);
int i=2;
c3=c1+c2;
c3.qualify();
cout<<"(1/2)+(1/4)=";
c3.show();
c3=c1-c2;
c3.qualify();
cout<<"(1/2)-(1/4)=";
c3.show();
c3=c1*c2;
c3.qualify();
cout<<"(1/2)*(1/4)=";
c3.show();
c3=c1/c2;
c3.qualify();
cout<<"(1/2)/(1/4)=";
c3.show();
c3=a+i;
c3.qualify();
cout<<"(1/3)+2=";
c3.show();
c3=i+a;
c3.qualify();
cout<<"2+(1/3)=";
c3.show();
c3=45+a;
c3.qualify();
cout<<"45+(1/3)=";
c3.show();
c3=a*27;
c3.qualify();
cout<<"(1/3)*27=";
c3.show();
c3=5/a;
c3.qualify();
cout<<"5/(1/3)=";
c3.show();
if(c1>c2)cout<<"(1/2)>(1/4)"<<endl;
else cout<<"(1/2)<(1/4)"<<endl;
return 0;
}
运行结果: