Oct 30, 2010 at 6:21am UTC
I want to do some fraction operation,my code has no error but can't run.
who can help me correct it,thank you very much.
#includte<iostream>
using namespace std;
class fraction
{
int fmu,fzi;
public:
fraction(){}
fraction(int fz,int fm){fmu=fm;fzi=fz;}
void get(int &z,int &m);
void tongfen(int &z1,int &m1,int &z2,int &m2);
fraction yuefen(int &fz,int &fm);
fraction daoshu(int &fz,int &fm);
fraction operator+(const fraction&);
fraction operator-(const fraction&);
fraction operator*(const fraction&);
fraction operator/(const fraction&);
friend int operator ==(const fraction&,const fraction&);
friend int operator !=(const fraction&,const fraction&);
friend int operator >=(const fraction&,const fraction&);
friend int operator <=(const fraction&,const fraction&);
void print();
};
void fraction::tongfen(int &z1,int &m1,int &z2,int &m2)
{
int fm,fz;
fm=m1*m2;
fz=z1*z2;
fraction::yuefen(fz,fm);/////
}
fraction fraction::yuefen(int &fz,int &fm)
{
int a=fm,b=fz;
int r=b<a?b:a;
while(a%b!=0)
{
a=b;
b=a%b;
}
r=b;
b=fz/r;
a=fm/r;
fraction yue(b,a);
return yue;
////////////////////
}
fraction fraction::daoshu(int &fz,int &fm)
{
return fraction(fm,fz);
}
fraction fraction::operator*(const fraction& c)
{
return fraction(c.fzi*fzi,c.fmu*fmu);
}
fraction fraction::operator /(const fraction& c)
{
return fraction(c.fzi*fmu,c.fmu*fzi);
}
fraction fraction::operator +(const fraction& c)
{
int z=(c.fzi*fmu+fzi*c.fmu);
int m=(c.fmu*fmu);
fraction::yuefen(z,m);
fraction x(z,m);
return x;/////////////////
}
fraction fraction::operator -(const fraction& c)
{
int z=(c.fzi*fmu-fzi*c.fmu);
int m=(c.fmu*fmu);
fraction::yuefen(z,m);
fraction x(z,m);
return x;
}
void fraction::print()
{
cout<<fzi<<"/"<<fmu<<endl;
}
class integer:public fraction
{
int a,b;
public:
integer(){}
int digit_number();
};
void main()
{
fraction f1(1,2),f2(3,4);
fraction fa,fb,fc,fd;
fa=f1+f2;fa.print();
fb=f1-f2;fb.print();
fc=f1*f2;
fd=f1/f2;
}
Oct 31, 2010 at 2:28am UTC
thank you very much,I got it..