YaoChong

Type in the content of your page here.

code format="cpp" class Rational { protected: int num, den; Rational simplify(int a, int b)   { if (abs(a-b) != 1) {               int first=a, second=b, temp; while (second != 0)                    // Euclid's algorithm {                                      // To find the HCF of                      temp = second;                    // two numbers second = first%second;           // subtract one from the other first = temp;                    // until the remainder is smaller than the subtracted number }               a=a/first; b=b/first; }            Rational ret(a,b); return ret; }; public: Rational {       num=0; den=1; };   Rational(int n, int d)    { num=n; den=d; };   ~Rational {}; void Display {       cout << num << "/" << den << endl; };   void Set(int newn, int newd) {       num=newn; den=newd; };   void SetFract(Rational fr) {        num=fr.num; den=fr.den; };   Rational add(Rational a)    { int ansden, ansnum; ansden=den*a.den; ansnum=num*a.den + den*a.num; return simplify(ansnum, ansden); };   Rational subt(Rational a)    { int ansden, ansnum; ansden=den*a.den; ansnum=num*a.den - den*a.num; return simplify(ansnum, ansden); };   Rational mult(Rational a)    { int finalnum, finalden; finalnum=num*a.num; finalden=den*a.den; return simplify(finalnum, finalden); };   Rational div(Rational a)    { int temp; temp = a.num; a.num = a.den; a.den = temp; Rational b(num,den); return b.mult(a); }; }; code