// Fraction (Rational number) class implementation (see Fraction.hpp) // ----------------------------------------------- #include "Fraction.hpp" long Fraction::gcd (LONG m, LONG n) // Greatest common divisor {return m % n ? gcd(n, m % n) : n; } // (Recursive implementation) long Fraction::lcm(LONG m, LONG n) {return (m/gcd(m,n)) * n;} void Fraction::reduce() // Increase speed by nullifying or {long d = gcd(abs(numer),denom); // selectively executing this function, numer /= d; denom /= d; // at increased risk of overflow. } Fraction& Fraction::operator+=(FRACTION rs) {long d = lcm(denom, rs.denom); // Common divisor numer *= d / denom; denom = d; numer += (d / rs.denom) * rs.numer; reduce(); return *this; } Fraction& Fraction::operator-=(FRACTION rs) {long d = lcm(denom, rs.denom); // Common divisor numer *= d / denom; denom = d; numer -= (d / rs.denom) * rs.numer; reduce(); return *this; } Fraction& Fraction::operator*=(FRACTION rs) {numer *= rs.numer; denom *= rs.denom; reduce(); return *this; } Fraction& Fraction::operator/=(FRACTION rs) {numer *= rs.denom; denom *= rs.numer; reduce(); return *this; } ostream& operator<<(ostream& s, FRACTION x) {return s << x.numerator() << '/' << x.denominator();}