thats the thin, I am not sure why it does not work.
Here is my implementation of all of the functions.
Code:
#include <cstdlib>
#include <iostream>
#include "rational.h"
using namespace std;
Rational::Rational( int n, int d )
{
if (d)
{
numerator = n;
denominator = d;
}
else
{
numerator = 1;
denominator = 1;
}
}
bool Rational::operator!=( const int num )
{ return !( numerator == num ); }
//Fraction reducer function definition
void Rational::reduce( )
{
int largest, gcd = 1; // greatest common divisor;
largest = ( numerator > denominator ) ? numerator: denominator;
for ( int loop = 2; loop <= largest; ++loop )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
numerator /= gcd;
denominator /= gcd;
}
//Input operator function definition
istream &operator>> (istream &in, Rational &pFractB)
{
char slash;
in >> pFractB.numerator >> slash >> pFractB.denominator;
return in;
}
//Output operator function defintion
ostream &operator<< (ostream &out, Rational &pFractB)
{
if(pFractB.denominator == 1)
out << pFractB.numerator << endl;
else
out << pFractB.numerator << "/" << pFractB.denominator << endl;
return out;
}
//Addition operator function definition
Rational Rational::operator+ (Rational pFractB)
{
Rational tObj;
//calculates denominator
tObj.numerator = (denominator * pFractB.numerator) + (numerator * pFractB.denominator);
//calculates numerator
tObj.denominator = (denominator * pFractB.denominator);
tObj.reduce();
return tObj;
}
//Subtraction operator function definition
Rational Rational::operator- (Rational pFractB)
{
Rational tObj;
//calculates denominator
tObj.numerator = (denominator * pFractB.numerator) - (numerator * pFractB.denominator);
//calculates numerator
tObj.denominator = (denominator * pFractB.denominator);
tObj.reduce();
return tObj;
}
//Multiplication operator function definition
Rational Rational::operator* (Rational pFractB)
{
Rational tObj;
//calculates the numerator
tObj.numerator = numerator * pFractB.numerator;
//calculates the denominator
tObj.denominator = denominator * pFractB.denominator;
tObj.reduce();
return tObj;
}
//Division operator function definition
Rational Rational::operator/ (Rational pFractB)
{
Rational tObj;
//calculates the numerator
tObj.numerator = numerator * pFractB.denominator;
//calculates the denominator
tObj.denominator = denominator * pFractB.numerator;
tObj.reduce();
return tObj;
}
//Greater Than operator function definition
bool Rational::operator> (Rational &pFractB)
{
bool greaterThan = 0;
float tObj;
float testObj;
testObj = numerator / denominator;
tObj = pFractB.numerator / pFractB.denominator;
if(testObj > tObj)
greaterThan = 1;
return greaterThan;
}
//Less Than operator function definition
bool Rational::operator< (Rational &pFractB)
{
bool lessThan = 0;
float tObj;
float testObj;
testObj = numerator / denominator;
tObj = pFractB.numerator / pFractB.denominator;
if(testObj < tObj)
lessThan = 1;
return lessThan;
}
This is the client file:
Code:
#include<iostream>
#include"rational.h"
#include<fstream>
using namespace std;
ofstream outfile;
ifstream infile;
int main()
{
Rational FractA, FractB, FractR;
infile.open("ratdata.dat");
outfile.open("ratout.txt");
int cnt = 0;
char oper;
infile >> FractA;
while (FractA != 0)
{ cnt++;
infile >> oper;
infile >> FractB;
if(oper == '+')
FractR = FractA + FractB;
else if(oper == '-')
FractR = FractA - FractB;
else if(oper == '*')
FractR = FractA * FractB;
else if(oper == '/')
FractR = FractA / FractB;
outfile << cnt << ": ";
outfile << FractA << " " << oper
<< " " << FractB << " = "
<< FractR << endl << endl;
outfile << cnt << ": ";
if (FractA > FractB)
outfile << FractA << " is greater than " << FractB << endl;
else
outfile << FractA << " is NOT greater than " << FractB << endl;
outfile << "--------------------------------------" << endl;
infile >> FractA;
} // while
return 0;
} // main
and here is the header file:
Code:
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
using namespace std;
class Rational
{
friend istream &operator>> (istream &in, Rational &pFractB);
friend ostream &operator<< (ostream &out, Rational &pFractB);
public:
Rational( int = 1, int = 1 ); // default constructor
bool operator!=( const int );
Rational operator+ (Rational pFractB);
Rational operator- (Rational pFractB);
Rational operator* (Rational pFractB);
Rational operator/ (Rational pFractB);
bool operator> (Rational &pFractB);
bool operator< (Rational &pFractB);
private:
int numerator;
int denominator;
void reduce( );
};
#endif
The program runs, it is just the answers are wrong and the greater and less than functions are not working. I tested to see if the addition was working and it was before going through the reduce function.
So I though the relationship between the two functions was off somehow.