In: Computer Science
C++ Program
Overload the following operators to work with the Rational class and add test cases in the driver program. Make sure your driver program now tests each of these symbols for your Rational Class.
+
–
*
/
==
!=
<
<=
>
>=
<< (stream insertion operator, use the toString
function)
>> (stream extraction operator)
Make sure you test all 12 operators
Example Run (Bold is input), everything else is
a print statement using the overloaded operators
Enter a rational number a/b >> c :
1/3
Enter another rational number c/d >> d:
2/4
c << 1/3
d << 1/2
1/3 + 1/2 = 5/6
1/3 - 1/2 = -1/6
1/3 * 1/2 = 1/6
1/3 / 1/2 = 2/3
1/3 > 1/2 is false
1/3 < 1/2 is true
1/3 >= 1/2 is false
1/3 <= 1/2 is true
1/3 == 1/2 is false
1/3 != 1/2 is true
/*** Rational.h ***/
#ifndef RATIONAL_H
#define RATIONAL_H
#include<iostream>
using namespace std;
class Rational
{
public:
Rational(int num=0, int denom=1);
//mutators / setters
void set(int num, int denom);
void set_num(int n);
void set_denom(int d);
//accessors / getters
int get_num() const;
int get_denom() const;
//will return object whose error state is set
// if one of the operands is in error.
friend Rational operator +(const Rational &num1, const Rational
&num2);
friend Rational operator -(const Rational &num1, const Rational
&num2);
friend Rational operator *(const Rational &num1, const Rational
&num2);
friend Rational operator /(const Rational &num1, const Rational
&num2);
friend bool operator ==(const Rational &num1, const Rational
&num2);
friend bool operator !=(const Rational &num1, const Rational
&num2);
friend bool operator <(const Rational &num1, const Rational
&num2);
friend bool operator >(const Rational &num1, const Rational
&num2);
friend bool operator <=(const Rational &num1, const Rational
&num2);
friend bool operator >=(const Rational &num1, const Rational
&num2);
//will print UNDEFINED if num is in error.
friend ostream& operator <<(ostream &outs, const
Rational &num);
friend istream& operator >>(istream &ins, Rational
&num);
void simplify();
private:
int _n; //numerator
int _d; //denominator
};
#endif // RATIONAL_H
/*** Rational.cpp ***/
#include <iostream>
#include "Rational.h"
using namespace std;
//sets digits numerator and denominator
Rational::Rational(int num, int denom) {
_n = num;
_d = denom;
}
void Rational::set(int num, int denom) {
_n = num;
_d = denom;
}
void Rational::set_num(int n) {
_n = n;
}
void Rational::set_denom(int d) {
_d = d;
}
int Rational::get_num() const {
return _n;
}
int Rational::get_denom() const {
return _d;
}
// //
//////////////////////operators//////////////////////////////
Rational operator +(const Rational &num1, const Rational
&num2) {
int n = num1._n*num2._d + num1._d*num2._n;
int d = num1._d*num2._d;
Rational r(n,d);
r.simplify();
return r;
}
Rational operator -(const Rational &num1, const Rational
&num2) {
int help = num2._n *-1;
int n = num1._n*num2._d + num1._d*help;
int d = num1._d*num2._d;
Rational r(n,d);
r.simplify();
return r;
}
Rational operator *(const Rational &num1, const Rational
&num2) {
int n = num1._n*num2._n;
int d = num1._d*num2._d;
Rational r(n,d);
r.simplify();
return r;
}
Rational operator /(const Rational &num1, const Rational
&num2) {
int n = num1._n*num2._d;
int d = num1._d*num2._n;
Rational r(n,d);
r.simplify();
return r;
}
ostream& operator <<(ostream &outs, const Rational
&num){
if(num.get_denom()>0)
outs<<num._n<<"/"<<num._d;
else if(num.get_denom()<0)
outs<<-num._n<<"/"<<-num._d;
else
outs<<"UNDEFINED";
return outs;
}
istream& operator >>(istream &ins, Rational
&num){
int n,d;
char slash;
ins>>n>>slash>>d;
num = Rational(n,d);
return ins;
}
// COMPARSON OPERATORS
bool operator ==(const Rational &num1, const Rational
&num2){
double compare = double(num1._n)/num1._d -
double(num2._n)/num2._d;
if(compare == 0) return true;
return false;
}
bool operator !=(const Rational &num1, const Rational
&num2){
double compare = double(num1._n)/num1._d -
double(num2._n)/num2._d;
if(compare != 0) return true;
return false;
}
bool operator <(const Rational &num1, const Rational
&num2){
double compare = double(num1._n)/num1._d -
double(num2._n)/num2._d;
if(compare < 0) return true;
return false;
}
bool operator <=(const Rational &num1, const Rational
&num2){
double compare = double(num1._n)/num1._d -
double(num2._n)/num2._d;
if(compare <= 0) return true;
return false;
}
bool operator >(const Rational &num1, const Rational
&num2) {
double compare = double(num1._n)/num1._d -
double(num2._n)/num2._d;
if(compare > 0) return true;
return false;
}
bool operator >=(const Rational &num1, const Rational
&num2) {
double compare = double(num1._n)/num1._d -
double(num2._n)/num2._d;
if(compare >= 0) return true;
return false;
}
int gcd(int a, int b)
{
if(a==0)
return b;
return gcd(b%a, a);
}
void Rational::simplify()
{
int g = gcd(_n, _d);
_n /= g;
_d /= g;
}
/*** main.cpp ***/
#include <iostream>
#include "rational.h"
using namespace std;
//main function
int main()
{
Rational a, c, d;
cout<<"Enter Rational number a/b: ";
cin>>c;
cout<<"Enter Rational number c/d: ";
cin>>d;
cout<<c<<endl;
cout<<d<<endl;
a = c + d;
cout<<c<<" + "<<d<< " =
"<<a<<endl;
a = c - d;
cout<<c<<" - "<<d<< " =
"<<a<<endl;
a = c * d;
cout<<c<<" * "<<d<< " =
"<<a<<endl;
a = c / d;
cout<<c<<" / "<<d<< " =
"<<a<<endl;
cout<<boolalpha;
bool f = c > d;
cout<<c<<" > "<<d<< " is
"<<f<<endl;
f = c < d;
cout<<c<<" < "<<d<< " is
"<<f<<endl;
f = c >= d;
cout<<c<<" >= "<<d<< " is
"<<f<<endl;
f = c <= d;
cout<<c<<" <= "<<d<< " is
"<<f<<endl;
f = c == d;
cout<<c<<" == "<<d<< " is
"<<f<<endl;
f = c != d;
cout<<c<<" != "<<d<< " is
"<<f<<endl;
return 0;
}
Output:
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.