CIS 250 - Fraction Calculator
Objectives
- Create and use functions.
- Use a custom library and namespace.
- Create a robust user interface with input error checking.
- Use exceptions to indicate errors.
Resources
- kishio.h
- kishio.cpp
Assignment notes
- Add documentation comments to your code.
- Use the kishio library for your user input.
- The kishio.h and kishio.cpp files are available on the remote server in the
~dklick/examples/ directory. You can copy them to your current directory using
this command (the space and period at the end are vital):
cp ~dklick/examples/kishio.* .
- You will need eight methods (including main). One is given to you. Their
requirements are specified below:
- menu: The menu function takes no arguments, but returns a char
which represents the user's choice. It prints out some instructions
and then asks the user to choose an option. Use the kishio library to
do your user input. Allow the user to enter upper or lower case for
the options. The menu should reject invalid input. The kishio function
will do that for you automatically if you use it correctly. The text to
be displayed is provided later on this page.
- gcd: This function takes two integer arguments and returns an
integer which is the greatest common divisor of the two arguments. This
function is provided for you later on this page.
- simplify: This function takes two references as arguments, a numerator
and a denominator. If the numerator is 0, then it should set the denominator
to 1. Otherwise, it should get the greatest common divisor of the numerator
and denominator, divide them by that divisor, and set them to those values.
For example, if the arguments are 30 and 12, then the gcd is 6, and this
function would set the numerator to 5 and the denominator to 2.
- addFractions: This function takes six arguments and returns nothing.
The first two arguments are the numerator and denominator of one fraction.
The next two arguments are the numerator and denominator of a second fraction.
The last two arguments are reference variables that are used to "return" the
numerator and denominator of the resulting fraction. If either denominator
sent in is 0, then an invalid_argument exception should be thrown with an
appropriate error message. Add the two fractions and simplify the resulting
numerator and denominator before returning. The math is provided later on
this page.
- subtractFractions: This function takes six arguments and returns nothing.
The first two arguments are the numerator and denominator of one fraction.
The next two arguments are the numerator and denominator of a second fraction.
The last two arguments are reference variables that are used to "return" the
numerator and denominator of the resulting fraction. If either denominator
sent in is 0, then an invalid_argument exception should be thrown with an
appropriate error message. Subtract the two fractions and simplify the resulting
numerator and denominator before returning. The math is provided later on
this page.
- multiplyFractions: This function takes six arguments and returns nothing.
The first two arguments are the numerator and denominator of one fraction.
The next two arguments are the numerator and denominator of a second fraction.
The last two arguments are reference variables that are used to "return" the
numerator and denominator of the resulting fraction. If either denominator
sent in is 0, then an invalid_argument exception should be thrown with an
appropriate error message. Multiply the two fractions and simplify the resulting
numerator and denominator before returning. The math is provided later on
this page.
- divideFractions: This function takes six arguments and returns nothing.
The first two arguments are the numerator and denominator of one fraction.
The next two arguments are the numerator and denominator of a second fraction.
The last two arguments are reference variables that are used to "return" the
numerator and denominator of the resulting fraction. If either denominator
sent in is 0, then an invalid_argument exception should be thrown with an
appropriate error message. If the numerator of the second fraction is 0, then
an invalid_argument should be thrown with an appropriate error message. Divide
the two fractions and simplify the resulting numerator and denominator before
returning. The math is provided later on this page.
- main: The main function must contain a loop to process user requests.
Processing a user request consists of displaying the menu and storing the
returned user choice. If the choice was not to quit, then the program must
ask the user for the numerator and denominator of two fractions. These should
be integer values. Based on the user choice, the code should then call one of
the four functions that does fraction math. Finally, the code should display
the numerator and denominator that were set by the fractional math function.
The code that calls the fractional math functions and displays the result
should be inside a try block so that any exceptions thrown during processing
the fractions displays the appropriate error message instead of trying to
display a result.
Recursive gcd funtion
int gcd(const int n1, const int n2) {
int x = n1, y = n2;
if (x < 0) x = -x;
if (y > 0) y = -y;
if (y==0) return x;
else return gcd(y, x%y);
}
Menu function text
This program performs math operations on fractions.
You get to choose what type of operation and then
enter the numerator and denominator for the operation.
Please choose one of the following:
A) Add fractions
S) Subtract fractions
M) Multiply fractions
D) Divide fractions
Q) Quit fractions
Fraction math
- (n1/d1) + (n2/d2) => (n1 * d2 + n2 * d1) / (d1 * d2)
- (n1/d1) - (n2/d2) => (n1 * d2 - n2 * d1) / (d1 * d2)
- (n1/d1) * (n2/d2) => (n1 * n2) / (d1 * d2)
- (n1/d1) / (n2/d2) => (n1 * d2) / (d1 * n2)
Grading rubric
Note: Program must be able to be compiled and run to get any points.
- 5 pts: style conventions followed (no tabs, proper indentation, documentation comments)
- 0 pts: gcd is coded properly (you are given this code in the assignment)
- 5 pts: menu is coded properly and includes error checking of input
- 5 pts: simplify is coded properly
- 6 pts: addFractions is coded properly including the exception handling
- 6 pts: subtractFractions is coded properly including the exception handling
- 6 pts: multiplyFractions is coded properly including the exception handling
- 7 pts: divideFractions is coded properly including the exception handling
- 10 pts: main is coded properly and fulfills requirements
Sample output
Click here to show/hide output.
This program performs math operations on fractions.
You get to choose what type of operation and then
enter the numerator and denominator for the operation.
Please choose one of the following:
A) Add fractions
S) Subtract fractions
M) Multiply fractions
D) Divide fractions
Q) Quit fractions
Enter choice: Add
Error: Must be one of: ASMDQasmdq
Enter choice: a
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 4
Enter the numerator of the second fraction: five
Error: Invalid number
Enter the numerator of the second fraction: 5
Enter the denominator of the second fraction: 4
Result = 3 / 2
This program performs math operations on fractions.
You get to choose what type of operation and then
enter the numerator and denominator for the operation.
Please choose one of the following:
A) Add fractions
S) Subtract fractions
M) Multiply fractions
D) Divide fractions
Q) Quit fractions
Enter choice: s
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 4
Enter the numerator of the second fraction: 5
Enter the denominator of the second fraction: 4
Result = -1 / 1
This program performs math operations on fractions.
You get to choose what type of operation and then
enter the numerator and denominator for the operation.
Please choose one of the following:
A) Add fractions
S) Subtract fractions
M) Multiply fractions
D) Divide fractions
Q) Quit fractions
Enter choice: m
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 4
Enter the numerator of the second fraction: 5
Enter the denominator of the second fraction: 4
Result = 5 / 16
This program performs math operations on fractions.
You get to choose what type of operation and then
enter the numerator and denominator for the operation.
Please choose one of the following:
A) Add fractions
S) Subtract fractions
M) Multiply fractions
D) Divide fractions
Q) Quit fractions
Enter choice: 1
Error: Invalid char
Enter choice: d
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 4
Enter the numerator of the second fraction: 0
Enter the denominator of the second fraction: 4
Error: Numerator of second fraction can not be 0
This program performs math operations on fractions.
You get to choose what type of operation and then
enter the numerator and denominator for the operation.
Please choose one of the following:
A) Add fractions
S) Subtract fractions
M) Multiply fractions
D) Divide fractions
Q) Quit fractions
Enter choice: d
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 0
Enter the numerator of the second fraction: 2
Enter the denominator of the second fraction: 0
Error: Denominator can not be 0
This program performs math operations on fractions.
You get to choose what type of operation and then
enter the numerator and denominator for the operation.
Please choose one of the following:
A) Add fractions
S) Subtract fractions
M) Multiply fractions
D) Divide fractions
Q) Quit fractions
Enter choice: d
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 4
Enter the numerator of the second fraction: 5
Enter the denominator of the second fraction: 4
Result = 1 / 5
This program performs math operations on fractions.
You get to choose what type of operation and then
enter the numerator and denominator for the operation.
Please choose one of the following:
A) Add fractions
S) Subtract fractions
M) Multiply fractions
D) Divide fractions
Q) Quit fractions
Enter choice: q