a program that will allow the user to input two fractions and one of the four operations of +, - , *, and / . Once this information has been entered, the program should compute the operation on the two fractions and then output the answer appropriate labeled. Reducing the fractions using the gcd algorithm. The program should then ask the user if another number is desired (y or n) and continue requesting pairs of fractions until the user answers no (n). My problem is encorporating the switch statement for the operations and also the gcd algorithm. Here is what I have, I only used addition and sub. because I couldn't figure out how to use the switch.
--------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
void EnterOperator(char &opchar)
{
cout << "Enter an operation to perform { + - / * } ";
cin >> opchar;
}
void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function will allow the user to enter two fraction. */
{
cout << "Enter the numerator and denominator for the first fraction; include a space: ";
cin >> Num >> Denom;
cout << endl;
cout << "Enter the numerator and denominator for the second fraction; include a space: ";
cin >> Num2 >> Denom2;
cout << endl;
}
//-------------------------------------------------------------------
void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two
fractions Reduce() reduced
Pre: Two Fractions
Post: One reduced fraction */
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num + Num2;
}
else
{
Num = Num + Num2;
}
}
void SubFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two
fractions Reduce() reduced
Pre: Two Fractions
Post: One reduced fraction */
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num - Num2;
}
else
{
Num = Num - Num2;
}
}
void DisplayFraction(int &Num, int &Denom)
/* This function displays the reduced and added fraction. This
function is called after AddFraction()
Post: Prints fraction */
{
cout << "The fraction value is: " << Num << "/" << Denom << endl;
}
int main()
{
char an;
int op;
do
{
int Num, Denom, Num2, Denom2 = 0;
char opchar;
EnterOperator(opchar);
ReadFraction(Num, Denom,Num2,Denom2);
AddFraction(Num, Denom, Num2, Denom2);
SubFraction(Num, Denom, Num2, Denom2);
DisplayFraction(Num, Denom);
cout << endl;
switch (opchar) {
case '+':
op = ;
break;
case '-':
op = ;
break;
default: // If operator is illegal shut program down
cout << "Invalid operator." << endl;
int gcd(int Num, int Denom ) {
assert(Denom != 0);
int rem = Num % Denom;
while(rem !=0 ){
Num = Denom;
Denom = rem;
rem = Num % Denom;
}
return Denom;
}
}
cout <<"Would you like to do another fraction? ";
cin >> an;
cout << endl;
} while ((an == 'y') || (an == 'Y'));
return(0);
}