Question

C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

C++

Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities:

  1. Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators.
  2. Overload the addition, subtraction, multiplication, and division operators for this class.
  3. Overload the relational and equality operators for this class.
  4. Provide a function that returns the string representation of the rational number in the form a/b, where a is the numerator and b is the denominator.
  5. Provide a function that returns the rational number as a double.

Also provide a main program to test your class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the c++ code for the above task:

#include <iostream>

using namespace std;

// function to compute gcd of two numbers

int _gcd(int x, int y) {

   x = abs(x);

   y = abs(y);

   if(x % y == 0) return y;

   else return _gcd(y, x % y);

}

// function to simplify the fraction

void simplify(int &n, int &d) {

   int gcd = _gcd(n, d);

   n /= gcd;

   d /= gcd;

   if(d < 0) {

       n = -n;

       d = -d;

   }

}

// Rational number class

class Rational {

   int num; // numerator

   int den; // denominator

   public:

   // constructor

   Rational(int n, int d) {

       if(d != 0) {

           num = n;

           den = d;

           simplify(num, den);

       }

   }

   // getters

   int getNum() {

       return num;

   }

   int getDen() {

       return den;

   }

   // setters

   int setNum(int n) {

       num = n;

   }

   int setDen(int d) {

       den = d;

   }

  

   // overloading operators

   Rational operator+(const Rational& rhs) {

       // make denominator equals to lcm and then add numerator

       int lcm = (den * rhs.den) / _gcd(den, rhs.den);

       Rational result((lcm / den) * num + (lcm / rhs.den) * rhs.num, lcm);

       return result;

   }

   Rational operator-(const Rational& rhs) {

       // make denominator equals to lcm and then subtract

       int lcm = (den * rhs.den) / _gcd(den, rhs.den);

       Rational result((lcm / den) * num - (lcm / rhs.den) * rhs.num, lcm);

       return result;

   }

   Rational operator*(const Rational& rhs) {

       // muliply

       Rational result(num * rhs.num, den * rhs.den);

       return result;

   }

   Rational operator/(const Rational& rhs) {

       // divide

       Rational result(num * rhs.den, den * rhs.num);

       return result;

   }

   bool operator==(const Rational& rhs) {

       // check for equlity

       return (num == rhs.num && den == rhs.den);

   }

   bool operator<(const Rational& rhs) {

       // check for less than after making denominator equal

       int lcm = (den * rhs.den) / _gcd(den, rhs.den);

       return (lcm / den) * num < (lcm / rhs.den) * rhs.num;

   }

   bool operator>(const Rational& rhs) {

       // check for greater than after making denominator equal

       int lcm = (den * rhs.den) / _gcd(den, rhs.den);

       return (lcm / den) * num > (lcm / rhs.den) * rhs.num;

   }

   bool operator<=(const Rational& rhs) {

       // check for less than or equal to after making denominator equal       

       int lcm = (den * rhs.den) / _gcd(den, rhs.den);

       return (lcm / den) * num <= (lcm / rhs.den) * rhs.num;

   }

   bool operator>=(const Rational& rhs) {

       // check for greater than or equal to after making denominator equal       

       int lcm = (den * rhs.den) / _gcd(den, rhs.den);

       return (lcm / den) * num >= (lcm / rhs.den) * rhs.num;

   }

   bool operator!=(const Rational& rhs) {

       return !(*this == rhs);

   }

   // function for string representation

   string to_str() {

       return to_string(num) + "/" + to_string(den);

   }

   // function to return rational as double

   double eval() {

       return (double)num / (double)den;

   }

};

// main function to test out class

int main() {

   Rational r1(6, 10);

   cout << "r1 = " << r1.to_str() << endl;

   Rational r2(4, -6);

   cout << "r2 = " << r2.to_str() << endl;

   Rational r3 = r1 + r2;

   cout << "r1 + r2 = " << r3.to_str() << endl;

  

   r3 = r1 - r2;

   cout << "r1 - r2 = " << r3.to_str() << endl;

  

   r3 = r1 * r2;

   cout << "r1 * r2 = " << r3.to_str() << endl;

  

   r3 = r1 / r2;

   cout << "r1 / r2 = " << r3.to_str() << endl;

   cout << "r1 = " << r1.eval() << endl;

   cout << "r2 = " << r2.eval() << endl;

}


Code screenshot:

Sample output:

Add a comment
Know the answer?
Add Answer to:
C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....
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
  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program 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 h and would be stored in the object as 1 in the numerator...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers 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 is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...

  • Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile us...

    c++ format Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile using gt+ on the Computer Science Linux systems. If your code does not compile on CS machines you will get 0 for the assignment. Organize your files into folders by lab assignment. For this assignment, create a folder called Lab9. A. Read Chapter 18 B. Create a new class called RationalNumber to represent fractions. The class should have the following capabilities: 1) It should...

  • Rational Number *In Java* A rational number is one that can be expressed as the ratio...

    Rational Number *In Java* A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school. Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner: The inverse, or reciprocal of...

  • In C# programming. Create a fractions class that represents fractions in the form a/b Your class...

    In C# programming. Create a fractions class that represents fractions in the form a/b Your class should implement the following members: int Numerator int Denominator Fraction(int numerator, int denominator) ­ creates a new Fraction double ToDecimal() ­ returns the fraction as a double Fraction Add(Fraction f) ­ adds the fraction to the one passed in and simplifies the result Fraction Multiply(Fraction f) ­ multiplies the fraction by the one passed in and simplifies the result Fraction Simplify() ­ simplifies the...

  • Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a...

    Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program 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 2...

  • c++ Write a rational number class. A rational number is a number that can be written...

    c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...

  • Make a new class called Rational to represent rational numbers in C++ language. Use a long...

    Make a new class called Rational to represent rational numbers in C++ language. Use a long to store each part (numerator and denominator). Be sure your main function fully test the class. 1. Create a helper function called gcd to calculate the greatest common divisor for the two given parameters. See Euclid’s algorithm (use google.com). Use this function to always store rationals in “lowest terms”. 2. Create a default, one argument and two argument constructor. For the two argument constructor,...

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

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

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
ADVERTISEMENT