Fraction calculator

Objectives

  • Create and use functions.
  • Use a custom library and namespace.
  • Use reference variables as parameters to return values from functions.
  • Create a robust user interface with input error checking.
  • Use exceptions to indicate errors.

Resources

Assignment requirements

  • 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

Menu function text

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