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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void addFractions(int,int,int,int,int&,int&);
void subtractFractions(int,int,int,int,int&,int&);
void multiplyFractions(int,int,int,int,int&,int&);
void divideFractions(int,int,int,int,int&,int&);
void menu();
bool getInputs(int& numerator1, int& denominator1, int& numerator2, int& denominator2, char& operation)
{
string strOperation = "";
char solidus1, solidus2;
switch(operation)
{
case '+':
strOperation = "add :";
break;
case '-':
strOperation = "subtract :";
break;
case '*':
strOperation = "multiply :";
break;
case '/':
strOperation = "divide :";
break;
default:
cout << "Invalid operation selected!";
return false;
}
cout << "Data is entered in the following format: \nnumerator/denominator \n" ;
cout << "Enter first fraction to " << strOperation << endl;
cin >> numerator1 >> solidus1 >> denominator1;
cout << "Enter the 2nd fraction to " << strOperation << endl;
cin >> numerator2 >> solidus2 >> denominator2;
if((denominator1 == 0) || (denominator2 == 0)) //tests for zero
{
cout << endl << "Error: denominator can not be zero!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return true;
}
else
{
return false;
}
}
int main()
{
menu();
return 0;
}
void menu()
{
bool zero_found = false; // Zero based fractions are NOT allowed.
bool valid = false; // Make sure user entered a valid operation.
char operation = ' '; // Holds the value of our math operator.
int numerator1, denominator1, numerator2, denominator2;
int numAnswer = 0;
int denAnswer = 0;
cout << setw(50) << "Welcome to my fraction calculator program:" << endl << endl;
cout << "Please select from the following functions:" << endl;
cout << "'+' To add fractions press + then enter" << endl;
cout << "'-' To subtract fractions press - then enter" << endl;
cout << "'*' To multiply fractions press * then enter" << endl;
cout << "'/' To divide fractions press / then enter" << endl;
cout << endl << ">>";
do{
cin >> operation;
if(operation == '+')
{
do{
zero_found = getInputs(numerator1,denominator1,numerator2,denominator2,operation);
if(zero_found != true)
{
addFractions(numerator1,denominator1,numerator2,denominator2, numAnswer, denAnswer); // perform calculations
cout << "The value of these fractions added are: " << numAnswer << "/" << denAnswer << endl;
valid = true;
}
}
while(zero_found == true);
}
else if(operation == '-')
{
do{
zero_found = getInputs(numerator1,denominator1,numerator2,denominator2,operation);
if(zero_found != true)
{
subtractFractions(numerator1,denominator1,numerator2,denominator2, numAnswer, denAnswer); // perform calculations
cout << "The value of these fractions subtracted are: " << numAnswer << "/" << denAnswer << endl;
valid = true;
}
}
while(zero_found == true);
}
else if(operation == '*')
{
do{
zero_found = getInputs(numerator1,denominator1,numerator2,denominator2,operation);
if(zero_found != true)
{
multiplyFractions(numerator1,denominator1,numerator2,denominator2, numAnswer, denAnswer); // perform calculations
cout << "The value of these fractions multiplied are: " << numAnswer << "/" << denAnswer << endl;
valid = true;
}
}
while(zero_found == true);
}
else if(operation == '/')
{
do{
zero_found = getInputs(numerator1,denominator1,numerator2,denominator2,operation);
if(zero_found != true)
{
divideFractions(numerator1,denominator1,numerator2,denominator2, numAnswer, denAnswer); // perform calculations
cout << "The value of these fractions divided are: " << numAnswer << "/" << denAnswer << endl;
valid = true;
}
}
while(zero_found == true);
}
else
{
cout << "Please select a valid operation!" << endl;
}
}
while(!valid);
}
void addFractions(int numerator1, int denominator1, int numerator2, int denominator2, int& numSum, int& denSum)
{
numSum = numerator1 * denominator2 + numerator2 * denominator1;
denSum = denominator1 * denominator2;
}
void subtractFractions(int numerator1, int denominator1, int numerator2, int denominator2, int& numSum, int& denSum)
{
numSum = numerator1 * denominator2 - numerator2 * denominator1;
denSum = denominator1 * denominator2;
}
void multiplyFractions(int numerator1, int denominator1, int numerator2, int denominator2, int& numSum, int& denSum)
{
numSum = numerator1 * numerator2;
denSum = denominator1 * denominator2;
}
void divideFractions(int numerator1, int denominator1, int numerator2, int denominator2, int& numSum, int& denSum)
{
numSum = numerator1 * denominator2;
denSum = denominator1 * numerator2;
}
|