Han+Cheng

code format="cpp-qt" class Rational { protected: int num, den;

int LCM(int den1, int den2) { int den3, min;

den3 = den1*den2; if (den1=2; i--) { if (den1%i == 0 && den2%i == 0) { den3 = den3/i; }            }

return den3; }

Rational Simplify(Rational r) { int sign1 = 1, sign2 = 1; int min;

if (r.num<0) { sign1 = -1; }           if (r.den<0) { sign2 = -1; }

if (r.num*sign1=2; i--) { if ((r.num*sign1)%i == 0 && (r.den*sign2)%i == 0) { r.num /= i;                           r.den /= i;                 } }            r.setNum(r.num*sign2); r.setDen(r.den*sign2);

return r;       }

public: //constructor Rational(int n=0, int d=1) { num=n; den=d; }

//destructor ~Rational {}

void Display { if (den == 1) { cout << num << endl; } else { cout << num << "/" << den << endl; }       }

//set void setNum(int n) { num = n;       }

void setDen(int n) { den = n;       }

//operators Rational Add(Rational r) { Rational ini(num,den); int temp1 = den, temp2 = r.den; int temp3 = num, temp4 = r.num; //add same denominator(easiest) if (temp1 == temp2) { ini.setNum(temp3+temp4); ini = ini.Simplify(ini); } else { // find lcm of denominator int lcm; lcm = ini.LCM(temp1,temp2); ini.setDen(lcm); ini.setNum((temp3*(lcm/temp1))+(temp4*(lcm/temp2))); ini = ini.Simplify(ini); }            return ini; }

Rational Minus(Rational r) { Rational ini(num,den); //minus same denominator(easiest) if (den == r.den) { ini.setNum(num-r.num); ini = ini.Simplify(ini); } else { // find lcm of denominator int lcm; lcm = ini.LCM(den,r.den); ini.setDen(lcm); ini.setNum((num*(lcm/den))-(r.num*(lcm/r.den))); ini = ini.Simplify(ini); }            return ini; }

Rational Multiply(Rational r) { Rational ini(num,den); ini.setNum(num*r.num); ini.setDen(den*r.den); ini = ini.Simplify(ini); return ini; }

Rational Divide(Rational r) { Rational ini(num,den); //check your implementation, you will always end up with 1! Careless r.setNum(r.den); r.setDen(r.num); ini = ini.Multiply(r); return ini; } }; code