#ifndef _FRACTION_H #define _FRACTION_H /*author: Carl Staab last modified: 6/8/00 contents: the Fraction class to hold fractions, and associated functions implementation: the Fraction class is implemented using a pair of int values for the numerator and denominator does not use the const keyword still missing: negative values should work now destructors (?) a few operators output fcn cannot handle input zeros to denominator*/ #include class Fraction { public: //constructors Fraction(); //initializes fraction to 1/1 Fraction(long int num); //initializes fraction to num/1, with sign of num Fraction(long int num, long int den); //initializes fraction to num/den, signs consolidated //prereq: den != 0 ***is very important*** Fraction(Fraction & a); //copy constructor //accessors int GetNumer(); //returns the numerator of the fraction int GetDenom(); //returns the denominator of the fraction int GetSign(); //returns the value of sign(-1 for negative, 0 for 0, +1 for positive) double ShowDbl(); //returns the double value of the evaluated fraction int Quotient(); //returns the value of the fraction in integer division int Remainder(); //returns the remainder of the fraction in integer division //comparisons bool IsEqual(Fraction & rhs); //to be used for == //returns true if *this == rhs, false otherwise bool IsGreater(Fraction & rhs); //to be used for > //returns true if *this > rhs, false otherwise bool IsLesser(Fraction & rhs); //to be used for < //returns true if *this < rhs, false otherwise //do the "unary" *=, +=, etc. operators here Fraction operator *=(Fraction & rhs); Fraction operator +=(Fraction & rhs); Fraction operator /=(Fraction & rhs); Fraction operator -=(Fraction & rhs); //overloading with int bool IsEqual(int rhs); bool IsGreater(int rhs); bool IsLesser(int rhs); //While not the most elegant, the simplest solution is probably //just to convert these int values to Fractions. Fraction operator *=(int rhs); Fraction operator +=(int rhs); Fraction operator /=(int rhs); Fraction operator -=(int rhs); //Here, though, it's just as easy to do the direct math. //Assignment operator: Fraction operator =(Fraction & rhs); Fraction operator =(int rhs); private: //data members long int numer; //the numerator long int denom; //the denominator int sign; //the sign is -1 if negative, 1 if positive //Value of numer = 0 will be represented as sign = 0 //helpers void LowestTerms(); //sets a fraction to lowest terms void Swap(); //swaps numer and denom- useful for /= }; bool operator <(Fraction & lhs, Fraction & rhs); bool operator <(Fraction & lhs, int rhs); bool operator <(int lhs, Fraction & rhs); bool operator >(Fraction & lhs, Fraction & rhs); bool operator >(Fraction & lhs, int rhs); bool operator >(int lhs, Fraction & rhs); bool operator ==(Fraction & lhs, Fraction & rhs); bool operator ==(Fraction & lhs, int rhs); bool operator ==(int lhs, Fraction & rhs); bool operator <=(Fraction & lhs, Fraction & rhs); bool operator <=(Fraction & lhs, int rhs); bool operator <=(int lhs, Fraction & rhs); bool operator >=(Fraction & lhs, Fraction & rhs); bool operator >=(Fraction & lhs, int rhs); bool operator >=(int lhs, Fraction & rhs); bool operator !=(Fraction & lhs, Fraction & rhs); bool operator !=(Fraction & lhs, int rhs); bool operator !=(int lhs, Fraction & rhs); Fraction operator *(Fraction & lhs, Fraction & rhs); Fraction operator *(Fraction & lhs, int rhs); Fraction operator *(int lhs, Fraction & rhs); Fraction operator +(Fraction & lhs, Fraction & rhs); Fraction operator +(Fraction & lhs, int rhs); Fraction operator +(int lhs, Fraction & rhs); #endif