1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include <iostream>
using namespace std;
void menuFunction();
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&);
int main()
{
menuFunction();
return 0;
}
void menuFunction()
{
int choice, num1, num2, denom1, denom2, numResult = 0, denomResult = 0;
cout<<"This program lets you perform operations on fractions."<<endl;
cout<<"Input data as (a/b) and (c/d), then select an operation to perform."<<endl<<endl;
cout<<"Operation Number"<<endl;
cout<<"-------------------------------"<<endl;
cout<<"Adding fractions: 1"<<endl;
cout<<"Subtracting fractions: 2"<<endl;
cout<<"Multiplying fractions: 3"<<endl;
cout<<"Dividing fractions: 4"<<endl;
cout<<"-------------------------------"<<endl;
cout<<"\nEnter fractions: "<<endl;
cin>>num1>>denom1>>num2>>denom2;
cout<<"Enter operation (number): "<<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;
}
|