//**************************************************************************// // // // Copyright (c) 1997. // // Richard D. Irwin, Inc. // // // // This software may not be distributed further without permission from // // Richard D. Irwin, Inc. // // // // This software is distributed WITHOUT ANY WARRANTY. No claims are made // // as to its functionality or purpose. // // // // Authors: James P. Cohoon and Jack W. Davidson // // Date: 7/15/96 // // Version: 1.0b // // // // 01/23/97 - Additional comments added by MAR // Overcommented because also explaining the language as well // as the program // 01/28/97 - Simplification of rational added by MAR // 04/02/98 - Added == operator so that could use function templates in // list.h (it then uses newly added RatsEqual) (MAR) // 12/01/98 - Added subtraction, division, inequality, relative equality // floating point conversion, and operators // //**************************************************************************// // rational.h: declaration of Rational ADT // conditional compilation to avoid trying to include same thing twice #ifndef RATIONAL_H #define RATIONAL_H #include // io library typically used with c++ - has some improvements over stdio // (which still can be used, but usually isn't) // a major advantage of iostream is that its operators can be overloaded // so that user defined class objects can be displayed/read with the // same interface - see below // Rational ADT: class description // Defines class and its interface to the world // implementation is in rational.cpp class Rational { // public interface can be used by anybody public: // member functions // constructors // used if no arguments passed Rational(); // used if 1 or 2 arguments passed // denom if not passed defaults to 1 Rational(int numer, int denom = 1); // inspectors // MAR - see simplified value for a rational object Rational GetSimplified() const; // MAR - see floating point equivalent value for a rational object float GetFloating() const; // mutators - establish new values of attributes (data members) // MAR - change a rational object to have its simplified value void SimplifyObj(); Rational& AddToMe(const Rational &r); // some arithmetic facilitators Rational Add(const Rational &r) const; Rational SimplifyAdd(const Rational &r) const; Rational Multiply(const Rational &r) const; Rational SimplifyMultiply(const Rational &r) const; // MAR - include subtraction and division Rational Subtract(const Rational &r) const; Rational SimplifySubtract(const Rational &r) const; Rational Divide(const Rational &r) const; Rational SimplifyDivide(const Rational &r) const; // some comparison facilitators // MAR - exactly equal bool RatsEqual(const Rational &s) const; // MAR - equivalent value bool Equal(const Rational &s) const; // MAR - less than bool LessThan(const Rational &s) const; // some stream facilitators // write to stream void Insert(ostream &sout) const; // read from stream void Extract(istream &sin); // protected interface can be used by objects that are in class, // and objects that are in a subtype class (derived class) protected: // inspectors // return values of attributes (data members) int Numerator() const; int Denominator() const; // mutators // establish new values of attributes (data members) void SetNumerator(int numer); void SetDenominator(int denom); // MAR // facilitators Rational Simplify() const; // private members can only be used by other member functions and // operators of this class // typically all attributes (data members) go here private: // data members int NumeratorValue; int DenominatorValue; }; // below overloads standard c++ operators so that they can be used // with rational class objects, so syntax is like with ints floats etc // // Rational ADT: auxiliary operator description Rational operator+(const Rational &r, const Rational &s); Rational operator-(const Rational &r, const Rational &s); Rational operator*(const Rational &r, const Rational &s); Rational operator/(const Rational &r, const Rational &s); bool operator==(const Rational &r, const Rational &s); bool operator<(const Rational &r, const Rational &s); bool operator<=(const Rational &r, const Rational &s); bool operator>(const Rational &r, const Rational &s); bool operator>=(const Rational &r, const Rational &s); Rational& operator+=(Rational &r, const Rational &s); ostream& operator<<(ostream &sout, const Rational &s); istream& operator>>(istream &sin, Rational &r); #endif