#include <iostream>
#include <iomanip>
usingnamespace std;
//declare a struct called rNum that has two fields called top and bot.
struct rNum
{
int top;
int bot;
};
rNum getRationalNumber();
rNum addRationalNumbers(rNum n1, rNum n2);
void print( rNum n1, rNum n2, rNum ans );
int main()
{
rNum num1;
rNum num2;
rNum answer;
cout << "Rational Number 1" << endl;
//call getRationalNumber function to get the first rational number
getRationalNumber();
cout << "Rational Number 2" << endl;
//call getRationalNumber function to get the second rational number
getRationalNumber();
//call addRationalNumbers function to get the sum of the two rational numbers
addRationalNumbers(num1, num2);
//call print function to show the answer
print(num1, num2, answer);
return 0;
};
/***************************************************************************\
********
n is a rational number
This function simplifies n, which is the rational number in the caller
****************************************************************************\
********/
/*
int simplify(rNum)
{
cout << "top num is" << num.top << endl;
/*
if(num.bot == 0)
{
return num.top;
else
{
if(num.top % num.bot == 0)
{
num.top = num.top / num.bot;
}
if(num.bot % num.top == 0)
{
num.bot = num.bot / num.bot;
}
}
return num;/
}
*/
/***************************************************************************\
*******
This function fills a rational number with the top and bottom numbers entere\
d from the keyboard
and returns it.
****************************************************************************\
********/
rNum getRationalNumber()
{
rNum num; //rational number
cout << "\tEnter the top number: ";
cin >> num.top;
cout << "\tEnter the bottom number: ";
cin >> num.bot;
return num;
}
/***************************************************************************
n1 is the first rational number
n2 is the second rational number
This function returns the sum of n1 and n2 in the simplest format.
**************************************************************************/
rNum addRationalNumbers(rNum n1, rNum n2)
{
rNum ans;
ans.top = (n1.top * n2.bot) + (n2.top * n1.bot);
ans.bot = n1.bot * n2.bot;
//call simplify here
//simplify(ans)
return ans;
}
/***************************************************************************
n1 is the first rational number
n2 is the second rational number
ans is the sum of n1 and n2
This function will show the answer in the following format:
6 13 5
----- + ----- = -----
5 10 2
********************************************************************************/
void print( rNum n1, rNum n2, rNum ans )
{
cout << n1.top << "\t\t " << n2.top << "\t\t " << ans.top << endl;
cout << "--" << "\t+\t" << "--" << "\t=\t" << "---" << endl;
cout << n1.bot << "\t\t " << n2.bot << "\t\t " << ans.bot << endl;
}
Here is a sample of my output:
Rational Number 1
Enter the top number: 2
Enter the bottom number: 3
Rational Number 2
Enter the top number: 5
Enter the bottom number: 7
1099091392 4197152 0
-- + -- = ---
32766 0 0