Use C++ Modify the code to have operator overloading functions with the following operators. ==,  <,  <=,  >,  >=,  +,  -,  *,  / Use the Rational interface and main code in the next page. I need three files one main.cpp, one rational.cpp file and one rational.h file.  Main file and h file are shown on the picture they don't need to be changed I just need rational.cpp file. Here is the rational.cpp file I have: #include "rational.h" Rational::Rational() { numerator = 0; denominator = 1; } Rational::Rational(int num, int den) { numerator = num; denominator = den; } istream& operator >>(istream& take, Rational& r1) { string userInput = " "; string num = " "; string den = " "; bool findSlash = false; take >> userInput; for (int i = 0; i < userInput.length(); i++) { if (userInput[i] != '/' && !findSlash) { num = num + userInput[i]; } else if (userInput[i] == '/') { findSlash = true; } else if (userInput[i] != '/' && !findSlash) { den = den + userInput[i]; } } r1.numerator = stoi(num); r1.denominator = stoi(den); return take; } ostream& operator<<(ostream& out, const Rational& r1) { Rational temp(r1.numerator, r1.denominator); temp.simplify(); out << temp.numerator << "/" << temp.denominator; return out; } Rational operator+(const Rational&f, const Rational&r) { Rational temp((f.numerator * r.denominator + r.numerator * f.denominator), (f.denominator * r.denominator)); return temp; } Rational operator-(const Rational&f, const Rational&r) { Rational temp((f.numerator * r.denominator - f.denominator * r.numerator), (f.denominator * r.denominator)); return temp; } Rational operator*(const Rational&f, const Rational&r) { Rational temp((f.numerator * r.numerator), (f.denominator * r.denominator)); return temp; } Rational operator/(const Rational&f, const Rational&r) { Rational temp((f.numerator * r.denominator), (f.denominator * r.numerator)); return temp; } bool operator==(const Rational& f, const Rational& r) { } bool operator>(const Rational&, const Rational&) { } bool operator<(const Rational&, const Rational&) { } bool operator>=(const Rational&, const Rational&) { } bool operator<=(const Rational&, const Rational&) { } void Rational::simplify() { }

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
100%

Use C++

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

I need three files one main.cpp, one rational.cpp file and one rational.h file.  Main file and h file are shown on the picture they don't need to be changed I just need rational.cpp file.

Here is the rational.cpp file I have:

#include "rational.h"
Rational::Rational()
{
numerator = 0;
denominator = 1;
}

Rational::Rational(int num, int den)
{
numerator = num;
denominator = den;
}

istream& operator >>(istream& take, Rational& r1)
{
string userInput = " ";
string num = " ";
string den = " ";
bool findSlash = false;
take >> userInput;
for (int i = 0; i < userInput.length(); i++)
{
if (userInput[i] != '/' && !findSlash)
{
num = num + userInput[i];
}
else if (userInput[i] == '/')
{
findSlash = true;
}
else if (userInput[i] != '/' && !findSlash)
{
den = den + userInput[i];
}
}
r1.numerator = stoi(num);
r1.denominator = stoi(den);
return take;

}

ostream& operator<<(ostream& out, const Rational& r1)
{
Rational temp(r1.numerator, r1.denominator);
temp.simplify();
out << temp.numerator << "/" << temp.denominator;
return out;

}

Rational operator+(const Rational&f, const Rational&r)
{
Rational temp((f.numerator * r.denominator + r.numerator * f.denominator), (f.denominator * r.denominator));
return temp;
}

Rational operator-(const Rational&f, const Rational&r)
{
Rational temp((f.numerator * r.denominator - f.denominator * r.numerator), (f.denominator * r.denominator));
return temp;
}

Rational operator*(const Rational&f, const Rational&r)
{
Rational temp((f.numerator * r.numerator), (f.denominator * r.denominator));
return temp;
}

Rational operator/(const Rational&f, const Rational&r)
{
Rational temp((f.numerator * r.denominator), (f.denominator * r.numerator));
return temp;
}

bool operator==(const Rational& f, const Rational& r)
{

}

bool operator>(const Rational&, const Rational&)
{

}

bool operator<(const Rational&, const Rational&)
{

}

bool operator>=(const Rational&, const Rational&)
{

}

bool operator<=(const Rational&, const Rational&)
{

}

void Rational::simplify()
{

}

 

 

#include<iostream>
#include<cstdlib>
using namespace std;
class Rational
{
public:
Rational();
Rational (int);
Rational (int, int);
};
rational.h
friend istream& operator>>(istream&, Rationals);// 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&);
private:
// Relational operators (==, >, < >, <=)
friend bool operator==(const Rationals, 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&);
CS DOD LABES
int numerator;
int denominator;
void simplify(); // make 2/4 --> 1/2
Transcribed Image Text:#include<iostream> #include<cstdlib> using namespace std; class Rational { public: Rational(); Rational (int); Rational (int, int); }; rational.h friend istream& operator>>(istream&, Rationals);// 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&); private: // Relational operators (==, >, < >, <=) friend bool operator==(const Rationals, 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&); CS DOD LABES int numerator; int denominator; void simplify(); // make 2/4 --> 1/2
int main()
{
Rational r1, 2;
char answer = 'y';
while (answer == 'y') {
cout << "Enter the first fraction (e.g. 3/4)
}
::cin>> rl;
cout<<"Enter the second fraction (e.g. 3/4) ; cin >> r2;
cout << "r1: << r1 << endl;
cout << "r2 = " << r2 << endl << endl;
+ x2 << endl;
cout << "rl + r2 == << r1
cout << "r1
cout << "r1
r2 = << r1 r2 << endl;
r2 == << r1 r2 << endl;
cout << "rl / r2 ==
cout << "r1 == x2 >
cout << "r1 < r2 →>
cout << "r1 <= r2>
cout << "r1 r2 ->
cout << "rl >= r2>
cout << "Again (y/n)? ";
cin >> answer;
return 0;
Enter the first fraction (eg 3/4): 3/5
Enter the second fraction (e. g. 3/4): 2/6
1: 3/5
2 1/3
1+12=14/15
1-12 4/15
r1 r2 = 1/5
1/ x2 = 9/5
r1 = 12-0
1 (12 -> 0
12-0
1 12
12 -> 1
1
Fi
<< rl/r2 << endl << endl;
<< (r1== r2) << endl;
<< (r1 < x2) << endl;
<< (r1 <= r2) << endl;
<< (r1 r2) << endl;
<< (r1 >= r2) << endl << endl;
Again (y/n)? y
Enter the first fraction (e.g. 3/4 : 4/7
Enter the second fraction (e. g 3/4): 1/2
14/7
2 1/2
1+12= 15/14
1-12 1/14
r1 r2 = 2/7
1/12 = 8/7
12-0
1 (12
0
r1
r2 0
r1 r2 - 1
r1 r21
Again (y/n)? a
Transcribed Image Text:int main() { Rational r1, 2; char answer = 'y'; while (answer == 'y') { cout << "Enter the first fraction (e.g. 3/4) } ::cin>> rl; cout<<"Enter the second fraction (e.g. 3/4) ; cin >> r2; cout << "r1: << r1 << endl; cout << "r2 = " << r2 << endl << endl; + x2 << endl; cout << "rl + r2 == << r1 cout << "r1 cout << "r1 r2 = << r1 r2 << endl; r2 == << r1 r2 << endl; cout << "rl / r2 == cout << "r1 == x2 > cout << "r1 < r2 →> cout << "r1 <= r2> cout << "r1 r2 -> cout << "rl >= r2> cout << "Again (y/n)? "; cin >> answer; return 0; Enter the first fraction (eg 3/4): 3/5 Enter the second fraction (e. g. 3/4): 2/6 1: 3/5 2 1/3 1+12=14/15 1-12 4/15 r1 r2 = 1/5 1/ x2 = 9/5 r1 = 12-0 1 (12 -> 0 12-0 1 12 12 -> 1 1 Fi << rl/r2 << endl << endl; << (r1== r2) << endl; << (r1 < x2) << endl; << (r1 <= r2) << endl; << (r1 r2) << endl; << (r1 >= r2) << endl << endl; Again (y/n)? y Enter the first fraction (e.g. 3/4 : 4/7 Enter the second fraction (e. g 3/4): 1/2 14/7 2 1/2 1+12= 15/14 1-12 1/14 r1 r2 = 2/7 1/12 = 8/7 12-0 1 (12 0 r1 r2 0 r1 r2 - 1 r1 r21 Again (y/n)? a
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Datatypes
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
  • SEE MORE 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