TEACHER\'S GUIDELINES The project is to revisit and test a class named Rational
ID: 3592484 • Letter: T
Question
TEACHER'S GUIDELINES
The project is to revisit and test a class named Rational that can be used to store rational numbers.
A rational number is nothing more than a number than can be represented by a fraction, i.e., the ratio of two whole numbers. A whole number like 100 is represented as:
100/1
A fractional number like 0.5 is represented by:
1/2
Your class will need to store two internal, integer values for each Rational number, the numerator (top) and denominator (bottom) of the fraction. It will have three constructor functions, with zero, one and two arguments, used as follows:
Rational test1, test2(10), test3(1, 2);
The declaration for test1 calls the default (no argument) constructor, which should set the value to 0. (Like any other whole number, zero will have a 1 in the denominator: we cannot divide by zero.) The declaration for test2 calls a constructor with one argument. The value for test2 will be 10, stored as 10 on the top and 1 on the bottom. The declaration for test3 calls the constructor with two arguments. test3 is equal to 0.5, with 1 on the top and 2 on the bottom.
All of the operators required in the problem should be provided for the Rational class as overloaded, Friend functions. In general, you should be able to execute code such as following:
// Display the three values to test cout
cout << " Test1 equals " << test1;
cout << " Test2 equals " << test2;
cout << " Test3 equals " << test3;
// Test our operators
cout << " Test1 * Test2 equals " << test1*test2;
cout << " Test1 / Test3 equals " << test1/test3;
cout << " Test2 + Test3 equals " << test2+test3;
cout << " Test3 - Test1 equals " << test3-test1;
if (test1 == test2)
cout << " Test1 is equal to Test2";
if (test1 < test2)
cout << " Test1 is less than Test2";
and so on until you test all the implemented member functions and Friend functions of the Rational class.
Hints on Comparisons
The textbook provides some hints on ways to compare two ratios. Be sure to read them and use them. It will also be okay, though not optimal, for your comparison operators to temporarily convert the ratios to floating point values. But that could leave you open to surprising results, especially on the == operator, where two converted ratios might not be exact to the last bit. Try to use the hints before resorting to conversions.
"Rationalizing" the Numbers
You should provide a way to "rationalize" the stored numbers. That is fancy way of saying that they should reduce to their lowest denominator and be as simple as possible. For example, a number declared as:
Rational testValue(25, 10);
should be stored with a 5 on the top and a 2 on the bottom. Similarly, a number declared as:
Rational testValue(-6, -4);
should be stored as two positive values, 3 on top and 2 on the bottom. We should rationalize the initial values of numbers and rationalize the results of any math operators. This will be an excellent place to define an internal, private function that can take a top and bottom value and "rationalize" them.
Here is the simplest way to rationalize the numbers (it's not mathematically optimal but it is guaranteed to work):
Find the smaller of the numerator and denominator
For every value from that smaller number on down to 2, see if the value divides both numerator and denominator evenly. If so, divide them by that value and keep going.
Be sure to count down by one, not up. We need to divide by higher powers before lower powers. In other words, we want to divide by 8 before dividing by 4, and 4 before dividing by 2, or we won't end up fully reduced.
You should call your rationalizing function every time the numerator and denominator change, to always keep them reduced. That way every bit of your code can rightly assume it is working with rationalized values.
Implementing the Math Operators
These numbers are simply fractions. We perform the basic operations the same way we learned back in elementary school. You may need to track down a fourth grader to help, but the basic math is pretty simple. If your programming gets too complicated you are missing a simpler solution.
Examples of Correctly Completed Project
Here is some sample output of my solution. The user first enters two whole numbers, which are used to create two Rational numbers using constructors. The user is also asked to enter a value in "Rational" format to test the overloaded >> operator. My implementation of << and >> expects the numbers to be enclosed in parentheses and separated by a comma:
Solution to Homework 6 Problem 5
Please enter two non-zero integers: 14 6
Please enter Rational number: (22, 4)
Test1 equals (7, 3)
Test2 equals (14, 1)
Test3 equals (11, 2)
Test1 * Test2 equals (98, 3)
Test1 / Test3 equals (14, 33)
Test2 + Test3 equals (39, 2)
Test3 - Test1 equals (19, 6)
Test1 is less than Test2
Test2 is greater than Test3
Test3 is greater than or equal to Test1
Another test case?
You'll have to do some character by character parsing on the >> operator, to detect the use of the parentheses and the comma. But if you are careful you can mix character by character reading with use of the standard >> operator that reads int values. For example, once you read the ( character you should be able to use >> to read the number, then back to character read to get the comma, then >> to get the next number. It will be okay if your code doesn't handle improper entries. In the above, if the use were to enter alphabetics instead of numbers:
Please enter Rational number: (A, B)
it will be okay if the program chokes on that. We don't need to get carried away with the input processing, we are focused on the mechanics of overloading.
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 O (that is, to 0/1). 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. To help with the normalize function, it may be helpful to also implement a greatest common divisor function. Overload the input and output operators >>andExplanation / Answer
#include using namespace std; class rationalNumbers { public: friend rationalNumbers operator +(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2); friend rationalNumbers operator -(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2); friend rationalNumbers operator -(const rationalNumbers& FRACTION1); friend rationalNumbers operator *(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2); friend rationalNumbers operator /(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2); friend bool operator ==(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2); friend bool operator =(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2); friend istream& operator >>(istream& ins, rationalNumbers& fraction); friend ostream& operatorRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.