// Program rational.cpp
#include <iostream>
#include <string>
#include "rational.h"
using namespace std;
// default constructor
Rational::Rational() {
SetNumerator(0);
SetDenominator(1);
}
// (numer, denom) constructor
Rational::Rational(int numer, int denom) {
SetNumerator(numer);
SetDenominator(denom);
}
// Rational: copy constructor
Rational::Rational(const Rational &r) {
int a = r.GetNumerator();
int b = r.GetDenominator();
SetNumerator(a);
SetDenominator(b);
}
// Rational: destructor
Rational::~Rational() {
// no body needed
}
// get the numerator
int Rational::GetNumerator() const {
return NumeratorValue;
}
// get the denominator
int Rational::GetDenominator() const {
return DenominatorValue;
}
// set the numerator
void Rational::SetNumerator(int numer) {
NumeratorValue = numer;
}
// set the denominator
void Rational::SetDenominator(int denom) {
if (denom != 0) {
DenominatorValue = denom;
}
else {
cerr << "Illegal denominator: " << denom
<< "using 1" << endl;
DenominatorValue = 1;
}
}
// adding Rationals
Rational Rational::Add(const Rational &r) const {
int a = GetNumerator();
int b = GetDenominator();
int c = r.GetNumerator();
int d = r.GetDenominator();
return Rational(a*d + b*c, b*d);
}
// subtracting Rationals
Rational Rational::Sub(const Rational &r) const {
int a = GetNumerator();
int b = GetDenominator();
int c = r.GetNumerator();
int d = r.GetDenominator();
return Rational(a*d - b*c, b*d);
}
// multiplying Rationals
Rational Rational::Multiply(const Rational &r) const {
int a = GetNumerator();
int b = GetDenominator();
int c = r.GetNumerator();
int d = r.GetDenominator();
return Rational(a*c, b*d);
}
// dividing Rationals
Rational Rational::Divide(const Rational &r) const {
int a = GetNumerator();
int b = GetDenominator();
int c = r.GetNumerator();
int d = r.GetDenominator();
return Rational(a*d, b*c);
}
// Rational: assignment operator
Rational& Rational::operator=(const Rational &r) {
int a = r.GetNumerator();
int b = r.GetDenominator();
SetNumerator(a);
SetDenominator(b);
return *this;
}
/*
void Rational::operator=(const Rational &r) {
int a = r.GetNumerator();
int b = r.GetDenominator();
SetNumerator(a);
SetDenominator(b);
return;
}
*/
// inserting a Rational
void Rational::Insert(ostream &sout) const {
sout << GetNumerator() << '/' << GetDenominator();
return;
}
// extracting a Rational
void Rational::Extract(istream &sin) {
int numer;
int denom;
char slash;
sin >> numer >> slash >> denom;
SetNumerator(numer);
SetDenominator(denom);
return;
}
// adding Rationals
Rational operator+(const Rational &r, const Rational &s) {
return r.Add(s);
}
// subtracting Rationals
Rational operator-(const Rational &r, const Rational &s) {
return r.Sub(s);
}
// multiplying Rationals
Rational operator*(const Rational &r, const Rational &s) {
return r.Multiply(s);
}
// dividing Rationals
Rational operator/(const Rational &r, const Rational &s) {
return r.Divide(s);
}
// inserting a Rational
ostream& operator<<(ostream &sout, const Rational &r) {
r.Insert(sout);
return sout;
}
// extracting a Rational
istream& operator>>(istream &sin, Rational &r) {
r.Extract(sin);
return sin;
}
int main() {
Rational r;
Rational s;
cout << "Enter 1st rational number (a/b): ";
cin >> r;
cout << "Enter 2nd rational number (a/b): ";
cin >> s;
Rational tr(r);
Rational ts = s;
Rational tts;
tts = s;
ts = tts = r;
Rational Sum = r + s;
Rational Difference = r - s;
Rational Product = r * s;
Rational Ratio = r / s;
cout << r << " + " << s << " = " << Sum << endl;
cout << r << " - " << s << " = " << Difference << endl;
cout << r << " * " << s << " = " << Product << endl;
cout << r << " / " << s << " = " << Ratio << endl;
return 0;
}