I'm lost based on the code provided, how to reduce a fraction down. I've looked on the internet and yes most are using global variables; I however, can not for my class. It must be within the function. And there needs to be two calls outside of each of the operators to the reduce() function. Where am I going wrong?
#include <iostream>
using namespace std;
#include "Rational.h"
// the setNumerator function sets the numerator value for the object.
void Rational::setNumerator (int numer)
{
numeratorValue = numer;
}
// setDenominator is responsible for changing the denominatorValue data
// member. It does some basic error checking to make sure we don.t try
// to set a 0 as the denominator. Our solution to this is to set the
// denom to 1 after outputting an error message
void Rational::setDenominator (int denom)
{
if (denom != 0)
{
denominatorValue = denom;
}
else
{
cerr << "Illegal denominator. Using 1 instead" << endl;
denominatorValue = 1;
}
return;
}
// specific constructor, called when one or two arguments are given
// in object instantiation. Sets rational to appropriate value
Rational::Rational (int numer, int denom)
{
setNumerator(numer);
setDenominator(denom);
reduce();
}
// getNumerator and getDenominator are simple, private inspector
// member functions that return the values of the numerator and
// denominator for our object.
int Rational::getNumerator() const
{
return numeratorValue;
}
int Rational::getDenominator() const
{
return denominatorValue;
}
// add() performs addition of two Rational objects. One object
// is doing the addition, the other object (the argument) is being
// added. This function creates a very (very!) temporary third
// Rational object as it.s setting itself up to return a value. It
// returns the newly created Rational object. This is one of
// the few times you see a constructor called explicitly.
Rational Rational::add (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
Rational result(a*d + b*c, b*d);
result.reduce();
return result;
}
// multiply() performs multiplying two Rational objects. One object
// is doing the multiplying, the other object (the argument) is being
// multiplied. This function creates a very (very!) temporary third
// Rational object as it.s setting itself up to return a value. It
// returns the newly created Rational object. This is one of
// the few times you see a constructor called explicitly.
Rational Rational::multiply (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
Rational result(a*c, b*d);
result.reduce();
return result;
}
// insert() takes an ostream as the only argument. An ostream
// is an output stream, like cout. by making this an argument, you
// could output Rational numbers to files or other devices or streams.
void Rational::insert (ostream &streamout) const
{
streamout << getNumerator() << "/" << getDenominator();
return;
}
// extract() takes an istream as the only argument. An istream
// is an input stream, like cin. by making this an argument, you
// could input Rational numbers from files or other devices or
// streams.
void Rational::extract (istream &streamin)
{
int numer;
int denom;
char slash;
streamin >> numer >> slash >> denom;
setNumerator(numer);
setDenominator(denom);
return;
}
// Here we implement the + operator. all this "function" does is end
// up calling the add() behavior of the rational number class.
// by overloading the operator, we are providing the .natural
// interface. for the object that we are looking for to make working
// with Rational objects intuitive.
Rational operator+ (const Rational &left, const Rational &right)
{
return left.add(right);
}
// and here is the implementation for the * operator. We simply end up
// calling the multiply() behavior of a rational object.
Rational operator* (const Rational &left, const Rational &right)
{
return left.multiply(right);
}
// here is the implementation for the << operator. We simply end
// up calling the insert() behavior of a rational object. Because
// of the "nature of output" in C++ (to be explained in more detail in 1620)
// we need to return the ostream out of the function. This is
// a critical step in overloading the << operator.
ostream& operator<< (ostream &output, const Rational &rat)
{
rat.insert(output);
return output;
}
istream& operator>> (istream &input, Rational &rat)
{
rat.extract(input);
return input;
}
Rational Rational::subtract (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
Rational result(a*d - b*c, b*d);
result.reduce();
return result;
}
Rational operator- (const Rational &left, const Rational &right)
{
return left.subtract(right);
}
Rational Rational::divide (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
Rational result(a*d, b*c);
result.reduce();
return result;
}
Rational operator/ (const Rational &left, const Rational &right)
{
return left.divide(right);
}
bool Rational::lessThan (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
if (a*d < c*b)
return true;
else
return false;
}
bool Rational::lessThanEqual (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
if ( a*d <= c*b)
return true;
else
return false;
}
bool Rational::greaterThan (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
if (a*d > c*b)
return true;
else
return false;
}
bool Rational::greaterThanEqual (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
if (a*d >= c*b)
return true;
else
return false;
}
bool Rational::equalTo (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
if ((a==c) && (b==d))
return true;
else
return false;
}
bool Rational::notEqualTo (const Rational &r) const
{
int a = getNumerator();
int b = getDenominator();
int c = r.getNumerator();
int d = r.getDenominator();
if ((a!=c) || (b!=d))
return true;
else
return false;
}
void Rational::reduce()
{
int x = getNumerator();
int a;
x = a;
int y = getDenominator();
int b;
y = b;
int i;
while (i = (a % b))
{
a = b;
b = i;
}
x /= b;
y /= b;
}
bool operator< (const Rational &left, const Rational &right)
{
return left.lessThan(right);
}
bool operator<= (const Rational &left, const Rational &right)
{
return left.lessThanEqual(right);
}
bool operator> (const Rational &left, const Rational &right)
{
return left.greaterThan(right);
}
bool operator>= (const Rational &left, const Rational &right)
{
return left.greaterThanEqual(right);
}
bool operator== (const Rational &left, const Rational &right)
{
return left.equalTo(right);
}
bool operator!= (const Rational &left, const Rational &right)
{
return left.notEqualTo(right);
}