Z
Zenon
Folks,
I am having a terrible time overloading operators. I have tried what
I thought was the correct way, I tried the cheating (friend
declarations), all to no avail. Sorry for posting tons of code but I
really need help.
thanks,
Zenon
#ifndef Rational__hpp
#define Rational__hpp
#include<iostream>
#include<iomanip>
#include <cstring>
#include <cmath>
#include <sstream>
#include <fstream>
class Rational
{
// Define data members
int numerator;
int denominator;
// define function prototypes
public:
Rational::Rational(int num, int denom); // Creates a fraction
Rational::Rational(); // Default constructor
Rational::~Rational(); // Default destructor
void Rational::signAdjuster();
int Rational::reducer(); // helper function to reduce fracion if
possible
int Rational::reducer(int numerator, int denominator); // helper
function to reduce fracion if possible
Rational Rational:perator += (const Rational& fraction);
Rational Rational:perator -= (const Rational& fraction);
Rational Rational:perator *= (const Rational& fraction);
Rational Rational:perator /= (const Rational& fraction);
friend const Rational operator << (std:stream& os, const Rational&
fraction);
friend const Rational operator + (const Rational& lhs, const
Rational& rhs);
friend const Rational operator - (const Rational& lhs, const
Rational& rhs);
friend const Rational operator * (const Rational& lhs, const
Rational& rhs);
friend const Rational operator / (const Rational& lhs, const
Rational& rhs);
// friend const Rational operator << (std:stream& os, const
Rational& fraction);
// Rational Rational:perator + (const Rational& lhs, const Rational&
rhs);
// Rational Rational:perator + (const Rational& lhs, const Rational&
rhs);
/* Rational:perator - (const Rational lhs, const Rational rhs);
Rational:perator * (const Rational lhs, const Rational rhs);
Rational:perator / (const Rational lhs, const Rational rhs);
*/
Rational:perator double(); // Typecast to double
Rational::string(); // Typecast to string
/* bool Rational::get(istream& is);
void Rational:ut(ostream& os);*/
};
//std:stream& operator<< (std:stream& os, const Rational&
fraction); // Output
std::istream& operator>> (std::istream& is, const Rational& fraction);
// Input
#endif
#include <cstring>
#include <cmath>
#include<iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include "Rational.hpp"
using namespace std;
Rational::Rational(int num, int denom) // Set a fractions numerator
and denominator
{
numerator = num;
denominator = denom;
cout << "constructor numerator " + num ;
signAdjuster();
}
Rational::Rational() {} // Default constructor
Rational::~Rational() {} // Destructor
// std:perator <<(std:stream& os, const Rational& fraction); //
Output
/* Rational:stream& operator <<(std:stream& os, const Rational&
fraction) // Output
{
os << setw(0) << numerator << " / " << denominator << "\n";
// fraction.put(os);
// Rational:ut(os);
}*/
/* std::istream& operator >>(std::istream& is, const Rational&
fraction) // Input
{
fraction.get(is);
}*/
Rational Rational:perator += (const Rational& fraction)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fraction.numerator * denominator;
tempDenom = fraction.denominator * denominator;
tempNumTwo = numerator * fraction.denominator;
numerator = tempNumOne + tempNumTwo;
denominator = tempDenom;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return *this;
}
Rational Rational:perator -= (const Rational& fraction)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fraction.numerator * denominator;
tempDenom = fraction.denominator * denominator;
tempNumTwo = numerator * fraction.denominator;
numerator = tempNumOne - tempNumTwo;
denominator = tempDenom;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return *this;
}
Rational Rational:perator *= (const Rational& fraction)
{
numerator *= fraction.numerator;
denominator *= fraction.denominator;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return *this;
}
Rational Rational:perator /= (const Rational& fraction)
{
numerator *= fraction.denominator;
denominator *= fraction.numerator;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return *this;
}
const Rational operator + (const Rational& fractionLeft, const
Rational& fractionRight)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fractionLeft.numerator * fractionRight.denominator;
tempDenom = fractionLeft.denominator * fractionRight.denominator;
tempNumTwo = fractionRight.numerator * fractionLeft.denominator;
int numerator = tempNumOne + tempNumTwo;
int denominator = tempDenom;
Rational obj;
int divisor = obj.reducer(numerator, denominator);
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return Rational(numerator, denominator);
}
const Rational operator - (const Rational& fractionLeft, const
Rational& fractionRight)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fractionLeft.numerator * fractionRight.denominator;
tempDenom = fractionLeft.denominator * fractionRight.denominator;
tempNumTwo = fractionRight.numerator * fractionLeft.denominator;
int numerator = tempNumOne - tempNumTwo;
int denominator = tempDenom;
Rational obj;
int divisor = obj.reducer(numerator, denominator);
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return Rational(numerator, denominator);
}
const Rational operator * (const Rational& fractionLeft, const
Rational& fractionRight)
{
int numerator = fractionLeft.numerator * fractionRight.numerator;
int denominator = fractionLeft.denominator *
fractionRight.denominator;
Rational obj;
int divisor = obj.reducer(numerator, denominator);
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return Rational(numerator, denominator);
}
const Rational operator / (const Rational& fractionLeft, const
Rational& fractionRight)
{
int numerator = fractionLeft.numerator * fractionRight.denominator;
int denominator = fractionLeft.denominator *
fractionRight.numerator;
Rational obj;
int divisor = obj.reducer(numerator, denominator);
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return Rational(numerator, denominator);
}
const Rational operator << (std:stream& os, const Rational&
fraction)
{
cout << "fraction.numerator " + fraction.numerator << "\n";
cout << fraction.numerator + " / " + fraction.denominator;
return Rational(fraction.numerator, fraction.denominator);
}
/* Rational:perator - (const Rational& fractionLeft, const Rational&
fractionRight)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fractionLeft.numerator * fractionRight.denominator;
tempDenom = fractionLeft.denominator * fractionRight.denominator;
tempNumTwo = fractionRight.numerator * fractionLeft.denominator;
numerator = tempNumOne - tempNumTwo;
denominator = tempDenom;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
}*/
/* Rational:perator * (const Rational& fractionLeft, const Rational&
fractionRight)
{
numerator = fractionLeft.numerator * fractionRight.numerator;
denominator = fractionLeft.denominator * fractionRight.denominator;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
}*/
/* Rational:perator / (const Rational& fractionLeft, const Rational&
fractionRight)
{
numerator = fractionLeft.numerator * fractionRight.denominator;
denominator = fractionLeft.denominator * fractionRight.numerator;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
}
*/
Rational:perator double() // Typecast to double
{
return double (numerator/denominator);
}
Rational::string() // Typecast to string
{
return (char (numerator) + char(" / ") + char (denominator)) ;
}
int Rational::reducer() // helper function to reduce fraction if
possible
{
int small = 0;
int num = 0;
int den = 0;
num = numerator;
den = denominator;
if (num == 0)
return den;
if (den == 0)
return num;
// Only absolute value is important
if (den < 0)
den = den * -1;
if (num < 0)
num = num * -1;
// Smaller of the two becomes small
if (num > den)
small = den;
else
small = num;
// Continue till no remainder
while (num % small != 0 || den % small != 0)
{
if (num > small)
{
num = num - (num % small);
small = num;
}
else
{
den = den - (den % small);
small = den;
}
}
// Return thr divisor which leaves no remainder
return small;
}
int Rational::reducer(int numerator, int denominator) // helper
function to reduce fraction if possible
{
int small = 0;
int num = 0;
int den = 0;
num = numerator;
den = denominator;
if (num == 0)
return den;
if (den == 0)
return num;
// Only absolute value is important
if (den < 0)
den = den * -1;
if (num < 0)
num = num * -1;
// Smaller of the two becomes small
if (num > den)
small = den;
else
small = num;
// Continue till no remainder
while (num % small != 0 || den % small != 0)
{
if (num > small)
{
num = num - (num % small);
small = num;
}
else
{
den = den - (den % small);
small = den;
}
}
// Return thr divisor which leaves no remainder
return small;
}
void Rational::signAdjuster() // Make sure that numerator has
negative sign if applicable
{
if (numerator >= 0 && denominator < 0) // Positive numerator,
negative denominator
{
numerator *= -1;
denominator *= -1;
}
if (numerator < 0 && denominator < 0) // Negative numerator,
negative denominator
{
numerator *= -1;
denominator *= -1;
}
}
/* bool Rational::get(istream& is)
{
if (!is)
{
return false;
}
string streamString;
getline(is, streamString);
int position = 0;
int streamConverter = 0;
stringstream ssOne(streamString.substr(position, 1));
ssOne >> streamConverter;
numerator = streamConverter;
position++;
// skip over any spaces and slashes
while (ssOne(streamString.substr(position, 1) == " " ||
ssOne(streamString.substr(position, 1) == "/")
{
position++;
}
stringstream ssOne(streamString.substr(position, 1));
ssOne >> streamConverter;
denominator = streamConverter;
if (inputString == "")
{
return false;
}
else
return true;
}
// Write string to output file
void Rational:ut(ostream& os)
{
if (!os)
{
return false;
}
if (os)
{
os << setw(0) << numerator << " / " << denominator << "\n";
}
return true;
}*/
I am having a terrible time overloading operators. I have tried what
I thought was the correct way, I tried the cheating (friend
declarations), all to no avail. Sorry for posting tons of code but I
really need help.
thanks,
Zenon
#ifndef Rational__hpp
#define Rational__hpp
#include<iostream>
#include<iomanip>
#include <cstring>
#include <cmath>
#include <sstream>
#include <fstream>
class Rational
{
// Define data members
int numerator;
int denominator;
// define function prototypes
public:
Rational::Rational(int num, int denom); // Creates a fraction
Rational::Rational(); // Default constructor
Rational::~Rational(); // Default destructor
void Rational::signAdjuster();
int Rational::reducer(); // helper function to reduce fracion if
possible
int Rational::reducer(int numerator, int denominator); // helper
function to reduce fracion if possible
Rational Rational:perator += (const Rational& fraction);
Rational Rational:perator -= (const Rational& fraction);
Rational Rational:perator *= (const Rational& fraction);
Rational Rational:perator /= (const Rational& fraction);
friend const Rational operator << (std:stream& os, const Rational&
fraction);
friend const Rational operator + (const Rational& lhs, const
Rational& rhs);
friend const Rational operator - (const Rational& lhs, const
Rational& rhs);
friend const Rational operator * (const Rational& lhs, const
Rational& rhs);
friend const Rational operator / (const Rational& lhs, const
Rational& rhs);
// friend const Rational operator << (std:stream& os, const
Rational& fraction);
// Rational Rational:perator + (const Rational& lhs, const Rational&
rhs);
// Rational Rational:perator + (const Rational& lhs, const Rational&
rhs);
/* Rational:perator - (const Rational lhs, const Rational rhs);
Rational:perator * (const Rational lhs, const Rational rhs);
Rational:perator / (const Rational lhs, const Rational rhs);
*/
Rational:perator double(); // Typecast to double
Rational::string(); // Typecast to string
/* bool Rational::get(istream& is);
void Rational:ut(ostream& os);*/
};
//std:stream& operator<< (std:stream& os, const Rational&
fraction); // Output
std::istream& operator>> (std::istream& is, const Rational& fraction);
// Input
#endif
#include <cstring>
#include <cmath>
#include<iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include "Rational.hpp"
using namespace std;
Rational::Rational(int num, int denom) // Set a fractions numerator
and denominator
{
numerator = num;
denominator = denom;
cout << "constructor numerator " + num ;
signAdjuster();
}
Rational::Rational() {} // Default constructor
Rational::~Rational() {} // Destructor
// std:perator <<(std:stream& os, const Rational& fraction); //
Output
/* Rational:stream& operator <<(std:stream& os, const Rational&
fraction) // Output
{
os << setw(0) << numerator << " / " << denominator << "\n";
// fraction.put(os);
// Rational:ut(os);
}*/
/* std::istream& operator >>(std::istream& is, const Rational&
fraction) // Input
{
fraction.get(is);
}*/
Rational Rational:perator += (const Rational& fraction)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fraction.numerator * denominator;
tempDenom = fraction.denominator * denominator;
tempNumTwo = numerator * fraction.denominator;
numerator = tempNumOne + tempNumTwo;
denominator = tempDenom;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return *this;
}
Rational Rational:perator -= (const Rational& fraction)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fraction.numerator * denominator;
tempDenom = fraction.denominator * denominator;
tempNumTwo = numerator * fraction.denominator;
numerator = tempNumOne - tempNumTwo;
denominator = tempDenom;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return *this;
}
Rational Rational:perator *= (const Rational& fraction)
{
numerator *= fraction.numerator;
denominator *= fraction.denominator;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return *this;
}
Rational Rational:perator /= (const Rational& fraction)
{
numerator *= fraction.denominator;
denominator *= fraction.numerator;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return *this;
}
const Rational operator + (const Rational& fractionLeft, const
Rational& fractionRight)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fractionLeft.numerator * fractionRight.denominator;
tempDenom = fractionLeft.denominator * fractionRight.denominator;
tempNumTwo = fractionRight.numerator * fractionLeft.denominator;
int numerator = tempNumOne + tempNumTwo;
int denominator = tempDenom;
Rational obj;
int divisor = obj.reducer(numerator, denominator);
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return Rational(numerator, denominator);
}
const Rational operator - (const Rational& fractionLeft, const
Rational& fractionRight)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fractionLeft.numerator * fractionRight.denominator;
tempDenom = fractionLeft.denominator * fractionRight.denominator;
tempNumTwo = fractionRight.numerator * fractionLeft.denominator;
int numerator = tempNumOne - tempNumTwo;
int denominator = tempDenom;
Rational obj;
int divisor = obj.reducer(numerator, denominator);
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return Rational(numerator, denominator);
}
const Rational operator * (const Rational& fractionLeft, const
Rational& fractionRight)
{
int numerator = fractionLeft.numerator * fractionRight.numerator;
int denominator = fractionLeft.denominator *
fractionRight.denominator;
Rational obj;
int divisor = obj.reducer(numerator, denominator);
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return Rational(numerator, denominator);
}
const Rational operator / (const Rational& fractionLeft, const
Rational& fractionRight)
{
int numerator = fractionLeft.numerator * fractionRight.denominator;
int denominator = fractionLeft.denominator *
fractionRight.numerator;
Rational obj;
int divisor = obj.reducer(numerator, denominator);
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
return Rational(numerator, denominator);
}
const Rational operator << (std:stream& os, const Rational&
fraction)
{
cout << "fraction.numerator " + fraction.numerator << "\n";
cout << fraction.numerator + " / " + fraction.denominator;
return Rational(fraction.numerator, fraction.denominator);
}
/* Rational:perator - (const Rational& fractionLeft, const Rational&
fractionRight)
{
int tempNumOne = 0;
int tempNumTwo = 0;
int tempDenom = 0;
tempNumOne = fractionLeft.numerator * fractionRight.denominator;
tempDenom = fractionLeft.denominator * fractionRight.denominator;
tempNumTwo = fractionRight.numerator * fractionLeft.denominator;
numerator = tempNumOne - tempNumTwo;
denominator = tempDenom;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
}*/
/* Rational:perator * (const Rational& fractionLeft, const Rational&
fractionRight)
{
numerator = fractionLeft.numerator * fractionRight.numerator;
denominator = fractionLeft.denominator * fractionRight.denominator;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
}*/
/* Rational:perator / (const Rational& fractionLeft, const Rational&
fractionRight)
{
numerator = fractionLeft.numerator * fractionRight.denominator;
denominator = fractionLeft.denominator * fractionRight.numerator;
int divisor = reducer ();
if (divisor != 1)
{
numerator /= divisor;
denominator /= divisor;
}
}
*/
Rational:perator double() // Typecast to double
{
return double (numerator/denominator);
}
Rational::string() // Typecast to string
{
return (char (numerator) + char(" / ") + char (denominator)) ;
}
int Rational::reducer() // helper function to reduce fraction if
possible
{
int small = 0;
int num = 0;
int den = 0;
num = numerator;
den = denominator;
if (num == 0)
return den;
if (den == 0)
return num;
// Only absolute value is important
if (den < 0)
den = den * -1;
if (num < 0)
num = num * -1;
// Smaller of the two becomes small
if (num > den)
small = den;
else
small = num;
// Continue till no remainder
while (num % small != 0 || den % small != 0)
{
if (num > small)
{
num = num - (num % small);
small = num;
}
else
{
den = den - (den % small);
small = den;
}
}
// Return thr divisor which leaves no remainder
return small;
}
int Rational::reducer(int numerator, int denominator) // helper
function to reduce fraction if possible
{
int small = 0;
int num = 0;
int den = 0;
num = numerator;
den = denominator;
if (num == 0)
return den;
if (den == 0)
return num;
// Only absolute value is important
if (den < 0)
den = den * -1;
if (num < 0)
num = num * -1;
// Smaller of the two becomes small
if (num > den)
small = den;
else
small = num;
// Continue till no remainder
while (num % small != 0 || den % small != 0)
{
if (num > small)
{
num = num - (num % small);
small = num;
}
else
{
den = den - (den % small);
small = den;
}
}
// Return thr divisor which leaves no remainder
return small;
}
void Rational::signAdjuster() // Make sure that numerator has
negative sign if applicable
{
if (numerator >= 0 && denominator < 0) // Positive numerator,
negative denominator
{
numerator *= -1;
denominator *= -1;
}
if (numerator < 0 && denominator < 0) // Negative numerator,
negative denominator
{
numerator *= -1;
denominator *= -1;
}
}
/* bool Rational::get(istream& is)
{
if (!is)
{
return false;
}
string streamString;
getline(is, streamString);
int position = 0;
int streamConverter = 0;
stringstream ssOne(streamString.substr(position, 1));
ssOne >> streamConverter;
numerator = streamConverter;
position++;
// skip over any spaces and slashes
while (ssOne(streamString.substr(position, 1) == " " ||
ssOne(streamString.substr(position, 1) == "/")
{
position++;
}
stringstream ssOne(streamString.substr(position, 1));
ssOne >> streamConverter;
denominator = streamConverter;
if (inputString == "")
{
return false;
}
else
return true;
}
// Write string to output file
void Rational:ut(ostream& os)
{
if (!os)
{
return false;
}
if (os)
{
os << setw(0) << numerator << " / " << denominator << "\n";
}
return true;
}*/