/* 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 class Rational {private: int num; int denom; void normalize(); //pre: num and denom are integers //post: num/denom has denom > 0 and if num=0 then denom = 1 int gcd(); //computes gcd of num, denom public: void input(); //post: the rational has a numerator and denominator assigned void output(); void set(int new_num, int new_denom); //pre: new_num and new_denom are a possible rational //post: the rational is returned with relatively prime factors // accessor functions int get_num(); int get_denom(); // Class constructors Rational(); Rational(int); // for a whole number Rational(int,int); // a friend function friend int equal(Rational r1, Rational r2); //Arithmetic functions with operator overload friend Rational operator +(const Rational& r1, const Rational& r2); // const call-by-reference parameters! // saves copying friend Rational operator -(const Rational& r1); // unary negation friend Rational operator *(const Rational& r1, const Rational& r2); // const call-by-reference parameters! friend Rational operator /(const Rational& r1, const Rational& r2); // const call-by-reference parameters! }; //Rational int main() { Rational r1,r2,r4; r1.input(); cout << "\n r1 normalized is :"; r1.output(); cout << endl; r2.input(); cout << "\n r2 normalized is :"; r2.output(); r4 = r1 + r2; cout << "\n the sum of r1 and r2 is: "; r4.output(); r4 = -r4; cout << "\n minus the sum: " ; r4.output(); r4 = r1*r2; cout << "\n the product of r1 and r2 is: "; r4.output(); r4 = r1/r2; cout << "\n the quotient of r1 and r2 is: "; r4.output(); return 0; } void Rational::normalize() { int temp; if (num == 0) denom = 1; else if (denom < 0) { num = -num; denom = -denom;} temp = gcd(); num = num/temp; denom = denom/temp; } int Rational::gcd() { int temp1, temp2, temp3; temp1 = abs(num); temp2 = abs(denom); 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 Rational::input() { int errcount = 0; cout << "\n\nEnter an integer numerator:"; cin >> num; cout << "Enter an integer denominator:"; cin >> denom; while (denom == 0) { errcount ++; if (errcount > 3) {cout << "If you insist, the denominator is 0, will cause errors"; exit (1);} cout << "Denominator 0!! Try again:"; cin >> denom; } //while normalize(); } void Rational::output() { cout << num << '/' << denom; } void Rational::set(int new_num, int new_denom) { num = new_num; denom = new_denom; normalize(); } int Rational::get_num() { return num;} int Rational::get_denom() { return denom;} Rational::Rational() { }// or can be set to 0 Rational::Rational(int whole) { num = whole; denom = 1; } Rational::Rational(int new_num, int new_denom) { num = new_num; denom = new_denom; if (denom == 0) cout << "Denominator initialized to 0!! - check program file "; normalize(); } int equal(Rational r1, Rational r2) // NOTE - NO scope resolution operator { r1.normalize(); // just to be sure r1.normalize(); // and note we can access the PRIVATE function normalize return ((r1.num == r2.num) && (r1.denom == r2.denom)); // and member vars } Rational operator +(const Rational& r1, const 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; temp.set(temp_num, temp_denom); return temp; } Rational operator -(const Rational& r1) { Rational temp; temp.set(-r1.num, r1.denom); // accessing r1 private variables return temp; } Rational operator *(const Rational& r1, const Rational& r2) // similar to + { Rational temp; temp.set(r1.num*r2.num, r1.denom*r2.denom); return temp; } Rational operator /(const Rational& r1, const Rational& r2) { Rational temp; temp.set(r1.num*r2.denom, r1.denom*r2.num); return temp; }