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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/*
Fraction Calculator
*/
#include <iostream>
using namespace std;
// Declare a fraction (defines an implicit typedef)
struct fraction
{
int num; // Declare Numerator Variable
int den; // Declare Denominator Variable
};
void functadd(fraction a, fraction b);
void functsubtr(fraction a, fraction b);
void functmulti(fraction a, fraction b);
void functdivide(fraction a, fraction b);
int main()
{
int ans;
fraction number[2];
char c; // Declare c, a dummy character to "eat"
// the slashes in the input fractions
char oprtr; // Declare oprtr, the operator in the
// input expression
fraction result; // Declare result, a fraction to store the
// result of the computation
fraction reduced; // Declare reduced, a fraction to store the
// reduced result of the computation
bool n = true; // Declare n, a boolean determining whether
// the program continues, defaults to true
bool showans; // Declare showans, a boolean determining
// whether the answer is shown
char cont;
do
{
showans = true;
// Prompt user for input fractions
cout << "\nEnter Values in this form 2/3+94/3: ";
cin >> number[0].num >> c >> number[0].den >> // 1st fraction
oprtr >> // Operator
number[1].num >> c >> number[1].den; // 2nd fraction
switch(oprtr)
{
case '+':
// User entered a addition sign, add the fractions
return ans;
break;
case '-':
// User entered a subtraction sign, subtract the fractions
return ans;
break;
case '*':
// User entered a multiplication sign, multiply the fractions
return ans;
break;
case '/':
// User entered a division sign, divide the fractions
return ans;
break;
default:
// User entered something else, show an error message and don't
// display the answer
cout << "You have used an invalid operator.\n";
cout << "Please use any one of following operators: +, -, *, /\n";
showans = false;
break;
}
if(showans)
{
// Display the un-reduced result
cout << "Answer = " << result.num << "/" << result.den;
// User will be stuck in this loop until they enter either: y,Y,n,N
do{
// Ask the user about whether to continue / repeat
cout << "\n";
cout << "Continue? y/n \n";
// Accept user input
cin >> cont;
} while((cont != 'y')&&(cont != 'Y')&&(cont != 'n')&&(cont != 'N'));
// If the user entered n, break out of this loop.
// (If the user entered y, don't do anything, n = true).
if((cont == 'n') || (cont == 'N'))
{
n = false;
}
}
} while(n);
// Return 0 for a successful exit
return 0;
}
void functadd(fraction a,fraction b);
{
// Declare ans, the fraction resulting from the operation
fraction ans;
// Calculate the numerator and denominator of the resulting fraction
ans.num = (a.num * b.den) + (b.num * a.den);
ans.den = a.den * b.den;
// Return the resulting fraction
result = functadd(number[0], number[1]);
return result;
}
void functsubtr(fraction a, fraction b);
{
// Declare ans, the fraction resulting from the operation
fraction ans;
// Calculate the numerator and denominator of the resulting fraction
ans.num = (a.num * b.den) - (b.num * a.den);
ans.den = a.den * b.den;
// Return the resulting fraction
result = functsubtr(number[0], number[1]);
return ans;
}
void functmulti(fraction a, fraction b);
{
// Declare ans, the fraction resulting from the operation
fraction ans;
// Calculate the numerator and denominator of the resulting fraction
ans.num = a.num * b.num;
ans.den = a.den * b.den;
// Return the resulting fraction
result = functmulti(number[0], number[1]);
;
}
void functdivide(fraction a, fraction b);
{
// Declare ans, the fraction resulting from the operation
fraction ans;
// Calculate the numerator and denominator of the resulting fraction
ans.num = a.num * b.den;
ans.den = a.den * b.num;
// Return the resulting fraction
result = functdivide(number[0], number[1])
return ans;
}
|