// Katrina Ward 3/8/2019
// File: fractions.cpp CS1570
// Description: A single file program for Moe to add and divide fractions.
#include <iostream>
using namespace std;
// Function Prototypes
// Function: greeting()
// Description: Displays a greeting to the screen
// Pre: None
// Post: Will output a message to the screen
void greeting();
// Function: goodbyes()
// Description: Will display an exit greeting to the screen
// Pre: None
// Post: Will display a message to the screen.
void goodbyes();
// Function: menu()
// Description: Displays a menu to the user and returns their menu choice
// Pre: None
// Post: Outputs a message to the screen
char menu();
// Function: noFracError()
// Description: Displays an error message to the screen that the user
// has not entered any fractions yet
// Pre: None
// Post: Outputs a message to the screen
void noFracError();
// Function: getFrac()
// Description: Prompts for and reads in the numerator and denominator
// for a fraction
// Pre: None
// Post: The numerator and denominator will be changed to read in values
void getFrac(int & num, int & den);
// Function: addFrac()
// Description: Adds two fractions together and passes the answer back
// Pre: Neither denominators of te two added fractions can be 0
// Post: Will change the values of the third fraction passed to be the
// sum of the first two fractions
void addFrac(const int firstNum, const int firstDen, const int secondNum,
const int secondDen, int & ansNum, int & ansDen);
// Function: divideFrac()
// Description: Divides two fractions and passes back the answer
// Pre: Neither denominators of the two divided fractions can be 0
// Post: Will change the values of the third fraction passed to be the
// answer of the first two fractions divided
void divideFrac(const int firstNum, const int firstDen, const int secondNum,
const int secondDen, int & ansNum, int & ansDen);
// Fraction: displayFrac()
// Description: Displays a fraction to the screen
// Pre: None
// Post: Will display the passed fraction to the screen.
void displayFrac(const int num, const int den);
// Function: simplifyFrac()
// Description: Simplifies a function
// Pre: None
// Post: Will change the function passed to the simplified version
void simplifyFrac(int & num, int & den);
int main()
{
char choice; // User choice from menu
bool quit = false; // Flag to quit program
bool enterFrac = false // Flag to mark the first option was chosen
int num1, den1, num2, den2; // Entered fraction variables
int ansNum, ansDen; // Answer from the arithmetic functions
greeting();
do{
choice = menu();
switch(choice)
{
case '1': case 'e': case 'E':
getFrac(num1, den1);
getFrac(num2, den2);
enterFrac = true;
break;
case '2': case 'a': case 'A':
if(!enterFrac) // Check if fractions were entered first
noFracError();
else
{
addFrac(num1, den1, num2, den2, ansNum, ansDen);
simplifyFrac(ansNum, ansDen);
displayFrac(ansNum, ansDen);
}
break;
case '3': case 'd': case 'D':
if(!enterFrac)
noFracError();
else
{
divideFrac(num1, den1, num2, den2, ansNum, ansDen);
simplifyFrac(ansNum, ansDen);
displayFrac(ansNum, ansDen);
}
break;
case '4': case 's': case 'S':
if(!enterFrac)
noFracError();
else
{
simplifyFrac(num1, den1);
simplifyFrac(num2, den2);
cout <<"For the first fraction, ";
displayFrac(num1, den1);
cout <<"For the second fraction, ";
displayFrac(num2, den2);
}
break;
case '5': case 'q': case 'Q':
goodbyes();
quit = true;
break;
default:
cout << "Invalid menu choice. Please enter a number from the menu.\n";
}
} while(!quit);
return 0;
}
void greeting()
{
cout << "Welcome to the fractionator!" << endl;
return;
}
void goodbyes()
{
cout << "Thank you for using the fractionator! You're a genius now!" << endl;
return;
}
char menu()
{
char choice;
cout << "\t\tMenu" << endl;
cout << "\n1. Enter Fractions" << endl;
cout << "2. Add Fractions" << endl;
cout << "3. Divide Fractions" << endl;
cout << "4. Display Simplified Fractions" << endl;
cout << "5. Quit" << endl;
cout << "Enter your selection: ";
cin >> choice;
return choice;
}
void noFracError()
{
cout << "You have to enter fractions before they can be added!\n";
return;
}
void getFrac(int & num, int & den)
{
cout << "Input a numerator: ";
cin >> num;
cout << "\nInput a denominator: ";
cin >> den;
while(den==0) // Prevents a fractions entered with a 0 denominator
{
cout << "The denominator cannot be zero!\nChoose a different denominator: ";
cin >> den;
}
// Make a fraction with a negative numerator and denominator positive
// If only the denominator is negative, moves the negative to the
// numerator for cleaner arithmetic later.
if((num<0 && den < 0) || den < 0)
{
num *= -1;
den *= -1;
}
return;
}
void addFrac(const int firstNum, const int firstDen, const int secondNum,
const int secondDen, int & ansNum, int & ansDen)
{
ansDen = firstDen * secondDen;
ansNum = (firstNum*secondDen) + (secondNum*firstDen);
return;
}
void displayFrac(const int num, const int den)
{
cout << "Your fraction is: " << num;
// Will just display 0 if the numerator is 0 for cleaner output.
if(num!=0)
cout << "/" << den << endl;
else
cout << endl;
return;
}
void simplifyFrac(int & num, int & den)
{
for(int i=num*den; i > 0; i--)
{
if((den % i == 0) && (num % i == 0))
{
den/=i;
num/=i;
}
}
return;
}
void divideFrac(const int firstNum, const int firstDen, const int secondNum,
const int secondDen, int & ansNum, int & ansDen)
{
ansNum = firstNum * secondDen;
ansDen = firstDen * secondNum;
return;
}