In: Computer Science
Write a rational number class in C++. Recall a rational number is a rational number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator.
A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following two constructors:
Remember the rules for a rational number. You constructor (and every mutator function) must enforce the rules!
EXTRA CREDIT: Feel free to write one constructor with default values.
You should also provide the following member functions:
Note the following formula for adding two rational numbers:
a/b + c/d = ( a*d + b*c)/(b*d)
Starter code:
int main()
{
// ToDo: declare three rational objects using
the default constructor
char answer;
// Main loop to read in rationals and compute the sum
do {
cout << "\nEnter op1 (in the format of p/q):";
// ToDo: use your input member function to read
the first rational
cout << "\nEnter op2 (in the format of p/q):";
// ToDo: use your input member function to read
the second rational
// ToDo: use the third rational to call Sum
with first and second as parameters
cout << "\nThe sum of op1 and op2 is:";
// ToDo: ouptput the third
rational
cout << endl;
cout << "\nTry again (Y/N)?";
cin >> answer;
} while (answer == 'y' || answer == 'Y');
// ToDo: test getters on rational that has
the sum above.
cout << "\nC's numerator is: " ;
cout << "\nC's denominator is: ";
// TODO: Use two constructors to declare a whole number 3/1 and a 4/5
// TODO: Use output to print both rationals
return 0;
}
Sample output:
Enter op1 (in the format of p/q): 3/4 Enter op2 (in the format of p/q): 4/5 The sum of op1 and op2 is: 31/20 Try again (Y/N)? Enter op1 (in the format of p/q): 9/0 wrong input. Enter a rational (p/q):9/3
Enter op2 (in the format of p/q): 4/31 The sum of op1 and op2 is: 291/93 Try again (Y/N)? C's numerator is: 291 C's denominator is: 93 3/1 4/5
The above diagram shows how your code should be formatted
CODE:
=====================================
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
using namespace std;
class Addition_Rational
{
private:
int numerator;
int denominator;
public:
//default constructor (no argument)
Addition_Rational();
//parameterized constructor - only takes numerator
Addition_Rational(int i);
//parameterized constructor - takes both numerator and
denominator
Addition_Rational(int p, int q);
//function to read the number in p/q format
void input();
//function to write the number in p/q format
void result();
//function to get numerator
int get_Numerator();
//function to get the denominator
int get_Denominator();
//function to produce the sum of the two rational numbers.
Formula for addition: a/b + c/d = (a*d + b*c)/(b*d)
void sum(Addition_Rational a, Addition_Rational b);
// test if two rational numbers are equal.
//bool isEqual(const Addition_Rational& op);
};
int main()
{
//declare three rational objects using the default
constructor
Addition_Rational p_q1, p_q2, pq;
char answer;
//main loop to read the rationals numbers and compute the
sum
do {
//read first number
cout << "\nEnter op1 (in the format of p/q):";
p_q1.input();
p_q1.result();
//read second number
cout << "\nEnter op2 (in the format of p/q):";
p_q2.input();
//Debug line
p_q2.result();
//third rational variable to pass the two rational numbers to
the sum function
pq.sum(p_q1, p_q2);
cout << "\nThe sum of op1 and op2 is:";
pq.result();
//ouptput the result of the two rational numbers passed
cout << endl;
cout << "\nTry again (Y/N)?";
cin >> answer;
cin.ignore(256,'\n'); //to flush
the buffer from storing unnecessary/garbage values
} while (answer == 'y' || answer == 'Y');
//testing getters
cout << "\nLast output's numerator is: " <<
pq.get_Numerator() << endl;
cout << "\nLast output's denominator is: " <<
pq.get_Denominator() << endl;
return 0;
}
//Implementation of class member functions
//default constructor - The constructor sets the rational
number's numerator to 0, and denominator to 1
Addition_Rational::Addition_Rational()
{
numerator = 0;
denominator = 1;
}
//single parameterized constructor - The constructor sets the
rational number's numerator to the given parameter and sets the
denominator to 1.
Addition_Rational::Addition_Rational(int i)
{
numerator = i;
denominator = 1;
}
//this constructor uses the two values to initialize the
rational number's numerator and denominator respectively.
Addition_Rational::Addition_Rational(int p, int q)
{
numerator = p;
denominator = q;
}
//function for addition of op1 and op2
void Addition_Rational::sum(Addition_Rational p_q1,
Addition_Rational p_q2)
{
int num = (p_q1.numerator*p_q2.denominator +
p_q1.denominator*p_q2.numerator);
int den = (p_q1.denominator*p_q2.denominator);
numerator = num;
denominator = den;
}
void Addition_Rational::input()
{
string in;
int num, den;
getline(cin, in);
int indx = in.find("/"); //finding the position of "/"
to separate numerator and denominator
num = atoi(in.substr(0, indx).c_str()); //numerator
separated from the rational number
den = atoi(in.substr(indx+1, in.length()).c_str());
//denominator separated from the rational number
numerator = num;
denominator = den;
}
void Addition_Rational::result()
{
cout << numerator << "/" << denominator;
}
// Two getter functions
int Addition_Rational::get_Numerator()
{
return numerator;
}
int Addition_Rational::get_Denominator()
{
return denominator;
}
========================
OUTPUT: