// Fraction.cpp - wdg 2009 #include "Fraction.h" int gcd(int a,int b) { if(b==0) return a; else return gcd (b, a%b) ; } Fraction::Fraction(int whole) : numer(whole), denom(1) { } Fraction::Fraction(int n,int d) { if(d<0) { d=-d; n=-n; } int h = gcd(n<0?-n:n,d); numer=n/h; denom=d/h; } Fraction::Fraction(const Fraction & other) : numer(other.numer), denom(other.denom) { } ostream & operator<< (ostream & out, const Fraction & fraction) { if(fraction.denom==1) out << fraction.numer; else out << fraction.numer << "/" << fraction.denom; return out; } Fraction Fraction::operator+(const Fraction & other) const { int newNumer = numer*other.denom + denom*other.numer; int newDenom = denom * other.denom; return Fraction(newNumer,newDenom); } Fraction Fraction::operator*(const Fraction & other) const { int newNumer = numer*other.numer; int newDenom = denom * other.denom; return Fraction(newNumer,newDenom); } bool Fraction::operator==(const Fraction & other) const { return (numer==other.numer && denom==other.denom); }