#include "fraction.h" #include #include Fraction::Fraction() : numer(1), denom(1) { } Fraction::Fraction(long int num) : numer(num), denom(1) { LowestTerms(); } Fraction::Fraction(long int num, long int den) : numer(num), denom(den) { LowestTerms(); } Fraction::Fraction(Fraction & a) : numer(a.GetNumer()), denom(a.GetDenom()), sign(a.GetSign()) { } int Fraction::GetNumer() { return numer; } int Fraction::GetDenom() { return denom; } double Fraction::ShowDbl() { return (sign * double(numer) / denom); } int Fraction::Quotient() { return (sign * numer / denom); } int Fraction::Remainder() { return (sign * (numer % denom)); } bool Fraction::IsEqual(Fraction & rhs) { return ((denom == rhs.GetDenom()) && (numer == rhs.GetNumer()) && (sign == rhs.GetSign()); } bool Fraction::IsEqual(int rhs) { Fraction a(rhs); return IsEqual(a); } bool Fraction::IsGreater(Fraction & rhs) { int a, b; if(sign > rhs.GetSign()) { return true; } else if(sign < rhs.GetSign()) { return false; } else { a = sign * numer * rhs.GetDenom(); b = sign * denom * rhs.GetNumer(); return (a > b); } } bool Fraction::IsGreater(int rhs) { Fraction a(rhs); return IsGreater(a); } bool Fraction::IsLesser(Fraction & rhs) { return(!(IsEqual(rhs) || IsGreater(rhs))); } bool Fraction::IsLesser(int rhs) { Fraction a(rhs); return IsLesser(a); } void Fraction::LowestTerms() { int k = 2; if(numer * denom < 0) { sign = -1; numer = labs(numer); denom = labs(denom); } else if(numer == 0) { sign = 0; denom = labs(denom); } else { sign = 1; numer = labs(numer); denom = labs(denom); } while(k <= denom && k <= numer) { if((denom % k == 0) && (numer % k == 0)) { denom = denom / k; numer = numer / k; } else { k++; } } return; } void Fraction::Swap() { int temp; temp = denom; denom = numer; numer = temp; return; } Fraction Fraction::operator *=(Fraction & rhs) { numer *= rhs.GetNumer(); denom *= rhs.GetDenom(); sign *= rhs.GetSign(); LowestTerms(); return *this; } Fraction Fraction::operator *=(int rhs) { numer *= rhs; LowestTerms(); return *this; } Fraction Fraction::operator /=(Fraction & rhs) { Fraction a(rhs); a.Swap(); *this *= a; return *this; } Fraction Fraction::operator /=(int rhs) { denom *= rhs; LowestTerms(); return *this; } Fraction Fraction::operator +=(Fraction & rhs) { numer = sign * numer * rhs.GetDenom() + rhs.GetSign() * denom * rhs.GetNumer(); denom *= rhs.GetDenom(); LowestTerms(); return *this; } Fraction Fraction::operator +=(int rhs) { numer += denom * rhs; LowestTerms(); return *this; } Fraction Fraction::operator -=(Fraction & rhs) { *this += rhs * -1; return *this; } Fraction Fraction::operator -=(int rhs) { *this += rhs * -1; return *this; } Fraction Fraction::operator =(Fraction & rhs) { numer = rhs.GetNumer(); denom = rhs.GetDenom(); sign = rhs.GetSign(); return *this; } Fraction Fraction::operator =(int rhs) { numer = rhs; denom = 1; LowestTerms(); return *this; } bool operator <(Fraction & lhs, Fraction & rhs) { return(lhs.IsLesser(rhs)); } bool operator <(Fraction & lhs, int rhs) { return(lhs.IsLesser(rhs)); } bool operator <(int lhs, Fraction & rhs) { return(rhs.IsGreater(lhs)); } bool operator >(Fraction & lhs, Fraction & rhs) { return(lhs.IsGreater(rhs)); } bool operator >(Fraction & lhs, int rhs) { return(lhs.IsGreater(rhs)); } bool operator >(int lhs, Fraction & rhs) { return(rhs.IsLesser(lhs)); } bool operator ==(Fraction & lhs, Fraction & rhs) { return(lhs.IsEqual(rhs)); } bool operator ==(Fraction & lhs, int rhs) { return(lhs.IsEqual(rhs)); } bool operator ==(int lhs, Fraction & rhs) { return(rhs.IsEqual(lhs)); } bool operator <=(Fraction & lhs, Fraction & rhs) { return(lhs < rhs || lhs == rhs); } bool operator <=(Fraction & lhs, int rhs) { return(lhs < rhs || lhs == rhs); } bool operator <=(int lhs, Fraction & rhs) { return(lhs < rhs || lhs == rhs); } bool operator >=(Fraction & lhs, Fraction & rhs) { return(lhs > rhs || lhs == rhs); } bool operator >=(Fraction & lhs, int rhs) { return(lhs > rhs || lhs == rhs); } bool operator >=(int lhs, Fraction & rhs) { return(lhs > rhs || lhs == rhs); } bool operator !=(Fraction & lhs, Fraction & rhs) { return(!(lhs == rhs)); } bool operator !=(Fraction & lhs, int rhs) { return(!(lhs == rhs)); } bool operator !=(int lhs, Fraction & rhs) { return(!(lhs == rhs)); } Fraction operator *(Fraction & lhs, Fraction & rhs) { Fraction temp(lhs); temp *= rhs; return temp; } Fraction operator *(Fraction & lhs, int rhs) { Fraction temp(lhs); temp *= rhs; return temp; } Fraction operator *(int lhs, Fraction & rhs) { Fraction temp(rhs); temp *= lhs; return temp; } Fraction operator +(Fraction & lhs, Fraction & rhs) { Fraction temp(lhs); temp += rhs; return temp; } Fraction operator +(Fraction & lhs, int rhs) { Fraction temp(lhs); temp += rhs; return temp; } Fraction operator +(int lhs, Fraction & rhs) { Fraction temp(rhs); temp += lhs; return temp; } int Fraction::GetSign() { return sign; }