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
|
#include <iostream>
using namespace std;
void getRational (int& numer, int& denom );
void displayRational ( int numer, int denom );
void addRational ( int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2);
void subtractRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2);
int GCD (int ansNum, int ansDenom);
int main()
{
int numer;
int denom;
int num1;
int denom1;
int num2;
int denom2;
int ansNum;
int ansDenom;
cout << "Input the first pair of rational numbers (numerator and denominator):" << endl;
cout << "First number entered is: ";
cin >> num1;
cout << "Second number entered is: ";
cin >> denom1;
displayRational (num1, denom1);
cout << "Input the second pair of rational numbers (numerator and denominator):" << endl;
cout << "First number entered is: ";
cin >> num2;
cout << "Second number entered is: ";
cin >> denom2;
getRational (num2, denom2);
displayRational (num2, denom2);
addRational (ansNum, ansDenom, num1, denom1, num2, denom2);
subtractRational (ansNum, ansDenom, num1, denom1, num2, denom2);
GCD (ansNum, ansDenom);
return 0;
}
void getRational (int& numer, int& denom)
{
double rational;
rational = ( numer / denom );
cout << "Result of numerator divided by the denominator is : " << rational << endl;
}
void displayRational (int numer, int denom)
{
double rational;
rational = ( numer / denom );
cout << "The rational numerator and denominator entered is : " << numer << "/" << denom << endl;
}
int GCD (int ansNum, int ansDenom)
{
int t;
while (ansDenom!=0)
{
t=ansDenom;
ansDenom=ansNum%ansDenom;
ansNum=t;
}
return ansNum;
}
void addRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
{
ansNum = (( num1/denom1) +( num2/denom2 ));
ansDenom = ( denom1 + denom2 );
cout << "The addition of the numerators entered is : " << ansNum << endl;
cout << "The addition of the denominators entered is : " << ansDenom << endl;
}
void subtractRational (int& ansNum, int& ansDenom, int num1, int denom1, int num2, int denom2)
{
ansNum = ( num1 - num2);
ansDenom = ( denom1 - denom2);
cout << "The subtraction of the numerators entered is: " << ansNum << endl;
cout << "The subtraction of the denominators entered is: " << ansDenom << endl;
}
|