Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Fraction calculator Need help with code, cant use using namespace std Objectives Create and use functions. Use a custom library and namespace. Use reference

C++ Fraction calculator

Need help with code, cant use "using namespace std"

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

  • kishio.h
  • kishio.cpp

Assignment requirements

  • 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions