Consider fractions such as
,
, and
. In a computer we usually represent them in their realforms: 0.333333, 0.833333, and 1.142857. The real form is not exact and it is sometimes better torepresent a fraction as two integers - the numerator and the denominator. For example,
would berepresented as a 5 (the numerator) and a 6 (the denominator). A negative fraction is represented bya negative numerator and positive denominator. For example,
is represented by -1 and 3.
The rules for fraction arithmetic are:
Fraction arithmetic can produce a large numerator and
denominator. It is preferred that the result be
converted to its reduced equivalent. The reduced
form has as small a numerator and denominator as
possible. For example, the reduced form of
is
and the reduced form of
is
.
Calculating the reduced form requires calculating the greatest common divisor of the numerator and denominator. For any two integers I and J, the greatest common divisor is the largest positive integerthat divides both I and J. For example, the greatest common divisor of 18 and 12 is 6 and the greatest common divisor of -8 and 20 is 4. The greatest common divisor is always less than or equal to the smaller of the absolute values of I and J.
For any fraction
, the reduced form is
where D is the greatest common divisor of I and J.
For example, the reduced form of
is
and the reduced form of
is
.
Write a program which does the following:
1. Prompts for the numerator and denominator of a fraction. If the denominator is 0 or negative, issue an error message and re-prompt.
2. Prompts for a second fraction (see step 1).
3. Prompts for an operator (add, subtract, multiply, divide). If the user's reply is invalid, issue an error message and re-prompt.
4. Performs the specified operation and displays the resulting fraction in its reduced form.
The program must contain the following functions:
A function which returns the greatest common divisor of two integers. The function must not write to or read from the screen.
A function which prompts for a fraction.
A function for each operator (add, subtract, multiply, divide). These functions must not write to or read from the screen.
Use reference parameters only when necessary.The main part of your program should contain mostly procedure calls plus the prompt for an operator and the display of the result. Use the design of lab6 as a model for the project.
Name your program fraction.cpp. Submit a listing of the program and a disk containing the program. The project will be graded on a 50 point basis and 5 points will be deducted for each day late (except Sunday).