In: Computer Science
(Fraction calculator c++) | 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. Furthermore, your program must consist of at least the following functions: 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. 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. (Notice that this function has a total of six 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.) 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.
C++ program (fraction calculator):
#include <iostream>
using namespace std;
struct fraction // structure for fraction
{
int num; // numerator
int denom; // denominator
};
int gcd(int a, int b) // gcd to calculate lcm
{
if(b == 0)
return a;
return gcd(b,a%b);
}
struct fraction add(struct fraction f1, struct fraction f2) //
adds two fractions
{
struct fraction f3;
f3.denom = (f1.denom/gcd(f1.denom,f2.denom)) * f2.denom; // lcm of
denominators
f3.num = f1.num*(f3.denom/f1.denom) +
f2.num*(f3.denom/f2.denom);
return f3;
}
struct fraction sub(struct fraction f1, struct fraction f2) //
subracts two fractions
{
struct fraction f3;
f3.denom = (f1.denom/gcd(f1.denom,f2.denom)) * f2.denom;
f3.num = f1.num*(f3.denom/f1.denom) -
f2.num*(f3.denom/f2.denom);
return f3;
}
struct fraction mul(struct fraction f1, struct fraction f2)
//multiplies two fractions
{
struct fraction f3;
f3.num = f1.num*f2.num;
f3.denom = f1.denom*f2.denom;
return f3;
}
struct fraction division(struct fraction f1, struct fraction f2)
// divides two fractions
{
struct fraction f3;
f3.num = f1.num*f2.denom;
f3.denom = f1.denom*f2.num;
return f3;
}
int main()
{
struct fraction f1,f2,f3;
int op;
cout<<"Enter fraction1 numerator and denominator :
";
cin>>f1.num>>f1.denom;
cout<<"Enter fraction2 numerator and denominator : ";
cin>>f2.num>>f2.denom;
cout<<"\n\nFraction Calculator Menu \n"; // Menu driven
program
cout<<"------------------------- \n";
cout<<"1. Addition\n";
cout<<"2. Subraction\n";
cout<<"3. Multiplication\n";
cout<<"4. Division\n\n";
cout<<"Choose a option 1/2/3/4 from menu : ";
cin>>op;
switch(op)
{
case 1:
f3 = add(f1,f2);
cout<<f1.num<<"/"<<f1.denom<<" +
"<<f2.num<<"/"<<f2.denom<<" =
"<<f3.num<<"/"<<f3.denom<<endl;
break;
case 2:
f3 = sub(f1,f2);
cout<<f1.num<<"/"<<f1.denom<<" -
"<<f2.num<<"/"<<f2.denom<<" =
"<<f3.num<<"/"<<f3.denom<<endl;
break;
case 3:
f3 = mul(f1,f2);
cout<<f1.num<<"/"<<f1.denom<<" *
"<<f2.num<<"/"<<f2.denom<<" =
"<<f3.num<<"/"<<f3.denom<<endl;
break;
case 4:
f3 = division(f1,f2);
cout<<f1.num<<"/"<<f1.denom<<" /
"<<f2.num<<"/"<<f2.denom<<" =
"<<f3.num<<"/"<<f3.denom<<endl;
break;
}
return 0;
}
Output :