Dec 2, 2016 at 1:48am UTC
hey guys, i figured out how to do this problem that my professor wanted but he put a twist on it that i cant figure out how to solve. he wants the final answer to display a decimal number and not a fraction. this is what i have, any help would be great, thanks.
#include <iostream>
using namespace std;
void addFraction(int, int, int, int, int&, int&);
void subtractFraction(int, int, int, int, int&, int&);
void multiplyFraction(int, int, int, int, int&, int&);
void divideFraction(int, int, int, int, int&, int&);
void startFunction();
int main()
{
startFunction();
return 0;
}
void startFunction()
{
int choice;
int num1, num2, denom1, denom2, numResult = 0, denomResult = 0;
cout<<"Enter the first Numerator: ";
cin>>num1;
cout<<"Enter the first Denominator: ";
cin>>denom1;
cout<<"Enter the second Numerator: ";
cin>>num2;
cout<<"Enter the second denominator: ";
cin>>denom2;
cout<<"Enter operation number (1= Add, 2= Subtract, 3= Multiply, 4= Divide): "<<endl;
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
addFraction(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 2:
subtractFraction(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 3:
multiplyFraction(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 4:
divideFraction(num1, denom1, num2, denom2, numResult, denomResult);
break;
default:
cout<<"\nInvalid input.\n"<<endl;
}
cout<<"Result = "<<numResult<<"/"<<denomResult<<endl;
}
void addFraction(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int numerator1 = (bottom1 * bottom2) / bottom1;
int numerator2 = (bottom1 * bottom2) / bottom2;
bottomResult = bottom1 * bottom2;
int newNum1 = numerator1 * top1;
int newNum2 = numerator2 * top2;
topResult = newNum1 + newNum2;
}
else
cout<<"\nInvalid input, denominator cannot be 0."<<endl;
}
void subtractFraction(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int numerator1 = (bottom1 * bottom2) / bottom1;
int numerator2 = (bottom1 * bottom2) / bottom2;
bottomResult = bottom1 * bottom2;
int newNum1 = numerator1 * top1;
int newNum2 = numerator2 * top2;
topResult = newNum1 - newNum2;
}
else
cout<<"\nInvalid input, denominator cannot be 0."<<endl;
}
void multiplyFraction(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
bottomResult = bottom1 * bottom2;
topResult = top1 * top2;
}
else
cout<<"\nInvalid input, denominator cannot be 0."<<endl;
}
void divideFraction(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int newTop = bottom2;
int newBottom = top2;
topResult = top1 * newTop;
bottomResult = bottom1 * newBottom;
}
else
cout<<"\nInvalid input.\n"<<endl;
}