# class RationalNumber

Need to create a class RationalNumber(fractions) with the following capabilities:

* Create a constructor that prevents a 0 denominator in a fraction.

* Design the add, sub, mul, and division functions for this class.

* Design the functions for the relational and equality operation in this class.

I am looking for the ACTUAL WORKING CODE(not snippets) that will generate this output:

5/3 subtraction 3/2 = 1/6
5/3 multiplication 3/2 = 5/2
5/3 division 3/2 = 10/9

5/3 is:
!= 3/2
!= 3/2

This is what I have so far.  Please incorporate this format if you can.  Please Advise.  Del

// rational.h

#ifndef RATIONAL_H
#define RATIONAL_H

class RationalNumber {

public:

RationalNumber(int=0, int=1);
RationalNumber sub(const RationalNumber &);
RationalNumber mul(const RationalNumber &);
RationalNumber div(const RationalNumber &);

bool equal(const RationalNumber &) const;
bool notEqual(const RationalNumber &) const;
void printRational() const;

private:

int numerator;
int denominator;

};

#endif

// rational.cpp

#include <iostream>

using namespace std;

#include "rational.h"

RationalNumber::RationalNumber(int n, int d)
{

numerator = n;
denominator = d;

}

{

RationalNumber t;

t.numerator = a.numerator * denominator;
t.numerator += a.denominator * numerator;
t.denominator = a.denominator * denominator;

return t;

}

RationalNumber RationalNumber::sub(const RationalNumber &s)
{

RationalNumber t;

t.numerator = s.denominator * numerator;
t.numerator -= denominator * s.numerator;
t.denominator = s.denominator * denominator;

return t;

}

RationalNumber RationalNumber::mul(const RationalNumber &m)
{

RationalNumber t;

t.numerator = m.numerator * numerator;
t.denominator = m.denominator * denominator;

return t;

}

RationalNumber RationalNumber::div(const RationalNumber &v)
{

RationalNumber t;

t.numerator = v.denominator * numerator;
t.denominator = denominator * v.numerator;

return t;

}

void RationalNumber::printRational() const
{

if(denominator == 0)
cout << "\nDivide by zero error!!!\n";

else
if(numerator == 0)
cout << 0;
else
cout << numerator << '/' << denominator;

}

bool RationalNumber::equal(const RationalNumber &e) const
{

...................................................

}

bool RationalNumber::notEqual(const RationalNumber &n) const
{

...................................................

}

// rational_driver.cpp

#include <iostream>

using namespace std;

#include "rational.h"

int main()
{

RationalNumber c(5,3);
RationalNumber d(3,2);
RationalNumber x;

c.printRational();
d.printRational();
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
cout << "\n\n";

c.printRational();
cout << " subtraction ";
d.printRational();
x = c.sub(d);
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
cout << "\n\n";

c.printRational();
cout << " multiplication ";
d.printRational();
x = c.mul(d);
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
cout << "\n\n";

c.printRational();
cout << " division ";
d.printRational();
x = c.div(d);
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
cout << endl;

return 0;

} // end driver

###### Who is Participating?

Commented:
Take this

// rational.h

#ifndef RATIONAL_H
#define RATIONAL_H
class RationalNumber {

public:

RationalNumber();
RationalNumber(int num, int denom = 1);
RationalNumber(const RationalNumber &);

RationalNumber sub(const RationalNumber &) const;
RationalNumber mul(const RationalNumber &) const;
RationalNumber div(const RationalNumber &) const;
bool equal(const RationalNumber &) const;
bool notEqual(const RationalNumber &) const;

bool operator == (const RationalNumber& rn);
bool operator != (const RationalNumber& rn);
bool operator <  (const RationalNumber& rn);
bool operator >  (const RationalNumber& rn);
bool operator <= (const RationalNumber& rn);
bool operator >= (const RationalNumber& rn);

RationalNumber& operator = (const RationalNumber& rn);
RationalNumber& operator = (int n);
RationalNumber& operator += (const RationalNumber& rn);
RationalNumber& operator += (int n);
RationalNumber& operator -= (const RationalNumber& rn);
RationalNumber& operator -= (int n);
RationalNumber& operator *= (const RationalNumber& rn);
RationalNumber& operator *= (int n);
RationalNumber& operator /= (const RationalNumber& rn);
RationalNumber& operator /= (int n);

RationalNumber operator + (const RationalNumber& rn) const;
RationalNumber operator + (int n)  const;
RationalNumber operator - (const RationalNumber& rn)  const;
RationalNumber operator - (int n)  const;
RationalNumber operator * (const RationalNumber& rn)  const;
RationalNumber operator * (int n)  const;
RationalNumber operator / (const RationalNumber& rn)  const;
RationalNumber operator / (int n)  const;

void printRational() const;

friend RationalNumber operator+ (int n, const RationalNumber& rn);
friend RationalNumber operator- (int n, const RationalNumber& rn);
friend RationalNumber operator* (int n, const RationalNumber& rn);
friend RationalNumber operator/ (int n, const RationalNumber& rn);

private:

int numerator;
int denominator;

};

#endif

// rational.cpp

#include <iostream>
#include <assert.h>

using namespace std;

#include "rational.h"

RationalNumber::RationalNumber()
: numerator(0), denominator(1)
{
}

RationalNumber::RationalNumber(int n, int d)
: numerator(n), denominator(d)
{
if (d == 0)
{
assert(d != 0);
denominator = 1;
}
}

RationalNumber::RationalNumber(const RationalNumber & rn)
:   numerator  (rn.numerator),
denominator(rn.denominator)
{
}

bool RationalNumber::operator == (const RationalNumber& rn)
{
return (numerator * rn.denominator == rn.numerator * denominator);
}

bool RationalNumber::operator != (const RationalNumber& rn)
{
return !(*this == rn);
}

bool RationalNumber::operator <  (const RationalNumber& rn)
{
return (numerator * rn.denominator < rn.numerator * denominator);
}

bool RationalNumber::operator >  (const RationalNumber& rn)
{
return (numerator * rn.denominator > rn.numerator * denominator);
}

bool RationalNumber::operator <= (const RationalNumber& rn)
{
return !(*this > rn);
}

bool RationalNumber::operator >= (const RationalNumber& rn)
{
return !(*this < rn);
}

RationalNumber RationalNumber::operator + (const RationalNumber& rn) const
{
}
RationalNumber RationalNumber::operator + (int n)  const
{
}
RationalNumber RationalNumber::operator - (const RationalNumber& rn)  const
{
return sub(rn);
}
RationalNumber RationalNumber::operator - (int n)  const
{
return sub(RationalNumber(n));
}
RationalNumber RationalNumber::operator * (const RationalNumber& rn)  const
{
return mul(rn);
}
RationalNumber RationalNumber::operator * (int n)  const
{
return mul(RationalNumber(n));
}
RationalNumber RationalNumber::operator / (const RationalNumber& rn)  const
{
return div(rn);
}
RationalNumber RationalNumber::operator / (int n)  const
{
return div(RationalNumber(n));
}

RationalNumber& RationalNumber::operator = (const RationalNumber& rn)
{
numerator   = rn.numerator;
denominator = rn.denominator;
return *this;
}

RationalNumber& RationalNumber::operator = (int n)
{
numerator   = n;
denominator = 1;
return *this;
}

RationalNumber& RationalNumber::operator += (const RationalNumber& rn)
{
return *this;
}

RationalNumber& RationalNumber::operator += (int n)
{
return *this;
}

RationalNumber& RationalNumber::operator -= (const RationalNumber& rn)
{
*this = sub(rn);
return *this;
}

RationalNumber& RationalNumber::operator -= (int n)
{
*this = sub(n);
return *this;
}

RationalNumber& RationalNumber::operator *= (const RationalNumber& rn)
{
*this = mul(rn);
return *this;
}

RationalNumber& RationalNumber::operator *= (int n)
{
*this = mul(n);
return *this;
}

RationalNumber& RationalNumber::operator /= (const RationalNumber& rn)
{
*this = div(rn);
return *this;
}

RationalNumber& RationalNumber::operator /= (int n)
{
*this = div(n);
return *this;
}

RationalNumber operator+ (int n, const RationalNumber& rn)
{
}

RationalNumber operator- (int n, const RationalNumber& rn)
{
RationalNumber t(n);
return t.sub(rn);
}

RationalNumber operator* (int n, const RationalNumber& rn)
{
return rn.mul(n);
}

RationalNumber operator/ (int n, const RationalNumber& rn)
{
RationalNumber t(n);
return t.div(rn);
}

{

RationalNumber t;

t.numerator = a.numerator * denominator;
t.numerator += a.denominator * numerator;
t.denominator = a.denominator * denominator;

return t;
}

RationalNumber RationalNumber::sub(const RationalNumber &s)  const
{

RationalNumber t;

t.numerator = s.denominator * numerator;
t.numerator -= denominator * s.numerator;
t.denominator = s.denominator * denominator;

return t;

}

RationalNumber RationalNumber::mul(const RationalNumber &m)  const
{

RationalNumber t;

t.numerator = m.numerator * numerator;
t.denominator = m.denominator * denominator;

return t;

}

RationalNumber RationalNumber::div(const RationalNumber &v)  const
{

RationalNumber t;

t.numerator = v.denominator * numerator;

if (v.numerator != 0)
{
t.denominator = denominator * v.numerator;
}
else
{
assert(v.numerator != 0);
}

return t;

}

void RationalNumber::printRational() const
{

if(numerator == 0)
cout << 0;
else
cout << numerator << '/' << denominator;

}

bool RationalNumber::equal(const RationalNumber &e) const
{
return (numerator*e.denominator == e.numerator*denominator);
}

bool RationalNumber::notEqual(const RationalNumber &n) const
{
return !equal(n);

}

// rational_driver.cpp
#include <iostream>

using namespace std;

#include "rational.h"

int main()
{

RationalNumber c(5,3);
RationalNumber d(3,2);
RationalNumber x;

c.printRational();
d.printRational();
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
cout << "\n\n";

c.printRational();
cout << " subtraction ";
d.printRational();
x = c.sub(d);
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
cout << "\n\n";

c.printRational();
cout << " multiplication ";
d.printRational();
x = c.mul(d);
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
cout << "\n\n";

c.printRational();
cout << " division ";
d.printRational();
x = c.div(d);
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
cout << endl;

return 0;

} // end driver

Regards, Alex
0

Commented:
0

Author Commented:
I need the two function definitions written for:

bool RationalNumber::equal(const RationalNumber &e) const
{

...................................................

}

bool RationalNumber::notEqual(const RationalNumber &n) const
{

...................................................

}

and then I need the code tested and possibly corrected so it will compile.
0

Author Commented:
In other words I need the above code modified to generate the output listed.  Please advise.  Del

5/3 subtraction 3/2 = 1/6
5/3 multiplication 3/2 = 5/2
5/3 division 3/2 = 10/9

5/3 is:
!= 3/2
!= 3/2

0

Commented:
0

Author Commented:
itsmeandnobodyelse,

Excellent!!!!!!!!!! Would you modify the above code to also include istream and ostream?  A million thanks.  Del
0

Commented:
Thank's for the points:

I added a function reduceFraction(), that might be called after any update of a RationalNumber. You may test the function using that code snippet at the end of main():

int main()
{
...

char  temp[128];
char* pszDenom;
while ( x != -1 )
{
cout << "Enter RationalNumber [e. g. 17/5] ==>";
cin  >> temp;

pszDenom = strchr(temp, '/');
if (pszDenom == NULL)
pszDenom = " 1";
x = RationalNumber(atoi(temp), atoi(++pszDenom));
x.reduceFraction();
x.printRational();
cout << endl << endl;
}
return 0;
}

RationalNumber& RationalNumber::reduceFraction()
{
int  i   = numerator;
int  j   = denominator;
// save sign of number
bool neg = i * j < 0;
// make all numbers positive
if ( i < 0 )
i = -i;
if ( j < 0 )
j = -j;

// get minimum
int m = (i < j)? i : j;

// we check 2 and all odd numbers til m
// so, we need a step of 1 first and a step of 2 else
for (int n = 2; n <= m; n += (n == 2)? 1 : 2)
{
// check if both i and j can be divided by n without rest
while ((i % n) == 0 && (j % n) == 0)
{
// ok, divide both and minimum also (as m is either i or j)
i   /= n;
j   /= n;
m   /= n;
}
}
numerator   = (neg)? -i : i;
denominator = j;
return *this;
}

Regards, Alex

0

Author Commented:
My pleasure.  Once again, excellent!!!!!!!!!! Thank you.  Del
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.