Question

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

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:

  • Rational numbers should be represented by two unsignedints.
  • The class should have three constructors:
    • A default constructor that initializes the object to0;
    • A constructor that takes a single unsignedint parameterN and initializes the object to the rational number N/1;
    • A constructor that takes two unsigned ints as parameters, representing the numerator and denominator of the rational.This constructor should fail gracefully if the class user attempts to create a rational object with 0 as the denominator.
  • It should have input and output member functions with the following signatures and meanings:

/* 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();

  • Overload the following operators as members of the class: +, -, *, /, ^. Plus, minus, asterisk and slash perform the usual arithmetic operations. You should assume that the user of your class will never attempt a subtraction that results in a rational number less than zero.The ^ operator is a binary operator that raises the rational number (the left operand) to a non-negative integer power (the right operand). If the user requests an undefined operation (for example, division by zero or 0^0), your class should fail gracefully: print a descriptive error message and call exit().
  • Your class should overload the insertion and extraction operators (<< and >>) as friends of the class.The insertion operator should print the rational in simplest form.(A rational number is in simplest form if the numerator and denominator have no common factors other than 1.)It is probably easiest to write the insertion and extraction operators so that they use input() and output().

Follow the usual C++ conventions when writing your class, including:

  • Place the class definition in the file "Rational.h" and its implementation in "Rational.cc".
  • Use reference parameters, const parameters, const functions, and const return values, as appropriate.
  • Anything that is not part of the public interface should be in the private section of the class declaration.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
A rational number is a number that can be represented as the quotient of two integers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT