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
|
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctype.h>
#include <locale>
using namespace std;
int EquationNum (int Num1, int Den1, int Num2, int Den2);
int EquationDen (int Num1, int Den1, int Num2, int Den2);
int GreatestCommonDenominator (int Numerator, int Denominator);
void Output (int Num1, int Den1, int Num2, int Den2, int Numerator, int Denominator);
int main()
{
int Numerator, Denominator, GCD, Num1, Num2, Den1, Den2;
char Restart;
do
{
cout << "This program adds fractions. 'Y' continues, any other key exits program\n";
cout << "=======================================================================\n";
cout << "Enter " << setw(18) << "numerator 1 ==> ";
cin >> Num1;
if (isdigit(Num1))
{
cout << "you need to enter a number here.";
cin.clear();
}
cout << "Enter denominator 1 ==> ";
cin >> Den1;
if (isdigit(Den1))
{
cout << "you need to enter a number here.";
cin.clear();
}
cout << endl;
cout << "Enter " << setw(18) << "numerator 2 ==> ";
cin >> Num2;
if (isdigit(Num2))
{
cout << "you need to enter a number here.";
cin.clear();
}
cout << "Enter denominator 2 ==> ";
cin >> Den2;
if (isdigit(Den2))
{
cout << "you need to enter a number here.";
cin.clear();
}
cout << endl;
Numerator = EquationNum (Num1, Den1, Num2, Den2);
Denominator = EquationDen (Num1, Den1, Num2, Den2);
GCD = GreatestCommonDenominator (Numerator, Denominator);
Numerator = Numerator / GCD;
Denominator = Denominator / GCD;
Output (Num1, Den1, Num2, Den2, Numerator, Denominator);
cout << "-----------------------------------------------------\n";
cout << "Continue? Y or N! ==> ";
cin >> Restart;
system("cls");
}
while (Restart == 'Y' || Restart == 'y');
}
int EquationNum ( int A, int B, int C, int D)
{
int Numerator;
Numerator = A * D + B * C;
return Numerator;
}
int EquationDen ( int A, int B, int C, int D)
{
int Denominator;
Denominator = B * D;
return Denominator;
}
int GreatestCommonDenominator (int Numerator, int Denominator)
{
int GCD = 0;
for(int i = 1; i <= Numerator && i <= Denominator ; i++)
if(Numerator % i == 0 && Denominator % i == 0 )
GCD=i;
return GCD;
}
void Output (int A, int B, int C, int D, int Numerator, int Denominator)
{
cout << setw(2) << A << setw(3) << " " << setw(3) << C << setw(3) << " " << setw(3) << Numerator << endl;
cout << setw(3) << "---" << " + " << setw(3) << "---" << " = " << setw(3) << "---" << endl;
cout << setw(2) << B << setw(3) << " " << setw(3) << D << setw(3) << " " << setw(3) << Denominator << endl;
}
|