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
|
#include <iostream>
enum menuSelectionEnum {
MENU_NONE = 0,
MENU_ADD = 1,
MENU_SUBTRACT = 2,
MENU_MULTIPLY = 3,
MENU_DIVIDE = 4,
MENU_QUIT = 5,
MENU_END = 6
};
int menuItemSelection = 0;
int menu();
void inputFractionValuesFromUser(int &num1, int &num2, int &den1, int &den2);
void addFractions(int num1, int num2, int den1, int den2, int &resultNum1, int &resultDen);
void subtractFractions(int num1, int num2, int de1, int den2, int &resultNum, int &resultDen);
void multiplyFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen);
void divideFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen);
void outputResults(int num1, int num2, int den1, int den2, int &resultNum,
int &resultDen, int operation);
int main()
{
int menuSelect = MENU_NONE;
int num1 = 0, num2 = 0,den1 = 0, den2 = 0;
int resultNum = 0, resultDen = 0;
// loop until menu selection is "Quit" (Menu loop)
do {
menuSelect = menu();
if (menuSelect == MENU_QUIT) { break; }
inputFractionValuesFromUser(num1, num2, den1, den2);
if (menuSelect == MENU_ADD) {
addFractions( num1, num2, den1, den2, resultNum, resultDen);
}
if (menuSelect == MENU_SUBTRACT) {
subtractFractions(num1, num2, den1, den2, resultNum, resultDen);
}
if (menuSelect == MENU_MULTIPLY) {
multiplyFractions(num1, num2, den1, den2, resultNum, resultDen);
}
if (menuSelect == MENU_DIVIDE) {
divideFractions(num1, num2, den1, den2, resultNum, resultDen);
}
outputResults(num1, num2, den1, den2, resultNum, resultDen, menuSelect);
}
while(menuSelect != MENU_QUIT);
system("pause"); // http://www.cplusplus.com/forum/beginner/1988/
}
int menu ()
{
int menuItemSelection = 0;
do {
std::cout << " Fraction Calculator Program\n"
" 1) Add Fractions\n"
" 2) Subtract Fractions\n"
" 3) Multiply Fractions\n"
" 4) Divide Fractions\n"
" 5) Quit\n"
" Please make a selection: ";
std::cin >> menuItemSelection;
if(menuItemSelection <= MENU_NONE || menuItemSelection >= MENU_END) {
std::cout << " Invalid menu selection. Please Try again.\n";
}
} while (menuItemSelection <= MENU_NONE || menuItemSelection >= MENU_END);
return menuItemSelection;
}
void inputFractionValuesFromUser(int &num1, int &num2, int &den1, int &den2)
{
std::cout << "\nPlease enter the first numerator fraction: ";
std::cin >> num1;
std::cout << "\nPlease enter the denominator for the first fraction: ";
std::cin >> den1;
std::cout << "\nPlease enter the second numerator fraction: ";
std::cin >> num2;
std::cout << "\nPlease enter the second denominator fraction: ";
std::cin >> den2;
std::cout << '\n';
}
void addFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen)
{
resultNum = (num1 * den2) + (num2 * den1);
resultDen = den1 * den2;
}
void subtractFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen)
{
resultNum = (num1 * den1) - (num2 * den2);
resultDen = den1 * den2;
}
void multiplyFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen)
{
resultNum = num1 * num2;
resultDen = den1 * den2;
}
void divideFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen)
{
resultNum = num1 * den2;
resultDen = den1 * num1;
}
void outputResults(int num1, int num2, int den1, int den2, int &resultNum,
int &resultDen, int operation)
{
if (operation == MENU_ADD) {
std::cout << num1 << '/' << den1 << '+' << den2 << '='
<< resultNum << '/' << resultDen << '\n';
}
if (operation == MENU_SUBTRACT) {
std::cout << num1 << '/' << den1 << '-' << num2 << '/' << den2 << '='
<< resultNum << '/' << resultDen << '\n';
}
if (operation == MENU_MULTIPLY) {
std::cout << num1 << '/' << den1 << '*' << num2 << '/' << '='
<< resultNum << '/' << resultDen << '\n';
}
if (operation == MENU_DIVIDE) {
std::cout << num1 << '/' << den1 << '/' <<num2 << '/' << den2 << '='
<< resultNum << '/' << resultDen << '\n';
}
}
|