In: Computer Science
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:
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.
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
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
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
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
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
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.
// 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" <
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<
}
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<
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