Rational Numbers
Let p and q be integers, with q being non-zero integer. A rational number n is a number which can be expressed as the following:1
n = p/q
It should be noted that any integer can be expressed as a rational number, since the integer q may be equal to 1.
As an example the double value in C++ 0.95 can be expressed as the following rational number:1
19/20
Arithmetic operations can be performed on rational numbers. For instance two rational numbers can be added, subtracted from, multiplied and divided. When adding and subtracting two rational numbers, the denominators first need to be reduced to a common denominator. As an example:1
5/4 + 1/6 = (15+2)/12 = 17/12
When multiplying two rational numbers, the numerators and the denominators are multiplied separately. Using integers a, b, c, and d as an example:1
(a/b) * (c/d) = ac/bd
When dividing two rational numbers, the second rational number is first inverted (known as its reciprocal) and then is multiplied with the first rational number like above:1
(a/b) ÷ (c/d) = (a/b) * (d/c) = ad/bc
It should be noted that rational numbers can be expressed in “simplest form” as well, for instance given the following rational number:1
10/15
If both the numerator and the denominator are each divided by 5, we obtain the following reduced rational number:1
2/3
Instructions
For this request, you are to write a C++ program which will read in rational numbers using the following convention (as an example):1
50 / 12
If in the code two integers n1 and d1 were used, after reading in the above from cin n1 would be set to 50 and n2 would be set to 12. (As a hint you can read the forward slash (/) into any arbitrary char data type.)
Your program will prompt the user for two rational numbers. Your program will then display the following menu for the user:1
2
3
41. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
If the user presses (1), the two rational numbers are added together. If the user presses (2) the two rational numbers are subtracted from each other. If the user presses (3) the two rational numbers are multiplied together. If the user presses (4) the two numbers are divided. After the answer is shown to the user, the user is prompted if he/she would like to run the program again.
Requirements
To receive full credit for this request, the following conditions must be met:
- If a user enters a zero value for either denominator, your program will loop until the user enters the rational number correctly.
- If the user enters an incorrect menu selection (correct values are 1, 2, 3 and 4), your program will loop until a correct value is entered.
- The program will loop as many times necessary until the user decides to quit.
- The output of each operation is a rational number expressed in reduced lowest terms. To express this, the greatest common divisor (GCD) must be obtained from the numerator and the denominator. The following C++ code can be used to calculate the GCD (which can be used to divide both the numerator and the denominator to reduce the number):
1
2
3
4
5
6
7
8int gcd, p, q;
for (int i=1; i<=p && i<=q; i++)
{
if ((p%i == 0) && (q%i == 0))
{
gcd=i;
}
}
Sample Output
1 | THIS PROGRAM PERFORMS ARITHMETIC OPERATIONS ON RATIONAL NUMBERS. |
Save your solution in a file named rational.cpp.