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
|
#include <iostream>
#include <iomanip>
using namespace std;
int Divisor(int Top, int Bottom);
void InputData(int &Num1, int &Num2, int &Den1, int &Den2);
void CalculatedAnswers(int Num1, int Num2, int Den1, int Den2, int &Top, int &Bottom);
void DisplayAnswer(int Num1, int Num2, int Den1, int Den2, int Top, int Bottom);
void main()
{
int Numerator1=0, Numerator2=0, Denominator1=0, Denominator2=0, TopAnswer=0, BottomAnswer=0, GCD;
char Continue;
do
{
InputData(Numerator1, Numerator2, Denominator1, Denominator2);
CalculatedAnswers(Numerator1, Numerator2, Denominator1, Denominator2, TopAnswer, BottomAnswer);
GCD=Divisor(TopAnswer, BottomAnswer);
TopAnswer=TopAnswer/GCD, BottomAnswer=BottomAnswer/GCD;
DisplayAnswer(Numerator1, Numerator2, Denominator1, Denominator2, TopAnswer, BottomAnswer);
cout<<"Please enter Y to continue: "<<endl;
cin>>Continue;
}
while (Continue=='Y'||Continue=='y');
}
int Divisor(int Top, int Bottom)
{
int Temp;
while( Bottom!= 0)
{
Temp = Top % Bottom;
Top=Bottom;
Bottom=Temp;
}
return Temp;
}
void InputData(int &Num1, int &Num2, int &Den1, int &Den2)
{
cout<<"Please enter the first Numerator: "<<endl;
cin>>Num1;
cout<<"Please enter the first Denominator: "<<endl;
cin>>Den1;
cout<<"Please enter the second Numerator: "<<endl;
cin>>Num2;
cout<<"Please enter the second Denominator: "<<endl;
cin>>Den2;
}
void CalculatedAnswers(int Num1, int Num2, int Den1, int Den2, int &Top, int &Bottom)
{
Top=Num1*Den2+Num2*Den1;
Bottom=Den1*Den2;
}
void DisplayAnswer(int Num1, int Num2, int Den1, int Den2, int Top, int Bottom)
{
cout<<setw(4)<<Num1<<" "<<setw(4)<<Num2<<" "<<Top<<endl;
cout<<"---- + ---- = ------"<<endl;
cout<<setw(4)<<Den1<<" "<<setw(4)<<Den2<<" "<<Bottom<<endl;
}
|