Part 02 (Fraction calculator) (40 points) Write a program that lets the user per
ID: 675541 • Letter: P
Question
Part 02 (Fraction calculator) (40 points) Write a program that lets the user perform arithmetic operations on fractions. Fractions ar e of the orm 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. Furthermore, your program must consist of at least the following functions: tion informs the user about the program's purpose, explains how to enter data, and allows the user to select the operation. 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. (Notice that this function has a total of six parameters.) 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. ( parameters.) 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. (Notice that this function has a total of six parameters.) 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. (Notice that this function has a total of six parameters.) b. c. Notice that this function has a total of six d. e. Some sample outputs are 3/4+2/5=23 / 20 2/3*3/5=6/15 Your answer need not be in the lowest terms. Operations on FractionsExplanation / Answer
#include <iostream>
using namespace std;
void add(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int n1 = (b1 * b2) / b1;
int n2 = (b1 * b2) / b2;
bottomResult = b1 * b2;
int newNum1 = n1 * top1;
int newNum2 = n2 * top2;
topResult = newNum1 + newNum2;
}
else
cout<<" Invalid input, denominator cannot be 0."<<endl;
}
void subtract(int top1, int b1, int top2, int b2, int& topResult, int& bottomResult)
{
if(b1 != 0 && b2 != 0)
{
int n1 = (b1 * b2) / b1;
int n2 = (b1 * b2) / b2;
bottomResult = b1 * b2;
int newNum1 = n1 * top1;
int newNum2 = n2 * top2;
topResult = newNum1 - newNum2;
}
else
cout<<" Invalid input, denominator cannot be 0."<<endl;
}
void multiply(int top1, int b1, int top2, int b2, int& topResult, int& bottomResult)
{
if(b1 != 0 && b2 != 0)
{
bottomResult = b1 * b2;
topResult = top1 * top2;
}
else
cout<<" Invalid input, denominator cannot be 0."<<endl;
}
void divide(int top1, int b1, int top2, int b2, int& topResult, int& bottomResult)
{
if(b1 != 0 && b2 != 0)
{
int newTop = b2;
int newBottom = top2;
topResult = top1 * newTop;
bottomResult = b1 * newBottom;
}
else
cout<<" Invalid input. "<<endl;
}
int main()
{
int choice, num1, num2, denom1, denom2, numResult = 0, denomResult = 0;
cout<<"Adding fractions: 1"<<endl;
cout<<"Subtracting fractions: 2"<<endl;
cout<<"Multiplying fractions: 3"<<endl;
cout<<"Dividing fractions: 4"<<endl;
cout<<" Enter fractions: "<<endl;
cin>>num1>>denom1>>num2>>denom2;
cout<<"Enter operation (number): "<<endl;
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
add(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 2:
subtract(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 3:
multiply(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 4:
divide(num1, denom1, num2, denom2, numResult, denomResult);
break;
default:
cout<<" Invalid input. "<<endl;
}
cout<<"Result = "<<numResult<<"/"<<denomResult<<endl;
return 0;
}