GRP4

NAME 2: Aldric Bek
 #include using namespace std; class Rational{

protected: int num, den;

public: Rational{num=0; den=1;}; ~Rational{}; void Set_num(int n){num = n; }; void Set_den(int d){den = d; }; //add Rational add(Rational Y) { Rational Z; Z.num = num*Y.den + Y.num*den; Z.den = den*Y.den; int x; x = 2; while (x <= Z.num) { if (((Z.num % x) == 0)&&((Z.den % x) == 0)) {

Z.den = Z.den / x; Z.num = Z.num / x; } x++; } return Z;}; //minus Rational minus(Rational Y) { Rational Z; int isnegative; Z.num = num*Y.den - Y.num*den; Z.den = den*Y.den; if (Z.num < 0) { isnegative = 1; } Z.num = abs(Z.num); int x; x = 2; while (x <= Z.num) { if (((Z.num % x) == 0)&&((Z.den % x) == 0)) { Z.den = Z.den / x; Z.num = Z.num / x; } x++; } if (isnegative == 1) { Z.num = Z.num * -1; } return Z; }; //mulitply Rational multiply(Rational Y) { Rational Z; Z.num = num*Y.num; Z.den = den*Y.den; int x; x = 2; while (x <= Z.num) { if (((Z.num % x) == 0)&&((Z.den % x) == 0)) { Z.den = Z.den / x; Z.num = Z.num / x; } x++; } return Z;}; //divide Rational divide(Rational Y) { Rational Z; Z.num = num*Y.den; Z.den = den*Y.num; int x; x = 2; while (x <= Z.num) { if (((Z.num % x) == 0)&&((Z.den % x) == 0)) { Z.den = Z.den / x; Z.num = Z.num / x; } x++; } return Z;}; void Display{ if (num == den) { cout << "1" <<endl; } else if (num == 0) { cout << "0" <<endl; } else if (den == 1) { cout << num <<endl; } else { cout << num << "/" << den << endl; } }; }; int main { Rational A, B, C, D, E, F; A.Set_num(2); A.Set_den(4); B.Set_num(4); B.Set_den(8); C = A.add(B); C.Display; D = A.minus(B); D.Display; E = A.multiply(B); E.Display; F = A.divide(B); F.Display; system("pause"); return 1; }