Define a class for rational numbers. A rational number is a number that can be r
ID: 2990636 • Letter: D
Question
Define a class for rational numbers. A rational number is a number that can be
represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so
forth are all rational numbers. (By 1/2 and so on we mean the everyday fraction, not
the integer division this expression would produce in a C++ program.) Represent
rational numbers as two values of type int , one for the numerator and one for
the denominator. Call the class Rational . Include a constructor with two arguments
that can be used to set the member variables of an object to any legitimate
values. Also include a constructor that has only a single parameter of type int ;
call this single parameter wholeNumber and define the constructor so that the
object will be initialized to the rational number wholeNumber /1. Include a default
constructor that initializes an object to 0 (that is, to 0/1). Overload the input and
output operators >> and << . Numbers are to be input and output in the form
1/2 , 15/32 , 300/401 , and so forth. Note that the numerator, the denominator, or
both may contain a minus sign, so -1/2 , 15/-32 , and -300/-401 are also possible
inputs. Overload all the following operators so that they correctly apply to the type
Rational : == , < , <= , > , >= , + , - , * , and / . Write a test program to test your class.
Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are
positive rational numbers, a/b is less than c/d provided a*d is less than c*b . You
should include a function to normalize the values stored so that, after normalization,
the denominator is positive and the numerator and denominator are as small
as possible. For example, after normalization 4/-8 would be represented the same
as -1/2.
The output should look like this:
Run1:
Enter a fraction in the format integer_numerator/integer_denominator
-25/-9
You entered the equivalent of: 25/9
Enter a fraction in the format integer_numerator/integer_denominator
3/5
You entered the equivalent of: 3/5
Enter a fraction in the format integer_numerator/integer_denominator
-21/9
You entered the equivalent of: -7/3
Testing arithmetic and relational operator overloading
25/9 * 3/5 = 5/3
25/9 + 3/5 = 152/45
25/9 - 3/5 = 98/45
25/9 / 3/5 = 125/27
25/9 < 3/5 = 0
25/9 < 25/9 = 0
25/9 <= 3/5 = 0
25/9 <= 25/9 = 1
25/9 > 3/5 = 1
25/9 > 25/9 = 0
25/9 >= 3/5 = 1
25/9 >= 25/9 = 1
-7/3 * 3/5 = -7/5
-7/3 + 3/5 = -26/15
-7/3 - 3/5 = -44/15
-7/3 / 3/5 = -35/9
-7/3 < 3/5 = 1
-7/3 < -7/3 = 0
-7/3 <= 3/5 = 1
-7/3 <= -7/3 = 1
-7/3 > 3/5 = 0
-7/3 > -7/3 = 0
-7/3 >= 3/5 = 0
-7/3 >= -7/3 = 1
Explanation / Answer
#include #include using namespace std; class Rational { public: Rational(); // Initializes the rational number to 0 Rational (int wholeNumber); // Initializes the rational number to wholeNumber/1 Rational (int m, int n); // Initializes the rational number to m/n if n is not 0; // the sign of the rational is stored in the numerator // (the denominator is always positive); // exits if n = 0 (invalid rational number) // I added to do some simple factoring void factorRational(); // Precondition: The rational number is defined. // Postcondition: The rational number is identical or may have been factored. void output(); // Precondition: The rational number is defined. // Postcondition: The rational number has been displayed on // the screen in the form m/n. friend Rational operator +(const Rational& r1, const Rational& r2); // Precondition: r1 and r2 are valid rational numbers // Returns the sum of r1 and r2 friend Rational operator *(const Rational& r1, const Rational& r2); // Precondition: r1 and r2 are valid rational numbers // Returns the product of r1 and r2 private: int num; // the numerator of the number int denom; // the denominator of the number void standardize(); // Precondition: num and denom have values // Postcondition: if denom is 0 the program is terminated; // otherwise the rational number is standardized so that // denom is positive. }; // ======================= // main function // ======================= int main () { // // Variable declarations // char doAgain; // for the loop control int m, n; // numerator and denominator - m/n Rational sum, product; coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.