Question

In: Computer Science

Design, implement and test a C++ class to work with real numbers as rational numbers called...

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."

  • <<" and">>" (i.e., input and output). It's a design decision as to when to "normalize" a rational number. The numerator and denominator will be in "lowest terms" and only the numerator may be negative. For example, if we were given as input 4/-8, we would store and display it as -1/2. Simililarly -8/-6 would turn into 4/3. Thus when they are first created, they must be stored as normalized and any operation that might change a number must also normalize it. We will maintain the numbers in their normalized form at all times. Either (or both) of the numerator and the denominator may be input as negative integers. The following are all possible inputs: 1/2, -1/-2, -1/2 and 1/-2. Rational numbers are read and written as an integer, followed by a slash, and followed by an integer.
  • "+=" operator must be implemented as a member function, aka method. "+" operator must be implemented (ie., addition) as a non-member function that calls the "+="operator. Do not make "+" operator a friend."==" operator must be Implement as a non-member. "!=" operator must be implemented as non-member, but not as a friend.
    • "++" operator and "--" operator must be:
      • Both pre- and post-.
      • Member for "++" operator
      • Non-member, non-friend for "--" operator
    • "<" operator must be non-member
    • "<=" , ">" and ">=" operators must be implemented as non-member and non-friend.
  • Make it possible to write "if (r) {}", where r is a Rational number. Use the code given below to compute the greatest common divisor of two non-negative integers, that should be useful for writing the normalize function:

Code:

int greatestCommonDivisor(int x, int y) {
while (y != 0) {
int temp = x % y;
x = y;
y = temp;
}
return x;
}

Solutions

Expert Solution

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


Related Solutions

Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write an app to test your class. Use integer variables to represent the private instance variables of the class—the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object...
Define a class called Rational for rational numbers. Arational number is a number that can...
Define a class called Rational for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, /2, 3/4, 64/2, and so forth are all rational numbers.Represent rational numbers as two private values of type int, numfor the numerator and den for the denominator. The class has a default constructor with default values (num = 0,den = 1), a copy constructor, an assignment operator and three friend functions for operator overloading...
C++ Program Overload the following operators to work with the Rational class and add test cases...
C++ Program Overload the following operators to work with the Rational class and add test cases in the driver program. Make sure your driver program now tests each of these symbols for your Rational Class. + – * / == != < <= > >= << (stream insertion operator, use the toString function) >> (stream extraction operator) Make sure you test all 12 operators Example Run (Bold is input), everything else is a print statement using the overloaded operators Enter...
In C++ Create a class called Rational (separate the files as shown in the chapter) for...
In C++ Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example,...
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module. Think of all the things you would want to do with such a class and write corresponding member functions for your Module class. Your class declaration should be well-documented so that users will know how to use it.Write a main program that does the following: Declare an array of all your modules. The elements of the array must be of type Module....
ASAP (Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13...
ASAP (Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13 using BigInteger for the numerator and denominator. Use the code at https://liveexample.pearsoncmg.com/test/Exercise13_15Test.txt to test your implementation. Here is a sample run: Sample Run Enter the first rational number: 3 454 Enter the second second number: 7 2389 3/454 + 7/2389 = 10345/1084606 3/454 - 7/2389 = 3989/1084606 3/454 * 7/2389 = 21/1084606 3/454 / 7/2389 = 7167/3178 7/2389 is 0.0029300962745918793 Class Name: Exercise13_15 If...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables: employee’s name reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10. hourly wage Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks: Compute yearly salary - both the gross pay and net pay Increase...
Design and implement a class called circle_location to keep track of the position of a single...
Design and implement a class called circle_location to keep track of the position of a single point that travels around a circle. An object of this class records the position of the point as an angle, measured in a clockwise direction from the top of the circle. Include these public member functions: • A default constructor to place the point at the top of the circle. • Another constructor to place the point at a specified position. • A function...
C++Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary...
C++Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search algorithm discussed in the lecture slides. Define method LinearSearch() to implement linear search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Define method BinarySearch() to implement binary search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT