GRP3

NAME 2: Tan Yu Zhi
code /* Rational Class OO Programming Done by: Wong Jun Ming 3S2 (29) Tan Yu Zhi 3S2 (27)

using namespace std;
 * 1) include

class Rational { protected: int num, den; Rational REDUCE(Rational Temp){ int small, maxfactor; if (Temp.num<Temp.den) small=Temp.num; else small=Temp.den; for (int i=1; i<=small; i++){if (Temp.num%i==0 && Temp.den%i==0) maxfactor=i;} num/=maxfactor; den/=maxfactor; }	public: Rational{num=0; den=1;}; // Default Constructor Rational(int n, int d){num = n; den = d;}; // Constructor ~Rational{}; // Destructor

// Functions void Display{ if (den==0) cout<<0; else cout<<num; if (num!=den) cout<<"/"<<den; cout<<endl; };		void Set_num(int n){num = n;}; void Set_den(int d){den = d;}; Rational ADD(Rational Temp){ Rational Z(num*Temp.den+Temp.num*den,den*Temp.den); Z.REDUCE(Z); return Z;		}; Rational MINUS(Rational Temp){ Rational Z(num*Temp.den-Temp.num*den,den*Temp.den); Z.REDUCE(Z); return Z;		}; Rational MULTIPLY(Rational Temp){ Rational Z(num*Temp.num,den*Temp.den); Z.REDUCE(Z); return Z;		}; Rational DIVIDE(Rational Temp){ Rational Z(num*Temp.den,den*Temp.num); Z.REDUCE(Z); return Z;		}; };

Rational R1, R2, R3;

void chooser { char choice; cout<<"Please enter operator sign (+,-,*,/): "; cin>>choice; if (choice=='+') R3=R1.ADD(R2); else if (choice=='-') R3=R1.MINUS(R2); else if (choice=='*') R3=R1.MULTIPLY(R2); else if (choice=='/') R3=R1.DIVIDE(R2); else chooser; }

int main { int n1,d1,n2,d2,n3,d3; cout<<"Please enter data for each rational number in this format.\n"; cout<<"num1 den1\nnum2 den2\nnum3 den3\n"; cin>>n1>>d1>>n2>>d2>>n3>>d3; R1.Set_num(n1); R1.Set_den(d1); R2.Set_num(n2); R2.Set_den(d2); R3.Set_num(n3); R3.Set_den(d3); chooser; cout<<"R1 = "; R1.Display; cout<<"R2 = "; R2.Display; cout<<"R3 = "; R3.Display; system("pause"); return 0; } code