GRP2

NAME 2: Bay We Heng 3I302
using namespace std; int GCD(int x, int y){ if(y==0) return x; return GCD(y,x%y); } class Rational{ public: int num, den; //Attributes should be in protected
 * 1) include
 * 2) include

Rational{num=0; den=1;}; Rational(int n, int d){num = n; den = d;}; ~Rational{};

void Display{ cout << num << "/" << den << endl; };

void Set_num(int n){num = n; }; void Set_den(int d){den = d; };

Rational ADD(Rational x){ Rational y; y.num = num*x.den+den*x.num; y.den = den*x.den; y.REDUCE; return y; } Rational MINUS(Rational x){ Rational y; y.num = num*x.den-den*x.num; y.den = den*x.den; y.REDUCE; return y; } Rational MULTIPLY(Rational x){ Rational y; y.num = num*x.num; y.den = den*x.den; y.REDUCE; return y; } Rational DIVIDE(Rational x){ Rational y; y.num = num*x.den; y.den = den*x.num; y.REDUCE; return y; }

void REDUCE{ int gcd = GCD(max(num,den),min(num,den)); num/=gcd; den/=gcd; if(num&&den<0 || num>0&&den<0){ num=-num; den=-den; } else if(num==0) den=1; } }; int main{ return 0; }