Question
I need to be able to do this Rational r1 = 1; cout < < (r1 == 1); // prints true cout < < (2
I need to be able to do this
Rational r1 = 1;
cout << (r1 == 1); // prints true
cout << (2 == r1); // prints false
cout << (r1 < 2); // prints true
cout << (0 < r1); // prints false
cout << (r1 + 2); // prints 3/1 (or just 3)
To do this, you must provide overloads of all operators that take an int as one of the arguments, and a Rational as the other. This will increase the size of your program considerable.
So far i have
Rational Rational::operator+(const Rational &r, int s)const { Rational t; t.numerator = r.numerator * denominator; t.numerator += r.denominator * numerator; t.denominator = r.denominator * denominator; return t;}
Im not sure how incorporate the int. I have a constructor that accepts an int and gives a rational number (Rational(int)). im also getting an error saying it can accept zero or 1. I also have to write the function for ==, !=, <. <=, >, >=. +,-,*,/. I have access to a gcd and a lcm function aswell. The other functions as i have them are
// 1. Comparing two rational numbers for equality: // - r1 == t2 bool Rational::operator==(const Rational &r, int s) const { Rational(s); // use this to create the int to a rational number with num and denom return (r.numerator * denominator == r.denominator*numerator); } // - r1 != r2 bool Rational::operator!=(const Rational &r, int s) const { Rational(s); // use this to create the int to a rational number with num and denom return !(r.numerator * denominator == r.denominator*numerator); }
// 2. Ordering rational numbers // - r1 < r2 bool Rational::operator<(const Rational &r, int s) const { int num1 = numerator * r.denominator; int num2 = denominator * r.numerator; return(num1 < num2); } // - r1 > r2 bool Rational::operator>(const Rational &r, int s) const { int num1 = numerator * r.denominator; int num2 = denominator * r.numerator; return(num1 > num2); } // - r1 <= r2 bool Rational::operator<=(const Rational &r, int s) const { int num1 = numerator * r.denominator; int num2 = denominator * r.numerator; return(num1 <= num2); } // - r1 >= r2 bool Rational::operator>=(const Rational &r, int s) const { int num1 = numerator * r.denominator; int num2 = denominator * r.numerator; return(num1 >= num2); }
// 3. The standard arithmetic operators // - r1 + r2 // purpose: add two rational numbers Rational Rational::operator+(const Rational &r, int s)const { Rational t; t.numerator = r.numerator * denominator; t.numerator += r.denominator * numerator; t.denominator = r.denominator * denominator; return t; } // - r1 - r2 //purpose: subtract two rational numbers Rational Rational::operator-(const Rational &r, int s)const { Rational t; t.numerator = r.denominator * numerator; t.numerator -= denominator * r.numerator; t.denominator = r.denominator * denominator; return t;
} // - r1 * r2 // purpose: multiply two rational numbers Rational Rational::operator*(const Rational &r, int s)const { Rational t; t.numerator = r.numerator * numerator; t.denominator = r.denominator * denominator; return t; } // - r1 / r2 Rational Rational::operator/(const Rational &r, int s)const { Rational t; t.numerator = r.denominator * numerator; t.denominator = denominator * r.numerator; return t; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started