Question

In: Computer Science

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 +, << and >>.

In + operator, the sum of a/b and c/d is (a*d+b*c)/bd. In >> operator, if the user enter 0 for the denominator, output error message and prompt user to type again (use while loop). In << operator, output a rational number in the format a/b. If a = 0, just output 0. If b = 1, just output a. If the number is a/-b, output –a/b. If the number is –a/-b, output a/b. In the main function, declare two objects and use operator >> and assign data for them. Then sum the two numbers and display the results. Sample output:

Enter two integers: 3 0

Error: the denominator cannot be 0. Try again.

Enter two integers: 2 3

Enter two integers: 3 -4


2/3 + -3/4 = -1/12

(2 bonus marks) Add a private member function with function prototype void normalize( ) to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/-8 would be represented the same as -1/2. You must email me your code for this part.

Solutions

Expert Solution

//Your CPP code

#include
using namespace std;
class Rational{//class
   private:
       int num,den;
   public :
       Rational(){//default constructor
           num=0;
           den=1;
       }
       Rational(Rational const &obj){
           num=obj.num;
           den=obj.den;
       }
   int gcd(int a,int b){//gcd to normalize
       if(a == 0)return b;
       else
           gcd(b%a,a);
   }
        void normalize(){
       int n = this->num;
       int m = this->den;
       if( m < 0 ){
           m = -m;
           n = -n;
       }
       int k = gcd(m,n);
       if(k>0){//normalization
       m = m/k;
       n = n/k;}
       this->num=n;
       this->den=m;
   }//friend function prototype
   friend Rational operator+(Rational const &,Rational const &);
   friend istream &operator>>(istream &in,Rational &);
   friend ostream &operator<<(ostream &out,Rational &);  
};
ostream &operator<<(ostream &out,Rational &ob){
   if(ob.den < 0){
       ob.den = -ob.den;
       ob.num = -ob.num;
   }
   if(ob.num == 0 || ob.den == 1){
       out<    }else{
       out<    }
}
Rational operator+(Rational const &obj1 , Rational const &obj2 ){
   Rational R;
   R.num = obj1.num*obj2.den + obj2.num*obj1.den;
   R.den = obj1.den*obj2.den;
   return R;
}
istream &operator>>(istream &in ,Rational &obj1){
   int n,d;
   printf("Enter Numerator : ");
   in>>n;
   while(1){
       printf("Enter Denominator : ");
       in>>d;
       if(d == 0)
       printf("Wrong input again Enter ,Denominator can't be 0\n");
       else
           break;  
   }
   obj1.num = n;
   obj1.den = d;
   return in;
}
int main(){
   int i , j , k ;
   Rational R1,R2,R4;//object creation
   cin>>R1;
   cin>>R2;//>> operator called
   Rational R3 = R1+R2;//+ operator called
   printf("Sum result\n");
   cout<    R4=R3;
  
   R4.normalize();// Your additional function
   printf("Normalize answer\n");
   cout< return 0;
}

//Screenshot of my editor

//Screenshot of my output terminal


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...
Write a rational number class in C++. Recall a rational number is a rational number, composed...
Write a rational number class in C++. Recall a rational number is a rational number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following two constructors: one constructor to make...
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...
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter)...
9.6 (Rational Class) 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...
There are multiple derivations of the Rational Rule. Define the Rational Rule broadly. Define the Rational...
There are multiple derivations of the Rational Rule. Define the Rational Rule broadly. Define the Rational Rule for Buyers Define the Rational Rule for Sellers Define the Rational Rule for Workers Define the Rational Rule for Society Define the Rational Rule for Employers
Is rational number divided by an irrational number equal to irrational number or rational number?
Is rational number divided by an irrational number equal to irrational number or rational number? for example such as ( 5 / 2pi )
“A real number is rational if and only if it has a periodic decimal expansion” Define...
“A real number is rational if and only if it has a periodic decimal expansion” Define the present usage of the word periodic and prove the statement
1. Use cardinality to show that between any two rational numbers there is an irrational number....
1. Use cardinality to show that between any two rational numbers there is an irrational number. Hint: Given rational numbers a < b, first show that [a,b] is uncountable. Now use a proof by contradiction. 2. Let X be any set. Show that X and P(X) do not have the same cardinality. Here P(X) denote the power set of X. Hint: Use a proof by contradiction. If a bijection:X→P(X)exists, use it to construct a set Y ∈P(X) for which Y...
Use cardinality to show that between any two rational numbers there is an irrational number. Hint:...
Use cardinality to show that between any two rational numbers there is an irrational number. Hint: Given rational numbers a < b, first show that [a, b] is uncountable. Now use a proof by contradiction
Consider a class called Building. This class consists of a number of floors (numberOfFloors) for the...
Consider a class called Building. This class consists of a number of floors (numberOfFloors) for the building, a current floor for the elevator (current), a requested floor of a person waiting for the elevator (requestedFloor), and methods for constructing the building object, for moving the elevator one floor up, for moving the elevator one floor down, for requesting the elevator and for starting the elevator going. Assume that requestedFloor will be set to 0 if there are currently no requests...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT