#include <iostream>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(){nume=0;deno=0;}
CFraction(int n,int d) :nume(n),deno(d){}
CFraction operator +(CFraction &x);
CFraction operator -(CFraction &x);
CFraction operator *(CFraction &x);
CFraction operator /(CFraction &x);
CFraction operator +(int x);
CFraction operator -(int x);
CFraction operator *(int x);
CFraction operator /(int x);
bool operator >(CFraction &x);
bool operator <(CFraction &x); //构造函数及运算符重载的函数声明
bool operator =(CFraction &x);
bool operator >=(CFraction &x);
bool operator <=(CFraction &x);
bool operator !=(CFraction &x);//死记住开头的形式,
//CFraction operator-();/
int f(int,int);
int g(int,int);
void display();
};
CFraction CFraction::operator +(CFraction &x)
{
CFraction c;
int t=f(deno,x.deno);
c.nume=nume*(t/deno)+(t/x.deno)*x.nume;
c.deno=t;
t=g(c.nume,c.deno);
c.nume=c.nume/t;
c.deno=c.deno/t;
return c;
}
CFraction CFraction::operator -(CFraction &x)
{
CFraction c;
int t=f(deno,x.deno);
c.nume=nume*(t/deno)-(t/x.deno)*x.nume;
c.deno=t;
t=g(c.nume,c.deno);
c.nume=c.nume/t;
c.deno=c.deno/t;
return c;
}
CFraction CFraction::operator *(CFraction &x)
{
CFraction c;
int t;
c.nume=nume*x.nume;
c.deno=deno*x.deno;
t=g(c.nume,c.deno);
c.nume=c.nume/t;
c.deno=c.deno/t;
return c;
}
CFraction CFraction::operator /(CFraction &x)
{
CFraction c;
int t;
c.nume=nume*x.deno;
c.deno=deno*x.nume;
t=g(c.nume,c.deno);
c.nume=c.nume/t;
c.deno=c.deno/t;
return c;
}
bool CFraction::operator >(CFraction &x)
{
//bool flag=true;
CFraction c;
int t=f(deno,x.deno);
c.nume=nume*(t/deno)-(t/x.deno)*x.nume;
if(c.nume>0)
{
return true;
}
else
{
//flag=false;
return false;
}
}
bool CFraction::operator <(CFraction &x)
{
//bool flag=true;
int t=f(deno,x.deno);
CFraction c;
c.nume=nume*(t/deno)-(t/x.deno)*x.nume;
if(c.nume<0)
{
return true;
}
else
{
//flag=false;
return false;
}
}
bool CFraction::operator =(CFraction &x)
{
//bool flag=true;
int t=f(deno,x.deno);
CFraction c;
c.nume=nume*(t/deno)-(t/x.deno)*x.nume;
if(c.nume=0)
{
return true;
}
else
{
//flag=false;
return false;
}
}
bool CFraction::operator >=(CFraction &x)
{
//bool flag=false;
int t=f(deno,x.deno);
CFraction c;
c.nume=nume*(t/deno)-(t/x.deno)*x.nume;
if(c.nume<0)
{
return false;
}
else
{
//flag=true;
return true;
}
}
bool CFraction::operator <=(CFraction &x)
{
//bool flag=false;
int t=f(deno,x.deno);
CFraction c;
c.nume=nume*(t/deno)-(t/x.deno)*x.nume;
if(c.nume>0)
{
return false;
}
else
{
//flag=true;
return true;
}
}
bool CFraction::operator !=(CFraction &x)//注意开头的形式
{
//bool flag=false;
int t=f(deno,x.deno);
CFraction c;
c.nume=nume*(t/deno)-(t/x.deno)*x.nume;
if(c.nume==0)
{
return false;
}
else
{
//flag=true;
return true;
}
}
/*CFraction CFraction:: operator -()
{
//*this=this*(-1);
return(0- *this);
}*/
int CFraction::f(int x,int y)
{
int t,p,r;
if(x<y)
{
t=x;
x=y;
y=t;
}
p=x*y;
while(y!=0)
{
r=x%y;
x=y;
y=r;
}
return p/x;
}
int CFraction:: g(int x,int y)
{
int t,p,r;
if(x<y)
{
t=x;
x=y;
y=t;
}
p=x*y;
while(y!=0)
{
r=x%y;
x=y;
y=r;
}
return x;
}
void CFraction:: display()
{
cout<<nume<<'/'<<deno;
}
CFraction CFraction::operator +(int x)
{
CFraction c;
c.nume=nume+deno*x;
c.deno=deno;
int t=g(c.nume,c.deno);
c.nume=c.nume/t;
c.deno=c.deno/t;
return c;
}
CFraction CFraction:: operator -(int x)
{
CFraction c;
c.nume=nume-deno*x;
c.deno=deno;
int t=g(c.nume,c.deno);
c.nume=c.nume/t;
c.deno=c.deno/t;
return c;
}
CFraction CFraction:: operator *(int x)
{
CFraction c;
c.nume=nume*x;
c.deno=deno;
int t=g(c.nume,c.deno);
c.nume=c.nume/t;
c.deno=c.deno/t;
return c;
}
CFraction CFraction::operator /(int x)
{
CFraction c;
c.nume=nume;
c.deno=deno*x;
int t=g(c.nume,c.deno);
c.nume=c.nume/t;
c.deno=c.deno/t;
return c;
}
void main()
{
CFraction x1(3,12),x2(1,3);
int b=2;
//double c=b+x1;
//cout<<c<<endl;
cout<<"x1=";
x1.display();
cout<<endl;
cout<<"x2=";
x2.display();
cout<<endl;
(x1+b).display();
cout<<endl;
(x1-b).display();
cout<<endl;
(x1*b).display();
cout<<endl;
(x1/b).display();
cout<<endl;
cout<<"x1+x2=:";
(x1+x2).display();
cout<<endl;
cout<<"x1-x2=:";
(x1-x2).display();
cout<<endl;
cout<<"x1*x2=:";
(x1*x2).display();
cout<<endl;
cout<<"x1/x2=:";
(x1/x2).display();
cout<<endl;
if(x1<x2) cout<<"x1<x2";
cout<<endl;
if(x1!=x2) cout<<"x1!=x2";
}
感悟:整数和分数的相加其实很简答,只不过改了改参数而已。