Question

C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//please find the code below, the user will be asked values for numerator and denominator of both the fractions, then option will be given to add , subtract, multiply or divide the fractions, if keyword other than A,S,M,D is used the program asks for valid operation. 'Q' terminates the program.

//Error is intitally handles by checking if the denominator is zero, if it is the case, then program terminates as fraction is indetrminate.

// Doubts can be clarifed in the comment section.

// run using g++ filename.cpp -std=c++14

-------------------------------------------------------------

// Example program
#include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
int gcd(int a,int b){
int r;
while(b){
r = a%b;
a = b;
b = r;
}
return a;
}
void simplify(pii &res){
int g = gcd(res.first,res.second);
res.first/=g;
res.second/=g;
}
pii addFractions(int n1,int d1,int n2,int d2){
int lcm = (d1*d2)/gcd(d1,d2);
int f1 = lcm/d1 , f2 = lcm/d2;
pii res;
res.first = f1*n1 + f2*n2;
res.second = lcm;
simplify(res);
return res;
}
pii subtractFractions(int n1,int d1,int n2,int d2){
int lcm = (d1*d2)/gcd(d1,d2);
int f1 = lcm/d1 , f2 = lcm/d2;
pii res;
res.first = f1*n1 - f2*n2;
res.second = lcm;
simplify(res);
return res;
}
pii multiplyFractions(int n1,int d1,int n2,int d2){
pii res = {n1*n2,d1*d2};
simplify(res);
return res;
}
pii divideFractions(int n1,int d1,int n2,int d2){
pii res = {n1*d2,n2*d1};
simplify(res);
return res;
}
int main()
{
int n1,d1,n2,d2;
cout<<"enter numerator of fraction1 ";
cin>>n1;
cout<<"enter denomenator of fraction1 ";
cin>>d1;
cout<<"enter numerator of fraction2 ";
cin>>n2;
cout<<"enter denomenator of fraction1 ";
cin>>d2;
if(d1==0 || d2==0)
{
cout<<"Denominator is zero\n";
return 0;
}
char c='0';
try{
while(c!='Q'){
cout<<"select choice for operation on fraction1 <operation> fraction2 , Q to exit ";
cin>>c;
pii res;
if(c=='A'){
res = addFractions(n1,d1,n2,d2);
}
else if(c=='S'){
res = subtractFractions(n1,d1,n2,d2);
}
else if(c=='M'){
res = multiplyFractions(n1,d1,n2,d2);
}
else if(c=='D'){
res = divideFractions(n1,d1,n2,d2);
}
else if(c=='Q'){
break;
}
else{
cout<<"Enter a valid operation\n";
continue;
}
cout<<"answer = "<<res.first<<" / "<<res.second<<"\n";
}
}catch(...){
cout<<"Program terminating due to exception";
return 0;
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • Must write in Java - ignore the Junit tests Write a program that works with fractions....

    Must write in Java - ignore the Junit tests Write a program that works with fractions. You are first to implement three methods, each to perform a different calculation on a pair of fractions: subtract, multiply, and divide. For each of these methods, you are supplied two fractions as arguments, each a two-element array (the numerator is at index 0, the denominator is at index 1), and you are to return a resulting, simplified fraction as a new two-element array...

  • Design a class named Fraction. This class is used to represent a ratio of two integers,...

    Design a class named Fraction. This class is used to represent a ratio of two integers, such as 6 / 9. Include accessors and mutators that allow the user to get and set the numerator and the denominator. Also include a member method that returns the value of the numerator divided by the denominator as double (for example, 0.666…). Include an additional member method that returns the value of the fraction reduced to lowest terms as string. For example, instead...

  • CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

    CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...

  • Please help I am confused to where start. It C++ program thank you The stoi function...

    Please help I am confused to where start. It C++ program thank you The stoi function converts a string to an integer, but does not check whether or not the value of the string is a good integer. If the stoi function fails, an exception is thrown and the program terminates. Write a program that asks the user to enter two strings, one called sNumerator and the other sDenominator. Using the stoi function, convert the two strings to numbers but...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • Lab 1.java only Goal: This lab will give you experience with defining and using classes and...

    Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...

  • C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

    C++ Question Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors. You should check to make sure that the denominator is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT