A rational number is a number that can be represented as the quotient of two integers. For this project you are to write a C++ classthat can be used to represent non-negativerational numbers. Although I will only be grading the class that you write, you will probably also want to write a main program to test your class.
Your class should meet the following specifications:
/* Read a string of the form "n/d" from the keyboard,and
update the Rational object invoking the method to
represent the indicated number. Assume that both n
and d are unsignedint values, and that there
are nospaces between the n, the '/' and the d.*/
void input();
/* Write the object to the display in the form n/d.
The rational should be displayed in simplest form. */
void output();
Follow the usual C++ conventions when writing your class, including:
C++ Program:
File: Rational.h
#ifndef RATIONAL_H_INCLUDED
#define RATIONAL_H_INCLUDED
#include<iostream>
using namespace std;
class Rational
{
private:
int num;
int denom;
public:
//Default Constructor
Rational();
//Constructor
Rational(int val);
//Constructor
Rational(int n, int d);
//GCD Function
int gcd(int a, int b);
//Overloading + operator
Rational operator+(Rational r);
//Overloading - operator
Rational operator-(Rational r);
//Overloading * operator
Rational operator*(Rational r);
//Overloading / operator
Rational operator/(Rational r);
//Overloading ^ operator
Rational operator^(int val);
//Overloading equality operator
bool operator==(Rational r);
//Overloading in-equality operator
bool operator!=(Rational r);
//Overloading greater than operator
bool operator>(Rational r);
//Overloading less than operator
bool operator<(Rational r);
//Overloading greater than or equal to operator
bool operator>=(Rational r);
//Overloading less than or equal to operator
bool operator<=(Rational r);
//Extraction operator
friend ostream &operator<<(ostream &, Rational
&);
//Insertion operator
friend istream &operator>>(istream &, Rational
&);
//Function that prints reduced form
void reduceFraction();
//Function that prints decimal equivalent
void printDecimalEquivalent();
};
#endif // RATIONAL_H_INCLUDED
File: Rational.cpp
#include<iostream>
#include "Rational.h"
using namespace std;
int Rational::gcd(int a, int b)
{
return (b == 0 ? a : gcd(b, a%b));
}
//Default Constructor
Rational::Rational()
{
num = 0;
denom = 1;
}
//1 Arg constructor
Rational::Rational(int val)
{
num = val;
denom = 1;
}
//Constructor
Rational::Rational(int n, int d)
{
int i, g;
//If denominator is zero
if(d == 0)
{
cout << "\n Error!!! Denominator can't be zero... \n";
denom = 1;
num = n;
}
else
{
//Getting GCD
g = gcd(n, d);
//Reducing Fraction
num = n/g;
denom = d/g;
}
}
//Overloading + operator
Rational Rational::operator+(Rational r)
{
int g;
Rational temp;
//Adding numerator and denominator
temp.num = ( num * r.denom ) + ( denom * r.num );
temp.denom = denom * r.denom;
//Finding GCD
g = gcd(temp.num, temp.denom);
//Reducing fractions
temp.num = temp.num / g;
temp.denom = temp.denom / g;
return temp;
}
//Overloading - operator
Rational Rational::operator-(Rational r)
{
int g;
Rational temp;
//Updating numerator and denominator
temp.num = ( num * r.denom ) - ( denom * r.num );
temp.denom = denom * r.denom;
//Finding GCD
g = gcd(temp.num, temp.denom);
//Reducing fractions
temp.num = temp.num / g;
temp.denom = temp.denom / g;
if(temp.denom < 0)
{
temp.denom = -temp.denom;
temp.num = -temp.num;
}
return temp;
}
//Overloading * operator
Rational Rational::operator*(Rational r)
{
int g;
Rational temp;
//Updating numerator and denominator
temp.num = (num * r.num );
temp.denom = (denom * r.denom);
//Finding GCD
g = gcd(temp.num, temp.denom);
//Reducing fractions
temp.num = temp.num / g;
temp.denom = temp.denom / g;
return temp;
}
//Overloading / operator
Rational Rational::operator/(Rational r)
{
int g;
Rational temp;
//Updating numerator and denominator
temp.num = (num * r.denom);
temp.denom = (denom * r.num);
//Finding GCD
g = gcd(temp.num, temp.denom);
//Reducing fractions
temp.num = temp.num / g;
temp.denom = temp.denom / g;
return temp;
}
//Overloading ^ operator
Rational Rational::operator^(int val)
{
int g;
Rational temp;
//Updating numerator and denominator
temp.num = (pow((double)(num), (double)(val)));
temp.denom = (pow((double)(denom), (double)(val)));
//Finding GCD
g = gcd(temp.num, temp.denom);
//Reducing fractions
temp.num = temp.num / g;
temp.denom = temp.denom / g;
return temp;
}
//Overloading equality operator
bool Rational::operator==(Rational r)
{
return ((num * r.denom) == (r.num * denom));
}
//Overloading in-equality operator
bool Rational::operator!=(Rational r)
{
return ((num * r.denom) != (r.num * denom));
}
//Overloading greater than operator
bool Rational::operator>(Rational r)
{
return ((num * r.denom) > (r.num * denom));
}
//Overloading less than operator
bool Rational::operator<(Rational r)
{
return ((num * r.denom) < (r.num * denom));
}
//Overloading greater than or equal to operator
bool Rational::operator>=(Rational r)
{
return ((num * r.denom) >= (r.num * denom));
}
//Overloading less than or equal to operator
bool Rational::operator<=(Rational r)
{
return ((num * r.denom) <= (r.num * denom));
}
ostream &operator<<( ostream &output, Rational
&D ) {
output << D.num << "/" << D.denom;
return output;
}
istream &operator>>( istream &input, Rational
&D ) {
char ch;
input >> D.num >> ch >> D.denom;
return input;
}
//Function that reduces fraction
void Rational::reduceFraction()
{
int g;
Rational temp;
//Updating numerator and denominator
temp.num = num;
temp.denom = denom;
//Finding GCD
g = gcd(temp.num, temp.denom);
//Reducing fractions
temp.num = temp.num / g;
temp.denom = temp.denom / g;
//Printing reduced fraction
cout << endl << temp.num << "/" <<
temp.denom << endl;
}
//Function that prints decimal equivalent
void Rational::printDecimalEquivalent()
{
cout << endl << num << "/" << denom
<< " = " << (num/(double)denom) << endl;
}
File: main.cpp
#include <iostream>
#include <string>
#include "Rational.h"
using namespace std;
//Main method
int main()
{
char ch;
string op;
Rational r1, r2, r3;
Rational r4(3, 2), r5;
cout << endl << "r4 = " << r4;
//Power
r5 = r4^2;
cout << endl << "r4^2 = " << r5;
do
{
//Reading input
cout << "\n\n\nEnter the expression (e.g., 1/2-3/4) with
binary operator, and press enter:\n";
cin >> r1 >> op >> r2;
//Applying operations
if(op == "+")
cout << r1 << "+" << r2 << " equals to "
<< (r1+r2);
else if(op == "-")
cout << r1 << "-" << r2 << " equals to "
<< (r1-r2);
else if(op == "*")
cout << r1 << "*" << r2 << " equals to "
<< (r1*r2);
else if(op == "/")
cout << r1 << "/" << r2 << " equals to "
<< (r1/r2);
else if(op == "==")
cout << r1 << "==" << r2 << " equals to "
<< (r1==r2);
else if(op != "-")
cout << r1 << "!=" << r2 << " equals to "
<< (r1!=r2);
else if(op <= "-")
cout << r1 << "-" << r2 << " equals to "
<< (r1<=r2);
else if(op >= "-")
cout << r1 << ">=" << r2 << " equals to
" << (r1>=r2);
cout << "\nContinue to test binary
operators(y/n):?";
cin >> ch;
}while(ch=='y' || ch=='Y');
cout << "\n\n Bye! \n\n";
return 0;
}
Sample Run:

A rational number is a number that can be represented as the quotient of two integers....