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
|
#include <iostream>
using namespace std;
void functionMenu(); // displays main menu
void getFractions(int& numOne, int& denomOne, int& numTwo, int& denomTwo); // gets fraction inputs from user
void addFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns); // adds fractions
void subtractFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns); // subtracts fractions
void multiplyFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns); // multiplies fractions
void divideFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns); // divides fractions
void reduceFractions (int& numAns, int& denomAns); // reduces fractions
void displayResult(int choice, int numOne, int denomOne, int numTwo, int denomTwo, int numAns, int denomAns); // displays results of each task
int main()
{
//Declarations
int choice; // menu choice
//variables for fractions entered by user
int numOne;
int denomOne;
int numTwo;
int denomTwo;
//variables for answers to fraction calculations
int numAns = 0;
int denomAns = 0;
do
{
system("cls");
functionMenu();
cin >> choice;
cin.clear();
cout << endl << endl;
switch (choice)
{
case 1: // adds two fractions
system("cls");
getFractions(numOne, denomOne, numTwo, denomTwo);
addFractions(numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
displayResult(choice, numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
system("pause");
break;
case 2: // subtracts two fractions
system("cls");
getFractions(numOne, denomOne, numTwo, denomTwo);
subtractFractions(numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
displayResult(choice, numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
system("pause");
break;
case 3: // multiplies two fractions
system("cls");
getFractions(numOne, denomOne, numTwo, denomTwo);
multiplyFractions(numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
displayResult(choice, numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
system("pause");
break;
case 4: // divides two fractions
system("cls");
getFractions(numOne, denomOne, numTwo, denomTwo);
divideFractions(numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
displayResult(choice, numOne, denomOne, numTwo, denomTwo, numAns, denomAns);
system("pause");
break;
case 5: // exits program
return 1;
default: // the default result that is displayed if a user enters a number that does not match any of the cases
cout << "Invalid input, please press any key to try again." << endl << endl;
system("pause");
break;
}
}
while (choice != 5);
system("pause");
return 0;
}
//user definied function to show the menu screen
void functionMenu()
{
cout << "This program allows users to perform arithmetic operations on fractions." << endl
<< "Fractions are in the form of a/b, and a and b must be integers." << endl
<< "b should not equal 0." << endl << endl
<< "Enter the menu number of the operation you would like to perform." << endl << endl
<< "Menu: " << endl
<< "\t1.\tAddition (two fractions)" << endl
<< "\t2.\tSubtraction (two fractions)" << endl
<< "\t3.\tMultiply (two fractions)" << endl
<< "\t4.\tDivide (two fractions)" << endl
<< "\t5.\tExit" << endl << endl
<< "Your choice: ";
}
//user defined function to get the fractions
void getFractions(int& numOne, int& denomOne, int& numTwo, int& denomTwo)
{
int d; // dummy variable for '/' in fraction entered by user.
cout << "Fractions should be entered to include the '/'." << endl << endl
<< "Enter the first fraction: ";
cin >> numOne >> d >> denomOne;
cin.clear();
cout << "Enter the second fraction: ";
cin >> numTwo >> d >> denomTwo;
cin.clear();
cout << endl;
}
//user defined function to add two fractions
void addFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns)
{
numAns = (numOne * denomTwo) + (denomOne * numTwo);
denomAns = denomOne * denomTwo;
reduceFractions(numAns, denomAns);
}
//user defined function to subtract two fractions
void subtractFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns)
{
numAns = (numOne * denomTwo) - (denomOne * numTwo);
denomAns = denomOne * denomTwo;
reduceFractions(numAns, denomAns);
}
//user defined function to multiply two fractions
void multiplyFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns)
{
numAns = numOne * numTwo;
denomAns = denomOne * denomTwo;
reduceFractions(numAns, denomAns);
}
//user defined function to divide two fractions
void divideFractions(int numOne, int denomOne, int numTwo, int denomTwo, int& numAns, int& denomAns)
{
numAns = numOne * denomTwo;
denomAns = denomOne * numTwo;
reduceFractions(numAns, denomAns);
}
//user defined function to reduce fractions. this was not included in the homework, but i wanted to add it to
// clean up the fractions.
void reduceFractions (int& numAns, int& denomAns)
{
for (int i = denomAns * numAns; i > 1; i--)
if ((denomAns % i == 0) && (numAns % i == 0))
{
denomAns /= i;
numAns /= i;
}
}
//user defined function to display results based on the calculation chosen by user
void displayResult(int choice, int numOne, int denomOne, int numTwo, int denomTwo, int numAns, int denomAns)
{
switch (choice)
{
case 1: // display results if addition
cout << numOne << "/" << denomOne << " + " << numTwo << "/" << denomTwo
<< " = " << numAns << "/" << denomAns << endl << endl;
break;
case 2: // display results if subtractions
cout << numOne << "/" << denomOne << " - " << numTwo << "/" << denomTwo
<< " = " << numAns << "/" << denomAns << endl << endl;
break;
case 3: // dipslay results if multiplication
cout << numOne << "/" << denomOne << " * " << numTwo << "/" << denomTwo
<< " = " << numAns << "/" << denomAns << endl << endl;
break;
case 4: // dipsplay results if division
cout << numOne << "/" << denomOne << " / " << numTwo << "/" << denomTwo
<< " = " << numAns << "/" << denomAns << endl << endl;
break;
}
}
|