In: Computer Science
Design, implement and test a C++ class to work with real numbers as rational numbers called class "Rational". The data members are the numerator and denominator, stored as integers. We have no need for setters or getters (aka mutators or accessors) in the Rational class. All of our operations on rational numbers involve the entired number, not just the numerator or denominator."
Code:
int greatestCommonDivisor(int x, int y) {
while (y != 0) {
int temp = x % y;
x = y;
y = temp;
}
return x;
}
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
class Rational
{
public :
int numer;
int denom;
Rational();
Rational(int numer, int denom);
void operator+=(const Rational &);
Rational operator++();
/**
* Friend function declarations
* Output Rational number to an output stream, Example 3+4i
*/
friend std::ostream& operator<<(std::ostream&
dout,Rational&);
/**
* read Rational number to an input stream
*/
friend std::istream& operator>>(std::istream& din,
Rational&);
Rational normalize();
int greatestCommonDivisor(int x, int y) ;
};
Rational::Rational()
{
this->numer=0;
this->denom=1;
}
int Rational::greatestCommonDivisor(int x, int y) {
while (y != 0) {
int temp = x % y;
x = y;
y = temp;
}
return x;
}
Rational Rational::normalize() {
int num = this->numer;
int den = this->denom;
if (num < 0 && den < 0) {
num = num * -1;
den = den * -1;
} else if (den < 0) {
num = num * -1;
den = den * -1;
}
int g=greatestCommonDivisor(num,den);
Rational r(num/g, den/g);
return r;
}
// Function implementation which display Rational numbers by using
the operator overloading '<<'
ostream& operator<<(ostream& dout,Rational&
c)
{
c.normalize();
dout << c.numer << "/" << c.denom;
return dout;
}
// Function implementation which read Rational numbers by using the
operator overloading '>>'
istream& operator>>(istream& din, Rational&
c)
{
char ch;
din >> c.numer;
din >> ch;
din >> c.denom;
c.normalize();
return din;
}
Rational::Rational(int numer, int denom)
{
this->numer=numer;
this->denom=denom;
}
// Preincrememnt operator overloading
Rational Rational::operator++()
{
Rational f(++numer,++denom);
return f;
}
void Rational::operator+=(const Rational& a)
{
this->numer=a.numer * denom + a.denom * numer;
this->denom= a.denom * denom;
}
Rational operator+(Rational& f1,Rational& f2) ;
bool operator==(Rational f1,Rational f2);
bool operator!=(Rational& f1,Rational& f2);
Rational operator--(Rational &r,int);
bool operator<(Rational& f1,Rational& f2);
bool operator<=(Rational&
f1,Rational& f2);
bool operator>=(Rational& f1,Rational& f2);
bool operator>(Rational& f1,Rational& f2);
int main() {
//Declaring variables
Rational r1,r2;
cout<<"Enter Rational#1:";
cin>>r1;
cout<<"Enter Rational#2:";
cin>>r2;
cout<<"Rational#1
:"<<r1<<endl;
cout<<"Rational#2
:"<<r2<<endl;
Rational r3=r1+r2;
cout<<"Rational#3
:"<<r3<<endl;
if(r1<r2)
{
cout<<r1<<" less than
"<<r2<<endl;
}
else
{
cout<<r1<<" not less
than "<<r2<<endl;
}
if(r1>r2)
{
cout<<r1<<" greater
than "<<r2<<endl;
}
else
{
cout<<r1<<" not greater
than "<<r2<<endl;
}
if(r1<=r2)
{
cout<<r1<<" less than
or equal to "<<r2<<endl;
}
else
{
cout<<r1<<" not less
than or equal to "<<r2<<endl;
}
if(r1>=r2)
{
cout<<r1<<" greater
than or equal to "<<r2<<endl;
}
else
{
cout<<r1<<" not greater
than or equal to "<<r2<<endl;
}
if(r1==r2)
{
cout<<r1<<" is equal to
"<<r2<<endl;
}
else
{
cout<<r1<<" is not
equal to "<<r2<<endl;
}
++r1;
cout<<"Rational#1 after preinecrement
:"<<r1<<endl;
return 0;
}
Rational operator+(Rational& f1,Rational& f2)
{
Rational f3;
f3+=f1;
f3+=f2;
return f3;
}
bool operator==(Rational f1,Rational f2)
{
if(f1.numer==f2.numer && f1.denom ==
f2.denom)
return true;
else
return false;
}
bool operator!=(Rational& f1,Rational& f2)
{
if(f1==f2)
return false;
else
return true;
}
// Postincrememnt operator overloading
Rational operator--(Rational &r,int)
{
Rational r1(r.numer--,r.denom--);
return r1;
}
bool operator<(Rational& f1,Rational& f2)
{
int a = f1.numer;
int b = f1.denom;
int c = f2.numer;
int d = f2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) < (n2 / d2))
return true;
else
return false;
}
bool operator<=(Rational& f1,Rational&
f2)
{
int a = f1.numer;
int b = f1.denom;
int c = f2.numer;
int d = f2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) <= (n2 / d2))
return true;
else
return false;
}
bool operator>=(Rational& f1,Rational& f2)
{
int a = f1.numer;
int b = f1.denom;
int c = f2.numer;
int d = f2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;
}
bool operator>(Rational& f1,Rational& f2)
{
int a = f1.numer;
int b = f1.denom;
int c = f2.numer;
int d = f2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) > (n2 / d2))
return true;
else
return false;
}
______________________________
Output:

_______________Could you plz rate me well.Thank
You