Caleb+foo

code format="cpp" /* Caleb Foo 3S113 Classes Rational Task
 * 1) include
 * 1) include

using namespace std;

class Rational {	protected: int numer, denom;

Rational Simplify{ Rational simplified; //Store values of the new fraction int diff = abs(numer-denom); //As numerator and denominator cannot divide by more than the difference if(diff != 0){//Cannot divide by 0 if diff = 0 for(int i=diff; i>0; i--){ if(numer%i == 0 && denom%i == 0){//Highest common factor is found since for loop begins from the top simplified.numer = numer/i; simplified.denom = denom/i; break; }				}				return (simplified); }			else{ simplified.numer = 1; simplified.denom = 1; return (simplified); }	};

public: Rational{numer = 0; denom = 1;}; //Set default values of Rational object Rational(int a, int b){ numer = a;			denom = b;		};

~Rational{ };

Rational Add(Rational fraction){ Rational temp;//Temporary storage of new fraction temp.numer = numer*fraction.denom + fraction.numer*denom; // a/b + c/d = (ad + bc)/bd temp.denom = denom * fraction.denom; temp = temp.Simplify; return (temp); };

Rational Minus(Rational fraction){ Rational temp;//Temporary storage of new fraction temp.numer = numer*fraction.denom - fraction.numer*denom; // a/b - c/d = (ad - bc)/bd temp.denom = denom * fraction.denom; temp = temp.Simplify; return (temp); };

Rational Multiply(Rational fraction){ Rational temp;//Temporary storage of new fraction temp.numer = numer*fraction.numer; temp.denom = denom * fraction.denom; temp = temp.Simplify; return (temp); };

Rational Divide(Rational fraction){ Rational temp;//Temporary storage of new fraction temp.numer = numer*fraction.denom; // (a/b)/(c/d) = ad/bc temp.denom = denom * fraction.numer; temp = temp.Simplify; return (temp); };

void Display{ cout << numer << "/" << denom << endl; }; };

int main {	Rational R1(5,6), R2(1,4), R3; //Display fractions on screen cout << "First fraction is: "; R1.Display; cout << "Second fraction is : "; R2.Display;

int choice; cout << "Would you like to:\n1.Add\n2.Minus\n3.Multiply\n4.Divide\n"; cin >> choice;

switch(choice){ case 1: R3 = R1.Add(R2); cout << "Quotient is: "; R3.Display; break; case 2: R3 = R1.Minus(R2); cout << "Quotient is: "; R3.Display; break; case 3: R3 = R1.Multiply(R2); cout << "Quotient is: "; R3.Display; break; case 4: R3 = R1.Divide(R2); cout << "Quotient is: "; R3.Display; break; default: break; }

cout << "\n"; system("pause");

} code