Code:
#include <iostream>
#include <string>
using namespace std;
// End pre-processor directives...
/*
Program: Team Project
Author: CHRIS TERRY
Date written: 2/20/2008
This of each fractal equation like this:
(numeratorOne / denominatorOne) + (numeratorTwo / denominatorTwo)
This is of course assuming that you want to add them.
Assumptions/Checks:
Denominators are NOT 0
Menu choices can only be 0 - 5
The user only wants to do an equation regarding two(2) fractions
There are NO command args
Debug mode is choice 5 on the menu, it will restart the menu
If you got this, please use it appropriately.
*/
bool DEBUG = false;
// Global debug variable...
void menu(int&);
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 getNumbers(int&, int&, int&, int&, char);
int returnProduct(int, int);
// End method prototyping...
int main() {
int finalNumerator, finalDenominator;
// The finals...
int numeratorOne, numeratorTwo, denominatorOne, denominatorTwo;
// The numbers the will enter...
int menuChoice = 0;
// The menu item they enter...
char symbol;
// Which symbol for the equation...
menu(menuChoice);
// Get which method they want to do...
switch(menuChoice) {
// Determines which symbol based on which operation they want to perform...
case 1: symbol = '+'; break;
case 2: symbol = '-'; break;
case 3: symbol = 'x'; break;
case 4: symbol = '/'; break;
}
// End switch-case...
getNumbers(numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, symbol);
// Get their numbers...
if(DEBUG) cout << endl << numeratorOne << denominatorOne << numeratorTwo << denominatorTwo << endl;
// Debug code...
switch(menuChoice) {
// Switch to determine witch method to call...
case 1: {
// The addition case...
addFractions( numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, finalNumerator, finalDenominator);
// Call the method...
break;
}
case 2: {
// The subtraction case...
subtractFractions ( numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, finalNumerator, finalDenominator );
// Call the method...
break;
}
case 3: {
// The multiplication case...
multiplyFractions ( numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, finalNumerator, finalDenominator );
// Call the method...
break;
}
case 4: {
// The division case...
divideFractions ( numeratorOne, numeratorTwo, denominatorOne, denominatorTwo, finalNumerator, finalDenominator );
//Call the method...
break;
}
}
// End switch-case...
cout << endl << "Your problem was: ( " << numeratorOne << " / " << denominatorOne
<< " ) " << symbol << " ( " << numeratorTwo << " / " << denominatorTwo << " )" << endl;
// Output their original problem...
cout << "Your answer is: " << finalNumerator << " / " << finalDenominator << endl;
// Output the answer...
cout << "Just for fun, the decimal answer is: " << (finalNumerator + 0.0) / (finalDenominator + 0.0) << endl;
// I like to make sure the numbers are real, so output the decimal value...
cout << "Remember, this program does NOT calculate the lowest common denominator." << endl;
// Disclaimer...
return 0;
// Terminate the program...
}
void divideFractions( int numeratorOne, int numeratorTwo, int denominatorOne,
int denominatorTwo, int& finalNumerator, int& finalDenominator ) {
// The division method...
if(DEBUG) cout << endl << "divideFractions: Entering method..." << endl;
// Debug code...
int newNumerator = returnProduct(numeratorOne, denominatorTwo);
// Get the numerator: numeratorOne * denominatorTwo
int newDenominator = returnProduct(denominatorOne, numeratorTwo);
// Get the denominator: denominatorOne * numeratorTwo
finalNumerator = newNumerator;
finalDenominator = newDenominator;
// Set the references one we have both the values...
if(DEBUG) cout << endl << finalNumerator << finalDenominator << endl;
//Debug code...
}
void multiplyFractions( int numeratorOne, int numeratorTwo, int denominatorOne,
int denominatorTwo, int& finalNumerator, int& finalDenominator ) {
// The multiplication method...
if(DEBUG) cout << endl << "multiplyFractions: Entering method..." << endl;
// Debug code...
int newNumerator = returnProduct(numeratorOne, numeratorTwo);
// Get the numerator: numeratorOne * numeratorTwo
int newDenominator = returnProduct(denominatorOne, denominatorTwo);
// Get the denominator: denominatorOne * denominatorTwo
finalNumerator = newNumerator;
finalDenominator = newDenominator;
// Apply the references once we have the values...
if(DEBUG) cout << endl << finalNumerator << finalDenominator << endl;
// Debug code...
}
int returnProduct( int numberOne, int numberTwo ) {
return numberOne * numberTwo;
// All this method does is return the product of variable 1 by variable 2...
}
void subtractFractions ( int numeratorOne, int numeratorTwo, int denominatorOne,
int denominatorTwo, int& finalNumerator, int& finalDenominator ) {
// The subtraction method...
if(DEBUG) cout << endl << "subtractFractions: Entering method... " << endl;
// Debug code...
int commonDenominator = returnProduct( denominatorOne, denominatorTwo );
// Get the common denominator: denominatorOne * denominatorTwo
int newNumeratorOne = returnProduct(numeratorOne, denominatorTwo);
// Get the first numerator: numeratorOne * denominatorTwo
int newNumeratorTwo = returnProduct(numeratorTwo, denominatorOne);
// Get the second numerator: numeratorTwo * denominatorOne
finalNumerator = newNumeratorOne - newNumeratorTwo;
// The final numerator is the first minus the second...
finalDenominator = commonDenominator;
// Apply the references one we compute the values...
if(DEBUG) cout << endl << commonDenominator << finalNumerator << finalDenominator << endl;
// Debug code...
}
void addFractions ( int numeratorOne, int numeratorTwo, int denominatorOne,
int denominatorTwo, int& finalNumerator, int& finalDenominator) {
if(DEBUG) cout << endl << "addFractions: Entering method..." << endl;
// Debug code...
int commonDenominator = returnProduct(denominatorOne , denominatorTwo);
// Calculate the common denominator...
int newNumeratorOne = returnProduct(numeratorOne , denominatorTwo);
// Calculate the first new numerator...
int newNumeratorTwo = returnProduct(numeratorTwo , denominatorOne);
// Calculate the second new numerator...
finalNumerator = newNumeratorOne + newNumeratorTwo;
// Calculate the final numerator...
finalDenominator = commonDenominator;
// Calculate the final denominator...
if(DEBUG) cout << endl << commonDenominator << finalNumerator << finalDenominator << endl;
// Debug code...
}
void getNumbers( int& numeratorOne, int& numeratorTwo, int& denominatorOne, int& denominatorTwo, char symbol) {
// This method just gets the numbers from the user with a pretty menu...
if(DEBUG) cout << endl << "getNumbers: Entering method..." << endl;
// Debug code...
do {
cout << endl << "I am here to get your desired numerators and denominators" << endl;
cout << "Please enter them in this fashion: 1 2 3 4" << endl;
cout << "Please note the SPACES between the numbers." << endl;
cout << "Thus, 1 2 3 4 would mean: ( 1 / 2 ) " << symbol << " ( 3 / 4 ) " << endl;
cout << "I'll take those numbers now: ";
// Output a nice little input prompt...
cin >> numeratorOne >> denominatorOne >> numeratorTwo >> denominatorTwo;
// Get the numbers...
if((denominatorOne == 0) || (denominatorTwo == 0)) {
cout << endl << "You cannot have a denominator equal to 0. Please try again." << endl;
}
// Check for division by 0...
} while ( (denominatorOne == 0) || (denominatorTwo == 0));
// Run while the denominators are equal to 0...
}
void menu(int& menuChoice) {
// This is the menu method, it passes the choice back up to main...
do {
cout << "Welcome to the fraction calculator" << endl;
cout << "Your choices are:" << endl;
cout << "\t0) Replay Menu" << endl;
cout << "\t1) Add Fractions" << endl;
cout << "\t2) Subtract Fractions" << endl;
cout << "\t3) Multiply Fractions" << endl;
cout << "\t4) Divide Fractions" << endl;
( DEBUG ) ? cout << "\t5) DEBUG MODE OFF" << endl : cout << "\t5) DEBUG MODE ON" << endl;
cout << "Please enter your choice( ex. 1 ): ";
cin >> menuChoice;
// Get the actual choice after advisal...
if ( menuChoice == 5 ) {
DEBUG = !DEBUG;
menuChoice = 0;
}
if ( ( menuChoice < 0 ) || ( menuChoice > 5 ) ) {
cout << "Please enter a number 0 - 4." << endl;
menuChoice = 0;
}
// If it is 0 (or anything else), we will reprint the menu...
} while( menuChoice == 0 );
// End menu...
if(DEBUG) cout << endl << "Menu: Exit do-while... " << menuChoice << endl;
// Debug code...
}
Output:
Bookmarks