Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your program should be able to add, subtract, multiply, and divide two fractions

ID: 3547734 • Letter: Y

Question

Your program should be able to add, subtract, multiply, and divide two fractions.  Specifically, your program must request two fractions from the user by getting the numerator and denominator separately for each fraction.  It must also get the operation to perform (add, subtract, multiply, or divide) as a single character.  That is, the user should enter '+', '-', '*', or '/' for the operation.  Be sure to validate the input for the operation.  Your program will then compute the resulting fraction, keeping the numerator and denominator separate, and output the result.  The resulting fraction must be simplified.  For example, (4 / 6) should be displayed as (2 / 3), and (8 / 8) should be displayed as (1 / 1).  

You will need to use the GCD function to simplify the answer.
You must also write a separate function for each fraction operation.  That is, you must write a function that adds two fractions, another that subtracts two fractions, a third that multiplies two fractions, and a fourth that divides two fractions.  All input and output should happen in main().  These functions are only for handling fractional math.  Note that each of these functions should have six arguments: two for the first fraction, two for the second fraction, and two for the answer.  The resulting fraction must therefore be passed into and out of the function using call by reference arguments.

Explanation / Answer

#include<iostream>
using namespace std;
void add_fractions(int num1,int den1,int num2,int den2,int& res1,int& res2)
{
res1 = num1*den2+den1*num2;
res2 = den1*den2;
int max = res1>res2?res1:res2;
int gcd = 1;
for(int i=max; i>0; i--)
{
if(res1%i==0 && res2%i==0) { gcd = i; break; }
}
res1 = res1/gcd;
res2 = res2/gcd;
}
void subtract_fractions(int num1,int den1,int num2,int den2,int& res1,int& res2)
{
res1 = num1*den2-den1*num2;
res2 = den1*den2;
int max = res1>res2?res1:res2;
int gcd = 1;
for(int i=max; i>0; i--)
{
if(res1%i==0 && res2%i==0) { gcd = i; break; }
}
res1 = res1/gcd;
res2 = res2/gcd;
}
void multiply_fractions(int num1,int den1,int num2,int den2,int& res1,int& res2)
{
res1 = num1*num2;
res2 = den1*den2;
int max = res1>res2?res1:res2;
int gcd = 1;
for(int i=max; i>0; i--)
{
if(res1%i==0 && res2%i==0) { gcd = i; break; }
}
res1 = res1/gcd;
res2 = res2/gcd;
}
void divide_fractions(int num1,int den1,int num2,int den2,int& res1,int& res2)
{
res1 = num1*den2;
res2 = den1*num2;
int max = res1>res2?res1:res2;
int gcd = 1;
for(int i=max; i>0; i--)
{
if(res1%i==0 && res2%i==0) { gcd = i; break; }
}
res1 = res1/gcd;
res2 = res2/gcd;
}
int main()
{
int num1,den1,num2,den2;
int res1,res2;
char op;
cout <<"Enter numerator and denominator of fraction 1 :";
cin >> num1 >> den1;
cout << endl;
cout <<"Enter numerator and denominator of fraction 2 :";
cin >> num2 >> den2;
cout << endl;
cout <<"Enter operation to perform (+ for addition,- for subtraction, * for multiplication,/ for division ";
cin >> op;
cout << endl;
while(op!='+' && op!='-' && op!='*' && op!='/')
{
cout <<"Invalid operator entered. plesae re-enter :";
cin >> op;
cout << endl;
}
switch(op)
{
case '+': add_fractions(num1,den1,num2,den2,res1,res2); break;
case '-': subtract_fractions(num1,den1,num2,den2,res1,res2); break;
case '*': multiply_fractions(num1,den1,num2,den2,res1,res2); break;
case '/': divide_fractions(num1,den1,num2,den2,res1,res2); break;
}
cout << num1 << " / " << den1 << " "<< op << " " << num2 <<" / " << den2 << " = " << res1 << "/" << res2 << endl;
return 0;
}