Joshua+Foo

code format="cpp" using namespace std;
 * 1) include

class Rational { protected: int num, den;

Rational Simplify { int hcf = 1, diff=abs(num-den); Rational temp; if(diff != 1 && diff !=0) {//If diff of both numbers is 1, HCF is 1 for(int i=diff;i>0;i--) {//Highest possible HCF is diff if(num%i==0 && den%i==0) { hcf = i;                                                                         temp.num = num/i; temp.den = den/i; break; }                                  }

return temp;

}                   else if (diff == 0) {//if num and den are the same, fraction is 1 temp.num = 1; temp.den = 1; return temp; }          };

public: Rational{num=0; den=1;}; Rational(int n, int d) {num = n; den = d;}; ~Rational {}; void Display{cout << num << "/" << den << endl;}; Rational Add(Rational temp) {//Add Rational r, Simp; r.num = num*temp.den + temp.num*den; r.den = den*temp.den; Simp = r.Simplify; return(Simp); };      Rational Minus(Rational temp) {//Minus Rational r, Simp; r.num = num*temp.den - temp.num*den; r.den = den*temp.den; Simp = r.Simplify; return(Simp); };      Rational Multiply(Rational temp) {//Multiply Rational r, Simp; r.num = num*temp.num; r.den = den*temp.den; Simp = r.Simplify; return(Simp); };      Rational Divide(Rational temp) {//Divide Rational r,Simp; r.num = num*temp.den; r.den = den*temp.num; Simp = r.Simplify; return(Simp); };

};

int main{ Rational R1(1,4), R2(1,4), R3(0,1); int func; cout << "The first fraction is ";//Display first fraction R1.Display; cout << "The second fraction is ";//Display second fraction R2.Display; cout << endl; cout << "What function do you wish to use?\n 1.Add\n 2.Minus\n 3.Multiply\n 4.Divide\nEnter: "; cin >> func; if(func == 1) {R3 = R1.Add(R2); R3.Display;} if(func == 2) {R3 = R1.Minus(R2); R3.Display;} if(func == 3) {R3 = R1.Multiply(R2); R3.Display;} if(func == 4) {R3 = R1.Divide(R2); R3.Display;} system("pause"); }

code