#include <iostream.h>
int cishu=1;
class Fraction
{public:
Fraction(){a=0;b=0;}
Fraction(int x,int y){a=x;b=y;}
Fraction operator - (Fraction &k);
Fraction operator + (Fraction &k);
Fraction operator * (Fraction &k);
Fraction operator / (Fraction &k);
friend istream & operator >>(istream &,Fraction&);
friend bool operator == (Fraction &k1,int no);
void output();
private:
int a;
int b;
};
istream & operator >>(istream &in,Fraction&k)
{
in>>k.a>>k.b;
return in;
}
bool operator == (Fraction &k1,int no)
{
if(k1.a==no&&k1.b==no)
return true;
else
return false;
}
Fraction Fraction::operator - (Fraction &k)
{
return Fraction(a*k.b-k.a*b,b*k.b);
}
Fraction Fraction::operator + (Fraction &k)
{
return Fraction(a*k.b+k.a*b,b*k.b);
}
Fraction Fraction::operator * (Fraction &k)
{
return Fraction(a*k.a,b*k.b);
}
Fraction Fraction::operator / (Fraction &k)
{
return Fraction(a*k.b,b*k.a);
}
void Fraction::output()
{
int t,r,u,v;
u=b;
v=a;
if(v>u){t=u;u=v;v=t;}
while((r=u%v)!=0)
{
u=v;
v=r;
}
a/=v;
b/=v;
if(b<0)
{
a=0-a;
b=0-b;
}
if(b==-1)
cout<<0-a;
else if(b==1)
cout<<a;
else
cout<<a<<"/"<<b;
if(cishu%2!=0)
cout<<" ";
cishu+=1;
}
int main()
{
Fraction f1,f2,f3;
while(cin>>f1>>f2)
{
if(f1==0&&f2==0)
break;
f3=f1-f2;
f3.output();
f3=f1/f2;
f3.output();
cout<<endl;
}
return 0;
}
方法二:
#include <iostream.h>
#include<cmath>
class Fraction
{
public:
int flag;
Fraction(int nu=0,int de=1); //构造函数,初始化用
void set(int nu=0,int de=1); //置值,改变值时用
void simplify(); //化简(使分子分母没有公因子)
void output();
Fraction operator + (const Fraction &c2);
Fraction operator - (const Fraction &c2);
Fraction operator * (const Fraction &c2);
Fraction operator / (const Fraction &c2);
bool operator == (int x);
friend istream& operator>>(istream& input,Fraction &f);
private:
int nume; // 分子
int deno; //分母
};
int gys(int a,int b) //求最大公约数
{
return (a%b!=0?(gys(b,a%b)):b);
}
int gbs(int u,int v) //最小公倍数
{
int h;
h=gys(u,v);
return (u*v/h);
}
Fraction ::Fraction(int nu,int de)
{
if(de!=0)
{
nume=nu;
deno=de;
}
}
void Fraction ::set(int nu,int de)
{
nume=nu;
deno=de;
}
void Fraction:: output()
{
int b=1;
simplify();
if(deno!=1)
{
if(nume<0||deno<0)
cout<<"-"<<fabs(nume)<<"/"<<fabs(deno);
else
cout<<nume<<"/"<<deno;
}
else
cout<<nume;
if(b%2!=0)
cout<<" ";
b+=1;
}
Fraction Fraction::operator + (const Fraction &c2)
{
Fraction c;
int r;
if (deno!=c.deno) //取分母的最大公倍数
{
r=gbs(deno,c2.deno);
c.nume=(nume*r/deno)+(c2.nume*r/c2.deno);
c.deno=r;
}
else
{
c.nume=nume+c2.nume;
c.deno=deno;
}
return c;
}
Fraction Fraction::operator - (const Fraction &c2)
{
Fraction c;
int r;
if (deno!=c.deno) //取分母的最大公倍数
{
r=gbs(deno,c2.deno);
c.nume=(nume*r/deno)-(c2.nume*r/c2.deno);
c.deno=r;
}
else
{
c.nume=nume-c2.nume;
c.deno=deno;
}
return c;
}
Fraction Fraction::operator * (const Fraction &c2)
{
Fraction c;
c.nume=nume*c2.nume;
c.deno=deno*c2.deno;
return c;
}
Fraction Fraction::operator / (const Fraction &c2)
{
Fraction c;
c.nume=nume*c2.deno;
c.deno=deno*c2.nume;
return c;
}
bool Fraction::operator == (int x)
{
return !((nume>x)||(nume<x));
}
void Fraction::simplify()//化简(使分子分母没有公因子)
{
int r;
r=gys(nume,deno);
nume/=r;
deno/=r;
}
istream& operator >>(istream& input,Fraction &f)
{
input>>f.nume>>f.deno;
return input;
}
int main()
{
Fraction f1,f2,f3;
while(cin>>f1>>f2) {
if(f1==0&&f2==0)
break;
f3=f1-f2;
f3.output();
f3=f1/f2;
f3.output();
cout<<endl;
}
return 0;
}