Hi!
The program is running well but i need to help how to reduce the fraction. when I enter 2, -3 , 5, 6 the output gives -10/18 instead of -5/9. This is my program someone can help me how i can fix it.
#include <cstdlib>
#include<iostream>
using namespace std;
//create their own data types. Perhaps the simplest method for doing so is via an
//enumerated type. An enumerated type is a data type
//where every possible value is defined as a symbolic constant (called an enumerator).
enum Menu_Arthmetic_Choice {
None = 0,
Menu_Add = 1,
Menu_Subtract =2,
Menu_Multiply = 3,
Menu_Divide = 4,
Menu_Quit = 5,
End = 6,
};
//Passing structure to functions
struct Fractions //Holds data for Fractions input
{
int resultNum;
int resultDen;
int num1;
int num2;
int den1;
int den2;
};
//Function Prototypes
int menu();
void addFractions(Fractions, int &resultNum, int &resultDen);
void subtractFractions(Fractions, int &resultNum, int &resultDen);
void multiplyFractions(Fractions, int &resultNum, int &resultDen);
void divideFractions(Fractions, int &resultNum, int &resultDen);
void showResults(Fractions, int resultNum, int resultDen, int operation);
void readFractionValues(Fractions &);
int SimplifyFraction(int &resultNum, int &resultDen);
int Simplify(int &resultNum, int &resultDen);
int main()
{
Fractions input;
int menuArthmeticChoice = None;
int resultNum = 0;
int resultDen = 0;
do
{
menuArthmeticChoice = menu();
// I used if instead of switch statement because it is more efficiency than switch statement
if (menuArthmeticChoice == Menu_Quit)
break;
//Call The functions
readFractionValues(input);
//If choice is "addfractions" then
if (menuArthmeticChoice == Menu_Add)
{
addFractions(input, resultNum, resultDen);
}
//If choice "subtractfractions" so
if (menuArthmeticChoice == Menu_Subtract)
{
subtractFractions(input, resultNum, resultDen);
}
//If choice "multiplyfraction" so
if (menuArthmeticChoice == Menu_Multiply)
{
multiplyFractions(input, resultNum, resultDen);
}
//If choice "divivefraction" so
if (menuArthmeticChoice == Menu_Divide)
{
divideFractions(input, resultNum, resultDen);
}
void readFractionValues(Fractions &input)
{
cout << "Enter denomitor for first fraction: ";
cin >> input.den1;
cout << "Enter numerator for first fraction: ";
cin >>input. num1;
cout << "Enter denomitor for second fraction: ";
cin >> input.den2;
cout << "Enter numerator for second fraction: ";
cin >> input.num2;
cout << endl;
}
/*****************************************************************
*addFractions function: adds two fractions using input provided and*
*display the results. *
******************************************************************/
void addFractions(Fractions input, int &resultNum, int &resultDen)
{
/*****************************************************************
*subtrctFractions function: subtractes two fractions using input *
*provided and display the results. *
******************************************************************/
void subtractFractions(Fractions input, int &resultNum, int &resultDen)
{
resultNum = (input.den1 * input.num2) - (input.den2 *input.num1);
resultDen = input.num1 * input.num2;
}
/*****************************************************************
*multiplyFractions function: multiplies two fractions using input *
*provided and display the results. *
******************************************************************/
void multiplyFractions(Fractions input, int &resultNum, int &resultDen)
{
resultNum = input.den1 * input.den2;
resultDen = input.num1 * input.num2;
}
/*****************************************************************
*divideFractions function: divides two fractions using input *
*provided and display the results. *
******************************************************************/
void divideFractions(Fractions input, int &resultNum, int &resultDen)
{
resultNum = input.den1 * input.num2;
resultDen = input.den2 * input.num1;
}
int SimplifyFraction(int &resultNum, int &resultDen)
{
//For the output 1.
/*if ((resultNum < 0 && resultDen > 0) || (resultNum > 0 && resultDen < 0))
//return resultDen;
{
if (resultNum > 0)
{
resultNum *= -1;
}
resultDen = resultDen;
}*/
/////////////////////////////////////////////////
// For the output 2
if (resultDen < 0 && resultNum > 0)
{
resultNum = -resultNum;
resultDen = -resultDen;
}
////////////////////////////////////////////
//Reduce the fractions
for (int i = resultNum*resultDen; i > 1; i--)
{
if ((resultNum % i == 0) && (resultDen % i == 0))
{
resultNum /= i;
resultDen /= i;
}
}
return (resultNum, resultDen);
}
/*****************************************************************
*showResults function:Displays the fractions and results *
*in equation form. *
******************************************************************/