In order to clear up some confusion around question 3 of Assignment 2, please change it as follows:
Include a function to normalize the Fraction class so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/-8 would be presented the same as -1/2. Any sign is therefore carried by the numerator; keep the denominator positive.
A fraction can be reduced to its smallest value by computing the greatest common divisor (GCD) of the numerator and denominator, and then dividing each by this number.
A recursive definition to compute the GCD of two integers follows:
n if n divides evenly in m
gcd(m,n) =
gcd(n, remainder of m divided by n) otherwise
This definition can be coded as follows:
int gcd(int m, int n)
{
if ((m % n) == 0)
return n;
else
return gcd(n, m % n);
}
Modify the constructor(s) written in the previous exercise to include a call to normalize() so that every initialized fraction is in its lowest common terms. Also make sure that each overloaded operator function also uses normalize() to return a fraction in its smallest value.
Replace the display function with an overloaded insertion operator so that a Fraction object can be inserted directly into the cout stream. Also include an overloaded extraction operator that uses the cin stream with a Fraction object. Overload both the insertion and extraction operators as friend functions.
Note: The function to normalize the fractions is not part of the Fraction class although it should be included in the implementation file for separate compilation.