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: 3679661 • 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 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 wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1.

Also include a default constructor that initializes an object to 0 (that is, 0/1).

Overload the relational operators ==, <, <=, >, >=, and != so that they correctly apply to type Rational.

Also write a test program to test your class.

Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are
positive rational numbers then a/b is less than c/d provided a*d is less than c*b.

use the header file Rational.h as astarting point for this Exercise

A function to standardize the values stored so that the denominator is always positive should be included. (An additional exercise would be to include a function to reduce the rational number so the numerator and denominator are as small as possible.)

________________________________________________________________________________________________

#ifndef RATIONAL_H

#define RATIONAL_H

#include <iostream>

using namespace std;

class Rational

{

public:

   Rational();

   // Initializes the rational number to 0

   Rational(int wholeNumber);

   // Initializes the rational number to wholeNumber/1

   Rational(int m, int n);

   // Initializes the rational number to m/n if n is not 0;

   // the sign of the rational is stored in the numerator

   // (the denominator is always positive);

   // exits if n = 0 (invalid rational number)

   void output();

   // Precondition: The rational number is defined.

   // Postcondition: The rational number has been displayed on

   // the screen in the form m/n.

   friend bool operator ==(const Rational& r1, const Rational& r2);

   // Precondition: r1 and r2 are valid rational numbers

   // Returns true if r1 equals r2; false otherwise.

   friend bool operator <(const Rational& r1, const Rational& r2);

   // Precondition: r1 and r2 are valid rational numbers

   // Returns true if r1 is less than r2; false otherwise.

   friend bool operator >(const Rational& r1, const Rational& r2);

   // Precondition: r1 and r2 are valid rational numbers

   // Returns true if r1 is greater than r2; false otherwise.

   friend bool operator <=(const Rational& r1, const Rational& r2);

   // Precondition: r1 and r2 are valid rational numbers

   // Returns true if r1 is less than or equal to r2; false otherwise.

   friend bool operator >=(const Rational& r1, const Rational& r2);

   // Precondition: r1 and r2 are valid rational numbers

   // Returns true if r1 is greater than or equal to r2; false otherwise.

   friend bool operator !=(const Rational& r1, const Rational& r2);

   // Precondition: r1 and r2 are valid rational numbers

   // Returns true if r1 is not equal to r2; false otherwise.

private:

   int num; // the numerator of the number

   int denom; // the denominator of the number

   void standardize();

   // Precondition: num and denom have values

   // Postcondition: if denom is 0 the program is terminated;

   // otherwise the rational number is standardized so that

   // denom is positive.

};

#endif

Explanation / Answer

Solution.cpp

/*
A rational number is a number that can be represented
as the quotient of two integers. For example, 10/20, 13/4, 64/23 etc are all rational
numbers.
*/


// Rationals.cpp

#include <iostream>//header file for input output function
#include <cstdlib>// this header contains exit(1) functionTerminates the process normally,
//performing the regular cleanup for terminating programs.


using namespace std;// This directive tells the compiler that the subsequent code is making use of names in the //specified namespace.

class Rational
{
public:
Rational();
// Initializes the rational number to 0

Rational (int wholeNumber);
// Initializes the rational number to wholeNumber/1

Rational (int m, int n);
// Initializes the rational number to m/n if n is not 0;
// the sign of the rational is stored in the numerator
// (the denominator is always positive);
// exits if n = 0 (invalid rational number)

friend ostream& operator <<(ostream& fout, const Rational& r);
// Precondition: If fout is a file output stream, then fout has
// already been connected to a file.
// Postcondition: The rational number has been sent to the
// output stream as numerator slash denominator (with the
// denominator always positive).

friend istream& operator >>(istream& fin, Rational& r);
// Precondition: If fin is a file input stream, then fin has
// already been connected to a file. The rational number in
// the input stream is in the form of an integer followed by
// the slash character followed by an integer.
// Postcondition: r has been set to the input value (and
// standardized so the denominator is positive).

private:
int num; // the numerator of the number
int denom; // the denominator of the number

   void standardize();
// Precondition: num and denom have values
// Postcondition: if denom is 0 the program is terminated;
// otherwise the rational number is standardized so that
// denom is positive.
};

int main ()
{
//
// Variable declarations
//
int m, n; // numerator and denominator - m/n
Rational r;//instance for rational class

  

//
// Read in a rational number
//
cout << "Enter a rational number in the form m/n : ";
cin >> r;
cout << endl;

//
// Print the number out
//
cout << "Here's your number: " << r << endl;

  
  
  
  

   // Rational(int)
   Rational a(20);
   cout << "Rational function with one argument :"<<a;
   Rational b(-40);
   cout << "Rational function with one argument :"<< b;

   // Rational(int,int)
   Rational c(110,22);
   cout <<"Rational function with two arguments :" <<c;
   Rational d(12,30);
   cout <<"Rational function with two argument :" <<d;
   Rational e(-40,5);
   cout << "Rational function with two argument :"<<e;
   Rational f(50,-3);
   cout << "Rational function with two argument :"<<f;

return 0;
}


// ===========================
// Function Definitions
// ===========================

Rational::Rational()
{
num = 0;
denom = 1;//initiallies rational to 0
}


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


Rational::Rational (int m, int n)
{
   num = m;
   denom = n;

}


void Rational::standardize()
{
if (denom < 0)
{
// denom = -1 * denom;
// num = -1 * num;
       // made code more succint and clear
       denom = -denom;
       num = -num;

}
else if (denom == 0)
{
cout << "Zero cannot be in the denominator - program aborting!!"
<< endl;
exit(1);
}
}


   // --------------------------------
   // ----- ENTER YOUR CODE HERE -----
   // --------------------------------

ostream& operator <<(ostream& outs, const Rational& r) {  

   if (r.denom < 0) { // Print out opposite signs
       outs << -(r.num) << "/" << -(r.denom) << endl;
} else {
       outs << r.num << "/" << r.denom << endl;
   }
   return outs;
  
}

istream& operator >>(istream& ins, Rational& r) {

   char temp; // setup to hold '/' character
   ins >> r.num >> temp >> r.denom;
   r.standardize();
   return ins;

}

output

Here's your number: 12/13                                                                                                                                                       

                                                                                                                                                                                

Rational function with one argument  :20/1                                                                                                                                      

Rational function with one argument  :-40/1                                                                                                                                     

Rational function with two arguments  :110/22                                                                                                                                   

Rational function with two argument   :12/30                                                                                                                                    

Rational function with two argument  :-40/5                                                                                                                                     

Rational function with two argument  :-50/3

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