Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question
100%

Use C++

I need three files, one main.cpp file, one rational.cpp file and one rational.h file. I'll put the code for the main.cpp file and rational.h file below. No need to change them I just need the rational.cpp file.

Modify the code to have operator overloading functions with the following operators.
==, <, <=, >, >=, +, -, *, /
Use the Rational interface and main code in the next page.

main.cpp:

#include <iostream>
#include "rational.h"
int main()
{
    Rational r1, r2;
    char answer = 'y';
    while (answer == 'y') {
        cout << "Enter the first fraction (e.g. 3/4)  : "; cin >> r1;
        cout << "Enter the second fraction (e.g. 3/4) : "; cin >> r2;
        cout << "r1 : " << r1 << endl;
        cout << "r2 : " << r2 << endl << endl;
        cout << "r1 + r2 = " << r1 + r2 << endl;
        cout << "r1 - r2 = " << r1 - r2 << endl;
        cout << "r1 * r2 = " << r1 * r2 << endl;
        cout << "r1 / r2 = " << r1 / r2 << endl << endl;
        cout << "r1 == r2 -> " << (r1 == r2) << endl;
        cout << "r1 < r2  -> " << (r1 < r2) << endl;
        cout << "r1 <= r2 -> " << (r1 <= r2) << endl;
        cout << "r1 > r2  -> " << (r1 > r2) << endl;
        cout << "r1 >= r2 -> " << (r1 >= r2) << endl << endl;
        cout << "Again (y/n)? ";
        cin >> answer;
    }
    return 0;
}

rational.h:

#include<iostream>
#include<cstdlib>
using namespace std;
class Rational
{
public:
Rational();
Rational(int);
Rational(int, int);
friend istream& operator >>(istream&, Rational&);// input function
friend ostream& operator<<(ostream&, const Rational&);// output function
// Arithmetic operators (+, -, *, /)
friend Rational operator+(const Rational&, const Rational&);
friend Rational operator-(const Rational&, const Rational&);
friend Rational operator*(const Rational&, const Rational&);
friend Rational operator/(const Rational&, const Rational&);
// Relational operators (==, >, <, >=, <=)
friend bool operator==(const Rational&, const Rational&);
friend bool operator>(const Rational&, const Rational&);
friend bool operator<(const Rational&, const Rational&);
friend bool operator>=(const Rational&, const Rational&);
friend bool operator<=(const Rational&, const Rational&);

private:
int numerator;
int denominator;
void simplify();
};

make the code run the same as in the picture

Enter the first fraction (e.g. 3/4): 3/5  
Enter the second fraction (e.g. 3/4): 2/6  
r1: 3/5  
r2: 1/3  

- r1 + r2 = 14/15  
- r1 - r2 = 4/15  
- r1 * r2 = 1/5  
- r1 / r2 = 9/5  

- r1 == r2 -> 0  
- r1 != r2 -> 1  
- r1 >= r2 -> 1  
- r1 <= r2 -> 0  
- r1 > r2 -> 1  
- r1 < r2 -> 0  

Again (y/n)? y  
Enter the first fraction (e.g. 3/4): 4/7  
Enter the second fraction (e.g. 3/4): 1/2  
r1: 4/7  
r2: 1/2  

- r1 + r2 = 15/14  
- r1 - r2 = 1/14  
- r1 * r2 = 2/7  
- r1 / r2 = 8/7  

- r1 == r2 -> 0  
- r1 != r2 -> 1  
- r1 >= r2 -> 1  
- r1 <= r2 -> 0  
- r1 > r2 -> 1  
- r1 < r2 -> 0  

Again (y/n)? n
expand button
Transcribed Image Text:Enter the first fraction (e.g. 3/4): 3/5 Enter the second fraction (e.g. 3/4): 2/6 r1: 3/5 r2: 1/3 - r1 + r2 = 14/15 - r1 - r2 = 4/15 - r1 * r2 = 1/5 - r1 / r2 = 9/5 - r1 == r2 -> 0 - r1 != r2 -> 1 - r1 >= r2 -> 1 - r1 <= r2 -> 0 - r1 > r2 -> 1 - r1 < r2 -> 0 Again (y/n)? y Enter the first fraction (e.g. 3/4): 4/7 Enter the second fraction (e.g. 3/4): 1/2 r1: 4/7 r2: 1/2 - r1 + r2 = 15/14 - r1 - r2 = 1/14 - r1 * r2 = 2/7 - r1 / r2 = 8/7 - r1 == r2 -> 0 - r1 != r2 -> 1 - r1 >= r2 -> 1 - r1 <= r2 -> 0 - r1 > r2 -> 1 - r1 < r2 -> 0 Again (y/n)? n
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education