/* uses a class to represent a rational as a quotient of an integer by a positive integer. The member functions implement arithmetic operators. The function gcd is used to reduce quotients to lowest terms.*/ #include #include struct Rational { int num; int denom; }; void normalize(int int1, int int2, Rational& r); //pre: int1 and int2 are integers //post: r.num/r.denom has r.denom > 0 and if r.num=0 then r.denom = 1 int gcd(int int1, int int2); //computes gcd of int1, int2 void input_rat(Rational& r); //post: the rational has a normalized numerator and denominator assigned void output_rat(Rational& r); int equal(Rational r1, Rational r2); //true if r1 = r2 Rational add(Rational r1, Rational r2); // the normalized sum of r1 and r2 Rational mult(const Rational r1, const Rational r2); Rational div(const Rational r1, const Rational r2); int main() { Rational r1,r2,r4; input_rat(r1); cout << "\nThe first rational normalized is : "; output_rat(r1); cout << endl; input_rat(r2); cout << "\nThe second rational normalized is : "; output_rat(r2); r4 = add(r1,r2); cout << "\nThe sum of the two is: " ; output_rat(r4); r4 = mult(r1,r2); cout << "\nThe product of the two is: "; output_rat(r4); r4 = div(r1,r2); cout << "\nthe quotient of the two is: "; output_rat(r4); return 0; } void normalize(int int1, int int2, Rational& r) { int temp; if (int1 == 0) int2 = 1; else if (int2 < 0) { int1 = -int1; int2 = -int2;} temp = gcd(int1, int2); int1 = int1/temp; int2 = int2/temp; r.num = int1; r.denom = int2; } int gcd(int int1, int int2) { int temp1, temp2, temp3; temp1 = abs(int1); temp2 = abs(int2); if (temp1 < temp2) {temp3 = temp2; temp2 = temp1; temp1 = temp3;}; while (temp1 % temp2 != 0) {temp3 = temp2; temp2 = temp1 % temp2; temp1 = temp3; }; //while return temp2; } //gcd void input_rat(Rational& r) { int errcount = 0, int1, int2; cout << "\n\nEnter an integer numerator:"; cin >> int1; cout << "Enter an integer denominator:"; cin >> int2; while (int2 == 0) { errcount ++; if (errcount > 3) {cout << "If you insist, the denominator is 0, will cause errors"; exit (1);} cout << "Denominator 0!! Try again:"; cin >> int2; } //while normalize(int1, int2, r); } void output_rat(Rational& r) { cout << r.num << '/' << r.denom; } int equal(Rational r1, Rational r2) { normalize(r1.num, r1.denom, r1); // just to be sure normalize(r2.num, r2.denom, r2); return ((r1.num == r2.num) && (r1.denom == r2.denom)); } Rational add(Rational r1, Rational r2) // note that the following code is the same as for Add(r1,r2) { Rational temp; int temp_num, temp_denom; temp_num = r1.num*r2.denom + r2.num*r1.denom; temp_denom = r1.denom*r2.denom; normalize(temp_num, temp_denom, temp); return temp; } Rational mult(const Rational r1, const Rational r2) // similar to + { Rational temp; int t1, t2; t1 = r1.num*r2.num; t2 = r1.denom*r2.denom; normalize(t1,t2,temp); return temp; } Rational div(const Rational r1, const Rational r2) { Rational temp; int t1, t2; t1 = r1.num*r2.denom; t2 = r1.denom*r2.num; normalize(t1,t2,temp); return temp; }