Write a Rational class. 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.     #include #include #include   using namespace std;   class rational { public: /* default constructor set the rational number to 0, (i.e., numerator is 0, denominator is 1) */ rational();   /* Set the invoking object's value from user input */ void input();   /* Display invoking object's value in the standard output, in the form of numerator/denominator */ void output() const;   //return the invoking object's numerator int get_numerator() const;   //return the invoking object's denominator int get_denominator() const;   // Set invoking object to be the sum of op1 and op2 void Add(const rational& op1, const rational& op2);   // Set the invoking object to the difference of op1 and op2. void Subtract(const rational& op1, const rational& op2);   // Set the invoking object to the product of op1 and op2. void Multiply(const rational& op1, const rational& op2);   // Set the invoking object to the Quotient of op1 and op2. void Divide(const rational& op1, const rational& op2);   private: int numerator; int denominator; };   int main() { string binary_ops{"+-*/"}; rational op1, result; char oper;   cout << "Enter op1 (in the format of p/q): "; result.input();   //Test rational class member function do { cout << "\nEnter operator [+, -, /, *, =, c(lear), a(ccessors), q(uit)]: "; cin >> oper;   // Only get operand if needed if (binary_ops.find(oper) != string::npos) { cout << "\nEnter op2 (in the format of p/q): "; op1.input(); }   switch (oper) { case '+': result.Add(result, op1); break; case '-': result.Subtract(result, op1); break; case '*': result.Multiply(result, op1); break; case '/': result.Divide(result, op1); break; case '=': cout << "result = "; result.output(); break; case 'c': result = rational(); cout << "\nEnter op1 (in the format of p/q): "; result.input(); break; case 'a': cout << "\nresult's numerator is: " << result.get_numerator(); cout << "\nresult's denominator is: " << result.get_denominator(); break; case 'q': break; default: break; }   } while (oper != 'q'); return 0; }     //Definitions of all member functions   rational::rational() { numerator = 0; denominator = 1; }     /* Set the invoking object's value from user input */ void rational::input() { char c; int top, bottom;   // keep reading until valid input is entered cin >> top >> c >> bottom; if (denominator <= 0) { cout << "Invalid rational value."; exit(1); } // set the invoking object's numerator, denominator numerator = top; denominator = bottom; }   /* Display invoking object's value in the standard output, in the form of numerator/denominator */ void rational::output() const { cout << numerator << "/" << denominator; }   //return the invoking object's numerator int rational::get_numerator() const { return numerator; }   //return the invoking object's denominator int rational::get_denominator() const { return denominator; }   // Set invoking object to be the sum of op1 and op2 void rational::Add(const rational& op1, const rational& op2) { numerator = (op1.numerator * op2.denominator + op2.numerator*op1.denominator); denominator = op1.denominator*op2.denominator; }   // Set invoking object to be the sum of op1 and op2 void rational::Subtract(const rational& op1, const rational& op2) { numerator = (op1.numerator * op2.denominator - op2.numerator*op1.denominator); denominator = op1.denominator*op2.denominator; }   // Set invoking object to be the sum of op1 and op2 void rational::Multiply(const rational& op1, const rational& op2) { numerator = (op1.numerator * op2.numerator); denominator = op1.denominator*op2.denominator; }   // Set invoking object to be the sum of op1 and op2 void rational::Divide(const rational& op1, const rational& op2) { numerator = (op1.numerator * op2.denominator); denominator = op1.denominator*op2.numerator; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a Rational class. 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.

 

 

#include <iostream>

#include <string>

#include <stdlib.h>

 

using namespace std;

 

class rational

{

public:

/* default constructor set the rational number to 0, (i.e., numerator is 0, denominator is 1) */

rational();

 

/* Set the invoking object's value from user input */

void input();

 

/* Display invoking object's value in the standard output, in the form of numerator/denominator */

void output() const;

 

//return the invoking object's numerator

int get_numerator() const;

 

//return the invoking object's denominator

int get_denominator() const;

 

// Set invoking object to be the sum of op1 and op2

void Add(const rational& op1, const rational& op2);

 

// Set the invoking object to the difference of op1 and op2.

void Subtract(const rational& op1, const rational& op2);

 

// Set the invoking object to the product of op1 and op2.

void Multiply(const rational& op1, const rational& op2);

 

// Set the invoking object to the Quotient of op1 and op2.

void Divide(const rational& op1, const rational& op2);

 

private:

int numerator;

int denominator;

};

 

int main()

{

string binary_ops{"+-*/"};

rational op1, result;

char oper;

 

cout << "Enter op1 (in the format of p/q): ";

result.input();

 

//Test rational class member function

do {

cout << "\nEnter operator [+, -, /, *, =, c(lear), a(ccessors), q(uit)]: ";

cin >> oper;

 

// Only get operand if needed

if (binary_ops.find(oper) != string::npos) {

cout << "\nEnter op2 (in the format of p/q): ";

op1.input();

}

 

switch (oper) {

case '+':

result.Add(result, op1);

break;

case '-':

result.Subtract(result, op1);

break;

case '*':

result.Multiply(result, op1);

break;

case '/':

result.Divide(result, op1);

break;

case '=':

cout << "result = ";

result.output();

break;

case 'c':

result = rational();

cout << "\nEnter op1 (in the format of p/q): ";

result.input();

break;

case 'a':

cout << "\nresult's numerator is: " << result.get_numerator();

cout << "\nresult's denominator is: " << result.get_denominator();

break;

case 'q':

break;

default:

break;

}

 

} while (oper != 'q');

return 0;

}

 

 

//Definitions of all member functions

 

rational::rational()

{

numerator = 0;

denominator = 1;

}

 

 

/* Set the invoking object's value from user input */

void rational::input()

{

char c;

int top, bottom;

 

// keep reading until valid input is entered

cin >> top >> c >> bottom;

if (denominator <= 0) {

cout << "Invalid rational value.";

exit(1);

}

// set the invoking object's numerator, denominator

numerator = top;

denominator = bottom;

}

 

/* Display invoking object's value in the standard output, in the form of numerator/denominator */

void rational::output() const

{

cout << numerator << "/" << denominator;

}

 

//return the invoking object's numerator

int rational::get_numerator() const

{

return numerator;

}

 

//return the invoking object's denominator

int rational::get_denominator() const

{

return denominator;

}

 

// Set invoking object to be the sum of op1 and op2

void rational::Add(const rational& op1, const rational& op2)

{

numerator = (op1.numerator * op2.denominator + op2.numerator*op1.denominator);

denominator = op1.denominator*op2.denominator;

}

 

// Set invoking object to be the sum of op1 and op2

void rational::Subtract(const rational& op1, const rational& op2)

{

numerator = (op1.numerator * op2.denominator - op2.numerator*op1.denominator);

denominator = op1.denominator*op2.denominator;

}

 

// Set invoking object to be the sum of op1 and op2

void rational::Multiply(const rational& op1, const rational& op2)

{

numerator = (op1.numerator * op2.numerator);

denominator = op1.denominator*op2.denominator;

}

 

// Set invoking object to be the sum of op1 and op2

void rational::Divide(const rational& op1, const rational& op2)

{

numerator = (op1.numerator * op2.denominator);

denominator = op1.denominator*op2.numerator;

}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education