Rational Numbers continued


Take assignment 3 and re-write the Rational class to accomplish the following:

  • Properly handle either positive or negative rational numbers, including the case of both numerator and denominator being negative. (Some of you may have already implemented this as an extra credit exercise last week).
  • Implement all operations (add, sub, mul, div, equal, less, etc.) as overloaded operators +, -, *, /, ==, <
  • Whenever you perform an operation or construct a Rational, simplify the representation - i.e. reduce 20/50 to 2/5, or 16/4 to 4/1, etc.

    Note on fraction simplification:
    This will require finding the largest common factor, or all common factors, of the two numbers, i.e. the largest number that evenly divides BOTH the numerator and denominator.
    One way of doing this (not the most efficient, but perfectly adequate for small numbers) is to check all numbers, starting from 2, to see if they divide into both the numerator and the denominator without leaving a remainder (what operator will you use to do this?).
    When you find the largest number (called the largest common factor) that fits this criterion, divide both numbers by it and you have the simplified fraction.
    Question: what is the largest number you will have to try?