Question

So I'm writing a mixed numerals class for my OO class. We need to overload every comparison and boolean operand (among other things) but I'm having trouble with how to think about the '<' and '>' operands. Here are my '==' and '!=' operand functions for some context.

Edit: Also If anyone has any tips for to addition/subtraction methods I would be grateful.

bool operator ==(Mixed& mn1, Mixed& mn2){

mn1.ToFraction();
mn2.ToFraction();

mn1.Simplify();
mn2.Simplify();

if(mn1.numerator == mn2.numerator && mn1.denominator == mn2.denominator)
    return true;
else
    return false;
}

and

bool operator !=( Mixed& mn1,  Mixed& mn2){
mn1.ToFraction();
mn2.ToFraction();

mn1.Simplify();
mn2.Simplify();

if(mn1.numerator == mn2.numerator && mn1.denominator == mn2.denominator)
    return false;
else
    return true;
}

If anyone could offer some guidance I'd be appreciative. Oh and we can't convert them to decimals for the comparisons.

Edit: Here's my header.

#include <iostream>

using namespace std;

class Mixed
{
public:
Mixed(int integer, int numerator = 0, int denominator = 1);
Mixed(int integer = 0);
double Evaluate();
void ToFraction();
void Simplify();

friend istream& operator >>(istream& in, Mixed& mn);
friend ostream& operator <<(ostream& out, Mixed& mn);

friend bool operator ==( Mixed& mn1,  Mixed& mn2);
friend bool operator !=( Mixed& mn1,  Mixed& mn2);
friend bool operator >( Mixed& mn1,  Mixed& mn2);
friend bool operator <( Mixed& mn1,  Mixed& mn2);
friend bool operator <=( Mixed& mn1,  Mixed& mn2);
friend bool operator >=( Mixed& mn1,  Mixed& mn2);

friend const Mixed operator +( Mixed& mn1,  Mixed& mn2);
friend const Mixed operator -( Mixed& mn1,  Mixed& mn2);
friend const Mixed operator *( Mixed& mn1,  Mixed& mn2);
friend const Mixed operator /( Mixed& mn1,  Mixed& mn2);

private:
int GCD(int a, int b);
int integer, numerator, denominator;
};
Was it helpful?

Solution

Rational numbers are really quite straight forward: A rational number is a pair (a, b) of integers, usually written as a / b, up to the equivalence that (a, b) ≡ (a', b') if and only if ab' = a'b.

So if your class contains two representative integers p and q, you can implement the equality operator as:

struct Rational
{
    int p;            // numerator
    unsigned int q;   // denominator

    bool operator==(Rational const & rhs) const
    {
        return p * rhs.q == rhs.p * q;
    }

    // ...
};

For two positive rationals, you have inequality via p * rhs.q < rhs.p * q. All the other relations can be implemented in terms of those two, e.g. x <= y is the same as !(y < x) etc.

Cancellation of rationals to lowest terms can be done by dividing both numerator and denominator by their GCD, as you already appear to have implemented.

"Mixed" numbers, i.e. integer plus rational-smaller-than-one, are trivially converted into the usual numerator-denominator rational representation for manipulation, and back to mixed form by integral division and remainder.

OTHER TIPS

To compare rationals, you normally want to find a common denominator, then compare the numerators. For such a comparison, you don't really need to reduce to lowest terms either (though it's quick/easy enough that doing so is generally fairly harmless).

If you decide to keep your equality comparisons as they are, you can simplify them a bit to something like:

bool operator ==(Mixed mn1, Mixed mn2){    
    mn1.ToFraction();
    mn2.ToFraction();

    mn1.Simplify();
    mn2.Simplify();

    return mn1.numerator == mn2.numerator && mn1.denominator == mn2.denominator;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top