#include<iostream>
using namespace std;
class CFraction
{
private:
int nume;
int deno;
public:
CFraction(int a=0,int b=0);
CFraction operator+(const CFraction &a);
CFraction operator-(const CFraction &a);
CFraction operator*(const CFraction &a);
CFraction operator/(const CFraction &a);
friend CFraction operator+(const CFraction &b,int a);
friend CFraction operator-(const CFraction &b,int a);
friend CFraction operator*(const CFraction &b,int a);
friend CFraction operator/(const CFraction &b,int a);
friend CFraction operator+(int a,const CFraction &b);
friend CFraction operator-(int a,const CFraction &b);
friend CFraction operator*(int a,const CFraction &b);
friend CFraction operator/(int a,const CFraction &b);
friend int multiple(const CFraction &a,const CFraction &b);
void sort_CFraction();
void show_CFraction();
};
CFraction::CFraction(int a,int b) //
{
nume=a;
deno=b;
}
CFraction CFraction::operator+(const CFraction &a)
{
CFraction b;
int c;
c=multiple(*this,a);
b.nume=nume*(c/deno)+a.nume*(c/a.deno);
b.deno=c;
b.sort_CFraction();
return b;
}
CFraction CFraction::operator-(const CFraction &a)
{
CFraction b;
int c=multiple(*this,a);
b.nume=nume*(c/deno)-a.nume*(c/a.deno);
b.deno=c;
b.sort_CFraction();
return b;
}
CFraction CFraction::operator*(const CFraction &a)
{
CFraction b;
b.nume=nume*a.nume;
b.deno=deno*a.deno;
b.sort_CFraction();
return b;
}
CFraction CFraction::operator/(const CFraction &a)
{
CFraction b;
b.nume=nume*a.deno;
b.deno=deno*a.nume;
b.sort_CFraction();
return b;
}
CFraction operator+(const CFraction &b,int a)
{
CFraction c;
c.nume=b.nume+a*b.deno;
c.deno=b.deno;
c.sort_CFraction();
return c;
}
CFraction operator-(const CFraction &b,int a)
{
CFraction c;
c.nume=b.nume-a*b.deno;
c.deno=b.deno;
c.sort_CFraction();
return c;
}
CFraction operator*(const CFraction &b,int a)
{
CFraction c;
c.nume=b.nume*a;
c.deno=b.deno;
c.sort_CFraction();
return c;
}
CFraction operator/(const CFraction &b,int a)
{
CFraction c;
c.nume=b.nume;
c.deno=b.deno*a;
c.sort_CFraction();
return c;
}
CFraction operator+(int a,const CFraction &b)
{
CFraction c;
c.nume=b.nume+a*b.deno;
c.deno=b.deno;
c.sort_CFraction();
return c;
}
CFraction operator-(int a,const CFraction &b)
{
CFraction c;
c.nume=a*b.deno-b.nume;
c.deno=b.deno;
c.sort_CFraction();
return c;
}
CFraction operator*(int a,const CFraction &b)
{
CFraction c;
c.nume=b.nume*a;
c.deno=b.deno;
c.sort_CFraction();
return c;
}
CFraction operator/(int a,const CFraction &b)
{
CFraction c;
c.nume=b.deno*a;
c.deno=b.nume;
c.sort_CFraction();
return c;
}
int multiple(const CFraction &a ,const CFraction &b)
{
int c,d,temp,e;
c=a.deno;
d=b.deno;
while(d!=0)
{
temp=c%d;
c=d;
d=temp;
}
e=a.deno*b.deno/c;
return e;
}
void CFraction::sort_CFraction()
{
int a,b,temp;
a=nume;
b=deno;
while(b!=0)
{
temp=a%b;
a=b;
b=temp;
}
nume/=a;
deno/=a;
if(nume>0&&deno<0)
{
nume=-nume;
deno=-deno;
}
}
void CFraction::show_CFraction()
{
CFraction::sort_CFraction();
if(deno==1||nume==0)
cout<<nume<<endl;
else
cout<<nume<<'/'<<deno<<endl;
}
int main()
{
CFraction a(-3,-4),b(4,5),d;
int c=6;
a.show_CFraction();
d=a+b;
d.show_CFraction();
d=a-b;
d.show_CFraction();
d=a*b;
d.show_CFraction();
d=a/b;
d.show_CFraction();
d=a+c;
d.show_CFraction();
d=a-c;
d.show_CFraction();
d=a*c;
d.show_CFraction();
d=a/c;
d.show_CFraction();
d=c+a;
d.show_CFraction();
d=c-a;
d.show_CFraction();
d=c*a;
d.show_CFraction();
d=c/a;
d.show_CFraction();
return 0;
}
<img src="https://img-blog.csdn.net/20150509112051709?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2xpdXpoaWxpbw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />