Question

In: Computer Science

Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions.


Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a / b, in which a and b are integers and b != 0.

Your program must:

  • be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numerator and denominator of each fraction.

  • The input must be validated for correct operation (+, -, *, or /) and b != 0. If any of these cases are violated the program must display an error message and allow the user to continue to re-enter the data until correct.

  • Fractions should be able to be entered into the program as numerator / denominator.

    • Example: Enter a fraction: 1/4

    • The user should be able to enter the fraction in standard format

Furthermore, your program must consist of at least the following functions:

  1. Function menu: This function informs the user about the program’s purpose, explains how to enter data, and allows the user to select the operation. It should return the operation selected. You can do this by using a char variableto store '+', '-', '*', '/' or use an integer to specify the operator. You choose . . . but it must be clear what the user is expected to enter.

  2. Function addFractions: This function takes as input four integers representing the numerators and denominators of two fractions, adds the fractions, and returns the numerator and denominator of the result as a string

  3. Function subtractFractions: This function takes as input four integers representing the numerators and denominators of two fractions, subtracts the fractions, and returns the numerator and denominator of the result as a string

  4. Function multiplyFractions: This function takes as input four integers representing the numerators and denominators of two fractions, multiplies the fractions, and returns the numerators and denominators of the result as a string

  5. Function divideFractions: This function takes as input four integers representing the numerators and denominators of two fractions, divides the fractions, and returns the numerator and denominator of the result as a string

  6. Function GCD: This function takes as input two integers representing the denominators of two fractions and calculates and returns the greatest common divisor (the largest integer that is not zero that divides the two integers without a remainder

  7. Function reduce: This function takes as input 2 integers representing the numerator and denominator of a fraction and reduces the fraction to its lowest terms. This function will require the use of the GCD function. Return the numerator and denominator of the result as a string

Research the String Stream object in C++ to easily convert numeric values to String:

  • http://www.cplusplus.com/reference/sstream/stringstream/stringstream/

  • https://www.systutorials.com/131/convert-string-to-int-and-reverse/

Some sample outputs are:

  • 3 / 4 + 2 / 5 = 23 / 20

  • 2 / 3 * 3 / 5 = 2 / 5

If you are not certain of the formulas to perform the above operations then use the Internet to research.

Solutions

Expert Solution

// Header files
//Basic MATH Operation of Fractions Using C++
#include
#include
using namespace std;
void addFractions(int num1, int den1, int num2, int den2, int &calnum, int &calden);
void subFractions(int num1, int den1, int num2, int den2, int &calnum, int &calden);
void multiplyFractions(int num1, int den1, int num2, int den2, int &calnum, int &calden);
void divideFractions(int num1, int den1, int num2, int den2, int &calnum, int &calden);
int GCD(int calnum, int calden);
void reduce(int &calnum, int &calden);

//main function
int main()
{
int num1, num2, den1, den2;
int res, n, calnum, calden;
int ch; // variable declaration
cout<<"\n Basic MATH Operation of Fractions Using C++ ";
cout<<"\n -------------------------------------------------------------------";
cout<<"\n 1) Addition";
cout<<"\n 2) Subtraction";
cout<<"\n 3) Multiplication";
cout<<"\n 4) Division";
cout<<"\n 5) Greatest Common Divisor";
cout<<"\n 6) Quit";
cout<<"\n -------------------------------------------------------------------";
cout<<"\n Please select an operation: ";
cin>>ch;
switch(ch) //switch case
{
case 1:
addFractions(num1, den1, num2, den2, calnum, calden);
//cout<<"\n" < break;
case 2:
subFractions(num1, den1, num2, den2, calnum, calden);
break;
case 3:
multiplyFractions(num1, den1, num2, den2, calnum, calden);
break;
case 4:
divideFractions(num1, den1, num2, den2, calnum, calden);
break;
case 5:
cout<<"\n Program Ends";
break;
default:
cout<<"\n Enter Correct Input";
}
return 0;
}

void addFractions(int num1, int den1, int num2, int den2, int &calnum, int &calden)
{
cout<<"\n Addition";
cout<<"\n Enter Numerator1: ";
cin >>num1;
cout <<"\n Enter Denominator1: ";
cin >>den1;
cout<<"\n Enter Numerator2: ";
cin >>num2;
cout<<"\n Enter Denominator2: ";
cin >>den2;
  
if(den1 <= 0)
{
cout<<"\n Enter correct Value";
cout <<"\n Enter Denominator1: ";
cin >>den1;
}
if(den2 <= 0)
{
cout<<"\n'n Enter correct Value";
cout <<"\n Enter Denominator2: ";
cin >>den2;
}
  
calden = GCD(den1, den2);
calden = (den1 * den2) / calden;
calnum = (num1) * (calden/den1) + (num2) * (calden/den2);
reduce(calden, calnum);
cout< cout<<"\n" <   
  
}
void subFractions(int num1, int den1, int num2, int den2, int &calnum, int &calden)
{
cout<<"\n Subtraction";
cout<<"\n Enter Numerator1: ";
cin >>num1;
cout <<"\n Enter Denominator1: ";
cin >>den1;
cout<<"\n Enter Numerator2: ";
cin >>num2;
cout<<"\n Enter Denominator2: ";
cin >>den2;
  
if(den1 <= 0)
{
cout<<"\n Enter correct Value";
cout <<"\n Enter Denominator1: ";
cin >>den1;
}
if(den2 <= 0)
{
cout<<"\n'n Enter correct Value";
cout <<"\n Enter Denominator2: ";
cin >>den2;
}
calnum = (num1 * den2) - (num2 * den1);
calden = den1 * den2;
GCD(calnum, calden);
reduce(calnum, calden);
cout< cout<<"\n" < }

void multiplyFractions(int num1, int den1, int num2, int den2, int &calnum, int &calden)
{
cout<<"\n Multiplication";
cout<<"\n Enter Numerator1: ";
cin >>num1;
cout <<"\n Enter Denominator1: ";
cin >>den1;
cout<<"\n Enter Numerator2: ";
cin >>num2;
cout<<"\n Enter Denominator2: ";
cin >>den2;
if(den1 <= 0)
{
cout<<"\n Enter correct Value";
cout <<"\n Enter Denominator1: ";
cin >>den1;
}
if(den2 <= 0)
{
cout<<"\n'n Enter correct Value";
cout <<"\n Enter Denominator2: ";
cin >>den2;
}
calnum = num1 * num2;
calden = den1 * den2;
GCD(calnum, calden);
reduce(calnum, calden);
cout<<"\n" < }
void divideFractions(int num1, int den1, int num2, int den2, int &calnum, int &calden)
{
cout<<"\n Multiplication";
cout<<"\n Enter Numerator1: ";
cin >>num1;
cout <<"\n Enter Denominator1: ";
cin >>den1;
cout<<"\n Enter Numerator2: ";
cin >>num2;
cout<<"\n Enter Denominator2: ";
cin >>den2;
if(den1 <= 0)
{
cout<<"\n Enter correct Value";
cout <<"\n Enter Denominator1: ";
cin >>den1;
}
if(den2 <= 0)
{
cout<<"\n'n Enter correct Value";
cout <<"\n Enter Denominator2: ";
cin >>den2;
}
calnum = num1 * den2;
calden = num2 * den2;
GCD(calnum, calden);
reduce(calnum, calden);
cout<<"\n (" < }
int GCD(int x, int y)
{
  
if(x == 0)
return y;
return GCD(y % x, x);
  
  
}
void reduce(int &calnum, int &calden)
{
int gcd = GCD(calnum, calden);
calden = calden/gcd;
calnum = calnum/gcd;
}

OUTPUT


Basic MATH Operation of Fractions Using C++
-------------------------------------------------------------------
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Greatest Common Divisor
6) Quit
-------------------------------------------------------------------
Please select an operation: 4
Multiplication
Enter Numerator1: 2
Enter Denominator1: 3
Enter Numerator2: 3
Enter Denominator2: 5
(2/3) / (3/5) = 2 / 3


Basic MATH Operation of Fractions Using C++
-------------------------------------------------------------------
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Greatest Common Divisor
6) Quit
-------------------------------------------------------------------
Please select an operation: 3
Multiplication
Enter Numerator1: 2
Enter Denominator1: 3
Enter Numerator2: 3
Enter Denominator2: 5
2 / 3 * 3 / 5 = 2 / 5


Basic MATH Operation of Fractions Using C++
-------------------------------------------------------------------
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Greatest Common Divisor
6) Quit
-------------------------------------------------------------------
Please select an operation: 1
Addition
Enter Numerator1: 3
Enter Denominator1: 4
Enter Numerator2: 2
Enter Denominator2: 5

3/4 + 2/5 = 23/ 20


Basic MATH Operation of Fractions Using C++
-------------------------------------------------------------------
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Greatest Common Divisor
6) Quit
-------------------------------------------------------------------
Please select an operation: 5
Program Ends


Basic MATH Operation of Fractions Using C++
-------------------------------------------------------------------
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Greatest Common Divisor
6) Quit
-------------------------------------------------------------------
Please select an operation: 6
Enter Correct Input


Related Solutions

Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
ASAP (Convert decimals to fractions) Write a program that prompts the user to enter a decimal...
ASAP (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in Listing 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19Test.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is 13/4...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is...
Write an assembly program that lets the user to input only the word MASM and displays...
Write an assembly program that lets the user to input only the word MASM and displays invalid input for any other user inputs.
C++ Write a program that lets the user enter a two letters which are f and...
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program. Example: Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times. Output2: The second most repeating...
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
C++ Please For this assignment, you will write a program that lets the user play against...
C++ Please For this assignment, you will write a program that lets the user play against the computer in a variation of the popular blackjack car game. In this variation of the game, two-six sided dice are used instead of cards. The dice are rolled, and the player tries to beat the computer's hidden total without going over 21. Here are some suggestions for the game's design: Each round of the game is performed as an iteration of a loop...
Write a program that lets the user enter the loan amount, number of years, and interest...
Write a program that lets the user enter the loan amount, number of years, and interest rate and displays the amortization schedule for the loan. Steps: 1) Create scanner 2) Prompt the user to enter loan amount and declare double variable and relate to scanner input 3) Prompt the user to enter number of years and declare integer years and relate to scanner input 4) Prompt the user to enter annual interest rate and declare double variable and relate to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT