#include "Fraction.h" #include Fraction::Fraction(FractionPart num, FractionPart den) : numerator(num) , denominator(den ? den : 1) { simplify(); } Fraction::~Fraction() { } FractionPart Fraction::greatestCommonDivisor(FractionPart a, FractionPart b) { a = abs(a); b = abs(b); while (b) { FractionPart t = b; b = a % t; a = t; } return a; } bool Fraction::simplify() { FractionPart gdc = greatestCommonDivisor(numerator, denominator); numerator /= gdc; denominator /= gdc; return gdc > 1; } std::istream& operator>>(std::istream& input, Fraction& fraction) { input >> fraction.numerator; // ignore '/' separator and whitespace while ( isspace(input.get()) ) ; return input >> fraction.denominator; } std::ostream& operator<<(std::ostream& output, const Fraction& fraction) { return output << fraction.numerator << '/' << fraction.denominator; } Fraction Fraction::operator*(const Fraction& other) const { return Fraction(numerator * other.numerator, denominator * other.denominator); } Fraction operator+(const Fraction& f1, const Fraction& f2) { return Fraction(f1.getNumerator() * f2.getDenominator() + f1.getDenominator() * f2.getNumerator() , f1.getDenominator() * f2.getDenominator() ); }