Question

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, throw an exception if the denominator is zero.

3. Supply functions to support addition, subtraction, multiplication and division of two rational operands. After each operation, the rational value should be in “lowest terms” and the denominator positive.

4. Please note the the denominator must always be positive. Negative rationals should be stored with a negative numerator.

5. Implement the insertion operator. (rational values should be displayed as numerator / denominator). 6. Supply a complete program that exercises your class.

You must supply a listing of your program and sample output.

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

class DividedByZero : public exception
{
private:
public:
// Zero argumented constructor
DividedByZero()
{
}
virtual const char* what() const throw()
{
return "Denominator must not be zero";
}
}myex;


//Class
class Rational {
//Declaring variables
int numer;
int denom;
public:
Rational()
{
this->numer = 0;
this->denom = 1;
}
Rational(int num, int den);
Rational(int num);
//Declaring functions
Rational add(Rational rn);
Rational subtract(Rational rn);
Rational multiply(Rational rn);
Rational divide(Rational rn);
void printNumber();
void reduce(Rational& rn1);
int gcd(int num1,int num2);
//setter and getter function declarations
int getNumer();
void setNumer(int numer);
int getDenom();
void setDenom(int denom);
};
//Parameterized constructor having 2 arguments as parameters
Rational::Rational(int num, int den)
{
this->numer = num;
this->denom = den;
}
//Parameterized constructor having 1 argument as parameter
Rational::Rational(int num)
{
this->numer = num;
this->denom = 1;
}
//Setter and getter function implementations
void Rational::setNumer(int numer)
{
this->numer = numer;
}
int Rational::getNumer()
{
return numer;
}
void Rational::setDenom(int denom)
{
this->denom = denom;
}
int Rational::getDenom()
{
return denom;
}
//This method will reduce the rational number to smallest form
void Rational::reduce(Rational& rn1)
{
   int num = getNumer();
int den = getDenom();
if (num < 0 && den < 0) {
rn1.setNumer(num * -1);
rn1.setDenom(den * -1);
} else if (den < 0) {
           rn1.setNumer(num * -1);
rn1.setDenom(den * -1);
}

  
int temp = gcd(rn1.numer,rn1.denom);
//Reducing the numerator
rn1.setNumer(rn1.getNumer() / temp);
//Reducing the denominator
rn1.setDenom(rn1.getDenom() / temp);
}
//This function will calculates the gcd
int Rational::gcd(int num1,int num2)
{
if (num2 == 0) {
return num1;
}
return gcd(num2, num1 % num2);
}
//This method will add two rational numbers
Rational Rational::add(Rational rn)
{
Rational rn1;
int a, b, c, d;
a = getNumer();
b = getDenom();
c = rn.getNumer();
d = rn.getDenom();
rn1.setNumer((a * d + b * c));
rn1.setDenom((b * d));
reduce(rn1);
return rn1;
}
//This method will subtract two rational numbers
Rational Rational::subtract(Rational rn)
{
Rational rn1;
int a, b, c, d;
a = getNumer();
b = getDenom();
c = rn.getNumer();
d = rn.getDenom();
rn1.setNumer((a * d - b * c));
rn1.setDenom(b * d);
reduce(rn1);
return rn1;
}
//This method will multiply two rational numbers
Rational Rational::multiply(Rational rn)
{
Rational rn1;
int a, b, c, d;
a = getNumer();
b = getDenom();
c = rn.getNumer();
d = rn.getDenom();
rn1.setNumer(a * c);
rn1.setDenom(b * d);
reduce(rn1);
return rn1;
}
//This method will divide two rational numbers
Rational Rational::divide(Rational rn)
{
Rational rn1;
int a, b, c, d;
a = getNumer();
b = getDenom();
c = rn.getNumer();
d = rn.getDenom();
rn1.setNumer(a * d);
rn1.setDenom(c * b);
reduce(rn1);
return rn1;
}
//This function will display the rational number
void Rational::printNumber()
{
   if(getNumer()==1 && getDenom()==1)
   {
       cout << "(" << getNumer() << ")";
   }
   else
   {
       cout << "(" << getNumer() << "/" << getDenom() << ")";  
   }
}
int main()
{
int choice, a, b;
char ch, ch1;
cout << "THIS PROGRAM PERFORMS ARITHMETIC OPERATIONS ON RATIONAL NUMBERS:" << endl;
cout << "****************************************************************" << endl;
while(true)
{
while (true) {
   try
   {
   cout << "\nPlease enter the first rational number :";
cin >> a >> ch >> b;
if (b == 0) {
throw myex;
continue;
}
else
break;  
           }
           catch(DividedByZero& myex)
           {
               cout << myex.what() << endl;
               continue;

           }
}
Rational rn1(a, b);
while (true) {
   try
   {
   cout << "\nPlease enter the second rational number :";
cin >> a >> ch >> b;
if (b == 0) {
throw myex;
continue;
}
else
break;  
           }
           catch(DividedByZero& myex)
           {
               cout << myex.what() << endl;
               continue;

           }
}
Rational rn2(a, b);
cout << "\nOPERATIONS" << endl;
cout << "\t(1) ADDITION" << endl;
cout << "\t(2) SUBTRACTION" << endl;
cout << "\t(3) MULTIPLICATION" << endl;
cout << "\t(4) DIVISION" << endl;
cout << "Please select an operation (1,2,3 or 4):";
cin >> choice;
switch (choice) {
case 1: {
Rational res = rn1.add(rn2);
rn1.printNumber();
cout << " + ";
rn2.printNumber();
cout << " = ";
res.printNumber();
cout << endl;
break;
}
case 2: {
Rational res = rn1.subtract(rn2);
rn1.printNumber();
cout << " - ";
rn2.printNumber();
cout << " = ";
res.printNumber();
cout << endl;
break;
}
case 3: {
Rational res = rn1.multiply(rn2);
rn1.printNumber();
cout << " * ";
rn2.printNumber();
cout << " = ";
res.printNumber();
cout << endl;
break;
}
case 4: {
  
Rational res = rn1.divide(rn2);
rn1.printNumber();
cout << " / ";
rn2.printNumber();
cout << " = ";
res.printNumber();
cout << endl;
break;
}
default: {
cout << "** Invalid Choice **" << endl;
continue;
}
}
cout << "\nWould you like to run this program again (y/n) ?";
cin >> ch1;
if (ch1 == 'y' || ch1 == 'Y') {
continue;
}
else {
break;
}
}
return 0;
}

____________________________

Output:

___________________Thank You

Add a comment
Know the answer?
Add Answer to:
Make a new class called Rational to represent rational numbers in C++ language. Use a long...
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
  • 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 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...

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

  • (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...

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

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

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

  • 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: 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. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number...

    C++ and/or using Xcode... Define a class of 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 could produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call...

  • C++...please help follow exact steps output needs to be the exact same Create a class called...

    C++...please help follow exact steps output needs to be the exact same Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass 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...

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