/* rational.cpp * This file is a variation of the rational.cpp file written by: * // Authors: James P. Cohoon and Jack W. Davidson * // Date: 7/15/96 * // Version: 1.0b */ #include "rational.h" // Compute greatest common divisor of a and b using Euclidean Algorithm static int gcd(int a, int b) // "static" so this function is local to this file { while (b != 0) { int rem = a % b; a = b; b = rem; } return a; } // default constructor Rational::Rational(): numeratorValue(0), denominatorValue(0) {} // (numer, denom) constructor Rational::Rational(int numer, int denom): numeratorValue(numer), denominatorValue(denom) {} Rational Rational::operator*(const Rational &s) const { int num = numeratorValue * s.numeratorValue; int den = denominatorValue * s.denominatorValue; int g = gcd(num, den); return Rational(num/g, den/g); } Rational Rational::operator+(const Rational &s) const { int num = numeratorValue * s.denominatorValue + denominatorValue * s.numeratorValue; int den = denominatorValue * s.denominatorValue; int g = gcd(num, den); return Rational(num/g, den/g); }