Assignment:
(1) Design a modular program to solve the following problem.
(2) Enter and successfully run the program in the Visual C++ environment Problem (100 points):
Write a program to do fractional arithmetic. The program should handle the operations of addition,subtraction, multiplication, and division. It should also reduce the answer to its lowest form.
The program should have separate functions to handle the input and output of fractions, and to perform the various operations. The user should be allowed to enter any number of problems. The main program should only contain the variable declarations, the main loop, and function calls. Your program must consist of at least the following functions:
- A function to read in a single fraction
- A function to write out a single fraction
- A function to read in a problem
- A function to solve the problem (this may call other functions to perform the addition,subtraction, multiplication, and division, but this is not required)
- A function to reduce a fraction
The program will assume that fractions are entered properly (no zero denominators).
A sample dialogue follows (yours does not have to be like this). The user responses are underlined:
=======================================================================
Fractional Arithmetic Program
This program will perform arithmetic on fractions. Problems should be entered like this:
2/5 - 4/7
The allowed operations are addition (+), subtraction (-), multiplication (*), and division (/).
There must be at least one space around these operators.
Please enter a problem: 1/2 + 1/4
The answer is 3/4
Another problem (y/n)? y
Please enter a problem: 2/9 / 3/4
The answer is 8/27
Another problem (y/n)? n
Goodbye.
=======================================================================
To reduce a fraction, you will need to find the greatest common factor of the numerator and denominator. Once it is found, divide both the numerator and denominator by this value.
The greatest common factor (gcf) of two integers is the largest integer that will divide both numbers evenly. For example, the greatest common factor of 18 and 27 is 9, since 9 is the largest integer to divide evenly into both 18 and 27. Since 9 goes into 18 twice and into 27 three times, the fraction 18/27 can be reduced to 2/3. An efficient algorithm for computing the gcf is attributed to the Greek
mathematician Euclid (ca. 300 B.C.).
Euclid's algorithm:
1. let A and B be two positive integers
2. let R = remainder after dividing A by B
3. while R != 0
A = B
B = R
R = remainder after dividing A by B
4. B is the gcf
For example, consider finding the gcf of 24 and 60:
A = 24, B = 60 24/60 is 0 with remainder R = 24
set A = 60, B = 24 60/24 is 2 with remainder R = 12
set A = 24, B = 12 24/12 is 2 with remainder R = 0
So, the gcf is 12
Use sufficient test data sets to show your program works (at least one for each arithmetic operation,
which may include sets showing that the fraction reducing also works).
Some useful formulae (NOTE: deal with the numerators and denominators separately!):
bc
ad
d
c
b
a
bd
ac
d
c
b
a
bd
ad bc
d
c
b
a
bd
ad bc
d
c
b
a
÷ =
× =
-
- =
+
+ =
Extra Bonus Part A (10 points). This part is optional. If you can get it done, you will receive 10 extra
points.
Modify your program for Assignment 4 so that it can compute a list of fractions. For example, it can
compute
.
5
4
11
7
33
25
13
12 + × ÷
Extra Bonus Part B (20 points). This part is optional. If you can get it done, you will receive 20 extra
points.
Modify your program for Extra Bonus Part A so that it can compute a list of fractions with possible
parentheses. For example, it can compute
.
5
4
11
7
109
67
45
11
33
25
13
12
??