C++ operator overloading problem

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;
}
1
2
3
4
while(a%b!=0){
   a=b;
   b=a%b;
}

at line 3, a = b, so line 3 is b = b % b, which makes b = 0. So when your loop goes back to line 1, you are dividing by 0. What is this code supposed to do? Find the greatest common divisor using Euclid's algorithm? What you need here is a temporary variable.
1
2
3
4
5
while(a%b!=0){
   int tmp = b;
   b=a%b;
   a = tmp;
}
should work. I think..
thank you very much,I got it..
Topic archived. No new replies allowed.