[Solved] Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, where a and b are integers
Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, where a and b are integers and b is not equal to 0. Your program must be menu driven, allowing the user to select the operation (+, – *, or /) and input the numerator and denominator of each fraction. Furthermore, your program must run until the user quits and must consist of at least the following functions:
menu: This function informs the user about the program’s purpose, explains how to enter data, how to quit and allows the user to select the operation.
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.
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.
multiplyFractions: This function takes as input four integers representing the numerators and denominators of two fractions, multiplies the fractions, and returns the numerator and denominator of the result.
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.
Here are some sample outputs of the program:
3 / 4 +2 / 5 = 23 / 20
2 / 3 * 3 / 5 = 2 / 5
The answer needs to be in the lowest terms. Your program when executed must:
<!–[if !supportLists]–> i. <!–[endif]–>display a menu to the user that instructs them how to run input data and how to terminate the program.
<!–[if !supportLists]–> ii. <!–[endif]–>run until the user quits.
<!–[if !supportLists]–> iii. <!–[endif]–>correctly add,subtract, multiply and divide fractions and return the resulting fraction in its lowest terms.
My codes work well , please just make the result in the lowest term and run until the user quits
#include
using namespace std;
void menue(char&,int&,int&, int&, int&);
void addFraction(int,int, int, int, int&, int&);
void subtractFractions(char,int,int, int, int, int&, int&);
void multiplyFractions(char,int,int, int, int, int&, int&);
void divideFractions(char,int,int, int, int, int&, int&);
int gethcf(int,int);
bool wishContinue(int&);
int main()
{
int n1=0;
int n2=0;
int n3=0;
int d1=0;
int d2=0;
int d3=0;
int result=0;
char operation;
char slash=’/’;
menue(operation,n1,n2, d1, d2);
if(operation == ‘+’)
addFraction(n1,n2, d1, d2, n3, d3);
else if(operation == ‘-‘)
subtractFractions(operation,n1,n2, d1, d2, n3, d3);
else if(operation == ‘*’)
multiplyFractions(operation,n1,n2, d1, d2, n3, d3);
else if(operation == ‘/’)
divideFractions(operation,n1,n2, d1, d2, n3, d3);
cout << n3 << slash << d3 << endl;
return 0;
}
void menue(char& operation,int& n1,int& n2,int& d1,int&d2)
{
cout << “This programm calacualte the fraction” << endl;
cout << “select one of the following operation,(+ ,- ,*,/)” << endl;
cin >> operation;
while (operation != ‘+’ && operation!= ‘/’ && operation != ‘*’ && operation != ‘-‘)
{
cout << “You enter the wrong operator , select one of the following operation,(+ ,- ,*,/)” <
cin >> operation;
}
cout << “Enter first num: ” << endl;
cin >> n1;
cout << “Enter first dem: ” << endl;
cin >> d1;
cout << “Enter second num: ” << endl;
cin >> n2;
cout << “Enter second dem: ” << endl;
cin >> d2;
return;
}
void addFraction(int n1,int n2,int d1,int d2,int& n3,int& d3)
{
cout << n1 << ” ” << d1 << ” ” << n2 << ” ” << d2 << endl;
n3=(d1*n2)+(d2*n1);
d3=(d1*d2);
cout << “Answer: ” << n3 <<“/” << d3<
return;
}
void subtractFractions(char operation,int n1,int n2,int d1,int d2,int& n3,int& d3)
{
n3=(d2*n1)-(d1*n2);
d3=(d1*d2);
return;
}
void multiplyFractions(char operation,int n1,int n2,int d1,int d2,int& n3,int& d3)
{
n3=(n2*n1);
d3=(d1*d2);
return;
}
void divideFractions(char operation,int n1,int n2,int d1,int d2,int& n3,int& d3)
{
n3=(d2*n1);
d3=(d1*n2);
return;
}
Expert Answer
OR