Topic: Overloaded
Operators and Rational Numbers
Rational.h
Rational.cpp
Test.cpp
Constructor(s):
The user of the class should be able to create an instance of a rational number passing in 0-2 arguments. If no arguments are passed in the value should be 0, for example 0/1. If one argument is passed in, it should be considered a whole number and thus be represented over one in radical form, for example 5/1. If two numbers are passed in, the first should be considered the numerator the second the denominator. Be sure to reduce the fraction completely.
Unary Operators:
++ post and pre increment oerators. These operators should add one to the numerator. Be sure to reduce the resulting radical.
-- post and pre decrement operators. These operators should remove one from the numerator. Be sure to reduce the resulting radical.
+=, -=, *=, \= operators. These operators should modify the numerator appropriately according to the rValue passed in.
() function operator. Overload this operator to return a string in the form “n/d”.
double() type cast operator. Overload this operator to return a double for the numerator divided by the denominator.
Binary Operators:
+,-,*,/,=, and == should be overloaded. Each of these operators should work with whole numbers and other rational numbers. Be sure to reduce the resulting radical.
Non-member Binary Operators:
The stream insertion << and stream extraction >> operators should get and show the value of the radical.
The stream-extraction operator is only required to get the rational from the user in this form n/d
The stream insertion operator should show the radical with a forward slash ‘/’ appearing between the numerator and denominator with one exception. If the denominator is 1, only the numerator should be displayed.
Private Function(s):
A private reduce fraction and common denominator functions will be useful.
Watch out for negative values in both the numerator and denominator.
//Rational.h
#include<iostream>
#include<string>
using std::ostream;
using std::istream;
using std::string;
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational(long NewNumerator = 0, long NewDenominator = 1);
const Rational & operator=(const
Rational & rValue);
bool operator==(const Rational & rValue) const;
const Rational operator+ (const Rational & rValue) const;
const Rational operator- (const Rational & rValue) const;
const Rational operator* (const Rational & rValue) const;
const Rational operator/ (const Rational & rValue) const;
Rational& operator+= (const Rational & rValue);
Rational& operator-= (const Rational & rValue);
Rational& operator*= (const Rational & rValue);
Rational& operator/= (const Rational & rValue);
string operator()() const;
operator double()
const;
Rational &operator++();
Rational &operator--();
Rational operator++(int
Garbage);
Rational operator--(int Garbage);
long getNumerator()const;
long getDenominator()const;
private:
long m_Numerator;
long m_Denominator;
long LeastCommonMultiple(long x, long y) const;
long GreatestCommonDivisor(long x, long y) const;
void Reduce();
//any
other private functions you may need
};
//prototypes for
non-member methods
ostream &operator<<(ostream &, const
Rational & Fraction);
istream &operator>>(istream &, Rational &Fraction);
const Rational operator+ (const long lValue, const Rational & rValue);
const Rational operator- (const long lValue, const Rational & rValue);
const Rational operator* (const long lValue, const Rational & rValue);
const Rational operator/ (const long lValue, const Rational & rValue);
#endif
//Rational.cpp
#include"Rational.h"
#include<sstream>
Rational::Rational(long NewNumerator, long NewDenominator)
{
m_Numerator = NewNumerator;
m_Denominator = NewDenominator;
Reduce();
}
const Rational& Rational::operator=(const Rational & rValue)
{
m_Numerator = rValue.m_Numerator;
m_Denominator = rValue.m_Denominator;
Reduce();
return *this;
}
Rational& Rational::operator++()
{
m_Numerator += m_Denominator;
Reduce();
return *this;
}
Rational Rational::operator++(int Garbage)
{
Rational
result = *this;
m_Numerator += m_Denominator;
Reduce();
return result;
}
bool Rational::operator==(const Rational & rValue) const
{
bool
result = true;
if(m_Numerator
!= rValue.m_Numerator || m_Denominator
!= rValue.m_Denominator)
{
result = false;
}
return result;
}
const Rational Rational::operator+ (const Rational & rValue) const
{
long lcm = LeastCommonMultiple(rValue.m_Denominator,
m_Denominator);
long resultNumerator
= (m_Numerator * (lcm/m_Denominator))
+
(rValue.m_Numerator * (lcm/rValue.m_Denominator));
Rational
result(resultNumerator,lcm);
return result;
}
Rational& Rational::operator+= (const Rational & rValue)
{
*this = *this + rValue;
Reduce();
return *this;
}
string Rational::operator()() const
{
std::stringstream stream;
stream << m_Numerator
<< "/" << m_Denominator;
return stream.str();
}
long Rational::LeastCommonMultiple(long x, long y) const
{
bool Continue = true;
long Guess = x;
while (Continue)
{
if(Guess
% y == 0)
{
Continue = false;
}
else
{
Guess += x;
}
}
return Guess;
}
long Rational::GreatestCommonDivisor(long x, long y) const
{
long Remainder = x % y;
while(Remainder != 0)
{
x = y;
y = Remainder;
Remainder = x % y;
}
return y;
}
void Rational::Reduce()
{
long GCD = GreatestCommonDivisor(m_Numerator, m_Denominator);
m_Numerator = m_Numerator/GCD;
m_Denominator
= m_Denominator/GCD;
}
ostream &operator<<(ostream & out, const Rational & Fraction)
{
out << Fraction.getNumerator()
<< "\\" << Fraction.getDenominator();
return out;
}
long Rational::getNumerator()const
{
return m_Numerator;
}
long Rational::getDenominator()const
{
return m_Denominator;
}