Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a class for rational numbers. A rational number is a number that can be r

ID: 3838380 • Letter: D

Question

Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2 and so forth are all rational numbers (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program.) Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class Rational. Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also include a constructor that has only a single parameter of type int; call this single parameter whole number and define the constructor so that the object will be initialized to the rational number whole_number/1. Also include a default constructor that initializes an object to 0 (that is, to 0/1). Overload the input and output operators > > and

Explanation / Answer

#include<iostream>
#include<cmath>
#include<stdlib.h>
using namespace std;

class Rational
{
public:
Rational(int = 0, int = 1);

Rational(int whole_number);
void reduce();
void printData();
Rational operator +(Rational);
Rational operator -(Rational);
Rational operator *(Rational);
Rational operator /(Rational);
bool operator ==(Rational &f2);
bool operator != (Rational &f2);
bool operator <=(Rational &f2);
bool operator >= (Rational &f2);
bool operator >(Rational &f2);
bool operator < (Rational &f2);
friend ostream &operator<<( ostream &,const Rational & );
friend istream &operator>>( istream &, Rational &) ;


private:
int num, denom;
};


Rational::Rational(int whole_number)
{
num = whole_number;
denom = 1;
}

ostream &operator<<( ostream &output,const Rational &F ) {
output << " Numerator : " << F.num << " Denominator : " << F.denom;
return output;   
}

istream &operator>>( istream &input, Rational &F ) {
input >> F.num >> F.denom;
return input;   
}
bool Rational::operator== (Rational &f2) //operator function   
{
return ( this->num == f2.num && this->denom == f2.denom);
}
bool Rational::operator!= (Rational &f2) //operator function   
{
return ( this->num != f2.num && this->denom != f2.denom);
}

bool Rational::operator>= (Rational &f2) //operator function   
{
return ( this->num >= f2.num && this->denom >= f2.denom);
}
bool Rational::operator> (Rational &f2) //operator function   
{
return ( this->num > f2.num && this->denom > f2.denom);
}bool Rational::operator<= (Rational &f2) //operator function   
{
return ( this->num <= f2.num && this->denom <= f2.denom);
}
bool Rational::operator< (Rational &f2) //operator function   
{
return ( this->num < f2.num && this->denom < f2.denom);
}
Rational::Rational(int num, int denom)
{
this->num = num;
this->denom = denom;
reduce();
}
void Rational::reduce()
{

num = num%denom;
if (num < 0)
{
num = denom + num;

}
for (int i = 2; i <= sqrt(denom); i++)
{
if (num%i == 0)
if (denom%i == 0)
{
num = num / i;
denom = denom / i;
}
}
}
Rational Rational::operator+(Rational f1) //operator function
{
Rational f;

f.denom = this->denom * f1.denom;
f.num = (this->denom * f1.num) + (f1.denom * this->num);
reduce();
return f;
}
Rational Rational::operator -(Rational f1)
{
Rational f;

f.denom = this->denom * f1.denom;
f.num = (f1.denom * this->num) - (this->denom * f1.num);
reduce();
return f;
}
Rational Rational::operator *(Rational f1)
{
Rational f;
f.num =this->num * f1.num;
f.denom = this->denom * f1.denom;
reduce();
return f;
}
Rational Rational::operator /(Rational f1)
{
Rational f;
f.num = this->num * f1.denom;
f.denom = this->denom * f1.num;
reduce();
return f;
}
void Rational::printData()
{
cout << num << "/" << denom << endl;
}


int main()
{
Rational f1, f2, sum, diff, prod, quo;
cin >> f1;
cin >> f2;

cout << "The first fraction is: " <<f1 << endl << endl;
cout << "THe second fraction is: " <<f2 << endl << endl;

sum = f1 + f2;
diff = f1 - f2;
prod = f1 * f2;
quo = f1 / f2;

cout << "The sum is: " << sum << endl << endl;
cout << "The difference is: " << diff << endl << endl;
cout << "The product is: " << prod << endl << endl;
cout << "The quotient is: " << quo << endl << endl;

if(f1==f2)   
cout<< "The two fractions are equal!";
else
cout<< "The two fractions are not equal!";

if(f1>f2)
cout << " Fraction1 is greater than Fraction2";
else
cout << " Fraction2 is greater than Fraction1";

  

return 0;
}

Output:

The first fraction is: Numerator : 4 Denominator : 5

THe second fraction is: Numerator : 7 Denominator : 8

The sum is: Numerator : 67 Denominator : 40

The difference is: Numerator : -3 Denominator : 40

The product is: Numerator : 28 Denominator : 40

The quotient is: Numerator : 32 Denominator : 35

The two fractions are not equal!
Fraction2 is greater than Fraction1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote