The solution to this programming excercise must demonstrate the use of an enumer
ID: 3544254 • Letter: T
Question
The solution to this programming excercise must demonstrate the use of an enumeration type for each of the arithmetic operations.
(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 6¼ 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:
a. Function menu: This function informs the user about the programs
purpose, explains how to enter data, and allows the user to select the
operation.
b. 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.)
c. 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.)
d. 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.)
e. 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.
The solution to this programming excercise must demonstrate the use of an enumeration type for each of the arithmetic operations. I know this exact question has been answered several times but it doesn't demonstrate the enum for the operations.
Explanation / Answer
#include<iostream>
using namespace std;
enum operation{plus_op=1, minus_op, multiply_op, divide_op};
int menu()
{
cout<<"Welcome to Fractional Calculator " << endl;
cout <<"Please select one of the operators " << endl;
cout <<"1. Addition of Fractions " << endl;
cout <<"2. Subtraction of Fractions " << endl;
cout <<"3. Multiplication of Fractions " << endl;
cout <<"4. Division of Fractions " << endl;
cout << "Select A choice :";
int n;
cin >> n;
return n;
}
void addFractions(int n1,int d1,int n2,int d2,int& n3,int& d3)
{
n3 = n1*d2+d1*n2;
d3 = d1*d2;
}
void subtractFractions(int n1,int d1,int n2,int d2,int& n3,int& d3)
{
n3 = n1*d2-d1*n2;
d3 = d1*d2;
}
void multiplyFractions(int n1,int d1,int n2,int d2,int& n3,int& d3)
{
n3 = n1*n2;
d3 = d1*d2;
}
void divideFractions(int n1,int d1,int n2,int d2,int& n3,int& d3)
{
n3 = n1*d2;
d3 = d1*n2;
}
void getdata(int& n1,int& d1,int& n2,int& d2)
{
cout <<"Enter Fraction 1 Numerator :";
cin >> n1;
cout << endl;
cout <<"Enter Fraction 1 Denominator :";
cin >> d1;
cout << endl;
cout <<"Enter Fraction 2 Numerator :";
cin >> n2;
cout << endl;
cout <<"Enter Fraction 2 Denominator :";
cin >> d2;
cout << endl;
}
int main()
{
int n1,d1,n2,d2,n3,d3;
operation op = operation(menu());
switch(op)
{
case plus_op:
{
getdata(n1,d1,n2,d2);
addFractions(n1,d1,n2,d2,n3,d3);
cout << n1 << " / " << d1<< " + " << n2 << " / " << d2<< " = " << n3 << " / " << d3 << endl;
}
break;
case minus_op:
{
getdata(n1,d1,n2,d2);
subtractFractions(n1,d1,n2,d2,n3,d3);
cout << n1 << " / " << d1<< " - " << n2 << " / " << d2<< " = " << n3 << " / " << d3 << endl;
}
break;
case multiply_op:
{
getdata(n1,d1,n2,d2);
multiplyFractions(n1,d1,n2,d2,n3,d3);
cout << n1 << " / " << d1<< " * " << n2 << " / " << d2<< " = " << n3 << " / " << d3 << endl;
}
break;
case divide_op:
{
getdata(n1,d1,n2,d2);
divideFractions(n1,d1,n2,d2,n3,d3);
cout << n1 << " / " << d1<< " / " << n2 << " / " << d2<< " = " << n3 << " / " << d3 << endl;
}
break;
default: cout << "Unknown Operation " << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.