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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include<iostream>
#include<string>
using namespace std;
char menu();
void addFractions( int a1, int b1, int a2, int b2, int& num, int& den );
void subtractFractions( int a1, int b1, int a2, int b2, int& num, int& den );
void multiplyFractions( int a1, int b1, int a2, int b2, int& num, int& den );
void divideFractions( int a1, int b1, int a2, int b2, int& num, int& den );
int main()
{
int a1, b1, a2, b2, num, den;
char ch = menu();
try
{
cout << "\n\tEnter the numerator of the fraction 1: ";
cin >> a1;
cout << "\tEnter the denominator of fraction 1: ";
cin >> b1;
if ( b1 == 0 )
throw b1;
else if( b1 < 0 )
{
string s = "Negative denominator exception";
throw s;
}
cout << "\n\tEnter the numerator of fraction 2: ";
cin >> a2;
cout << "\tEnter the denominator of fraction 2: ";
cin >> b2;
if( b2 == 0 )
throw b2;
else if( b2 < 0 )
{
string s = "Negative denominator exception";
throw s;
}
switch( ch )
{
case '+':
addFractions( a1, b1, a2, b2, num, den );
cout << "\n\tThe sum of the fractions is" << num << "/" << den;
break;
case '-':
subtractFractions( a1, b1, a2, b2, num, den );
cout << "\n\tThe difference of the fraction is " << num << "/" << den;
case '*':
multiplyFractions( a1, b1, a2, b2, num, den );
cout << "\n\tThe product of the fractions is " << num << "/" << den;
case '/':
if( a2 != 0 )
{
divideFractions( a1, b1, a2, b2, num, den );
cout << "\n\tDivision of the fractions is " << num << "/" << den;
}
else
cout << "\n\tDenominator fraction cannot be zero.";
break;
}
}
catch( int x )
{
cout << "Denominator is " << x << "exception" << endl;
}
catch( string s )
{
cout << s;
}
cout << endl;
system( "pause" );
return 0;
}
char menu()
{
char operationType;
cout << "\n\n\tProgram that lets the user perform " << endl;
cout << "arithmetic operations on fractions." << endl;
cout << "\n\n\t\t M E N U";
cout << "\n\nAddition :+";
cout << "\n\tSubtraction :-";
cout << "\n\tMultipication :*";
cout << "\n\tDivision :/";
cout << "\n\n\tEnter operation to perform: ";
cin >> operationType;
return operationType;
}
void addFractions( int a1, int b1, int a2, int b2, int& num, int& den )
{
num = ( a1 * b2 ) + ( b1 * a2 );
den = b1 * b2;
}
void subtractFractions( int a1, int b1, int a2, int b2, int& num, int& den )
{
num = ( a1 * b2 ) - ( b1 * b2 );
den = b1 * b2;
}
void multiplyFractions( int a1, int b1, int a2, int b2, int& num, int& den )
{
num = a1 * a2;
den = b1 * a2;
}
|