home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX10021.H
- // definition of the Fraction classs
- //---------------------------------------------------------------
-
- // files in this example:
- // EX10021.H this file
- // %F,15,EX10021.CPP%EX10021.CPP member functions of the class Fraction
- // %F,15,EX1002.CPP%EX1002.CPP main program to exercise Fraction class
- //-------------------------------------------------------------
-
- #ifndef EX1002_H
- #define EX1002_H
-
- #include <iostream.h>
-
- //--------------------------------------------------------
- // Definition of the Fraction class
- // Fractions are always kept in simplest form:
- // num/denom denom > 0
- // Fractions can be improper fractions
- //--------------------------------------------------------
-
- class Fraction
- {
-
- private:
-
- int num; // numerator signed
- int denom; // demoninator always positive
- void reduce (); // reduces Fraction to simplest form
- int gcd( int n, int d); // greatest common divisor
-
- public:
-
- //------------------------------------------------------
- // default constructor, convertion from integer
- // creates Fraction from numerator, denominator
- //------------------------------------------------------
- Fraction() : num(0), denom(1) {};
- inline
- Fraction( int i, int j ); // default j = 1
-
- //------------------------------------------------------
- // conversion from real
- // may reduce accuracy: minimum value 1/32737
- // : maximun value 32767/denom
- //------------------------------------------------------
- Fraction( double x);
-
- //------------------------------------------------------
- // copy constructor
- //------------------------------------------------------
- inline
- Fraction( const Fraction& src);
-
- //------------------------------------------------------
- // assignment operator
- //------------------------------------------------------
- inline Fraction&
- operator=( const Fraction& src);
-
- //------------------------------------------------------
- // conversion from Fraction to builtin types
- // NOTE: mixed mode expression would be ambiguous
- // if float() and int() are also overloaded.
- //------------------------------------------------------
- friend double
- real(const Fraction& F);
-
- //------------------------------------------------------
- // convert to integer - truncate
- //------------------------------------------------------
- friend int
- trunc(const Fraction& F);
-
- //------------------------------------------------------
- // two functions to output a Fraction
- //------------------------------------------------------
- ostream&
- display(ostream& os);
-
- friend ostream&
- print( const Fraction& F, ostream& os);
-
- //------------------------------------------------------
- // invert a fraction, keep sign in numerator
- //------------------------------------------------------
- Fraction
- Fraction::invert();
-
- //------------------------------------------------------
- // overloading >> and <<
- //------------------------------------------------------
- friend ostream&
- operator<<( ostream& os, const Fraction F);
-
- friend istream&
- operator>>( istream& is, Fraction& F);
-
- //------------------------------------------------------
- // overloading unary math operators + -
- //------------------------------------------------------
- inline Fraction operator+() const
- { return (*this); }
-
- inline Fraction operator-() const
- { return Fraction(-num,denom); }
-
- //------------------------------------------------------
- // overloading Prefix increment and decrement
- //------------------------------------------------------
- Fraction operator++()
- { num += denom;
- return *this; }
-
- Fraction operator--()
- { num -= denom;
- return *this; }
-
- //------------------------------------------------------
- // overloading Postfix increment and decrement
- //------------------------------------------------------
- Fraction operator++( int)
- { int temp = num;
- num += denom;
- return Fraction(temp,denom); }
-
- Fraction operator--( int)
- { int temp = num;
- num -= denom;
- return Fraction(temp,denom); }
-
- //------------------------------------------------------
- // overloading binary math operators + - * /
- // friend - first or second operand must be fraction
- //------------------------------------------------------
- friend Fraction
- operator+( const Fraction& f1, const Fraction& f2);
-
- friend Fraction
- operator-( const Fraction& f1, const Fraction& f2);
-
- friend Fraction
- operator*( const Fraction& f1, const Fraction& f2);
-
- friend Fraction
- operator/( const Fraction& f1, const Fraction& f2);
-
- //------------------------------------------------------
- // overloading binary comparsion operators < == >
- // inline - first operand must be fraction
- //------------------------------------------------------
-
- inline int
- operator<( const Fraction& f);
-
- inline int
- operator==( const Fraction& f);
-
- inline int
- operator>( const Fraction& f);
- };
-
-
- //--------------------------------------------------------------
- // inline member functions
- //--------------------------------------------------------------
-
- //------------------------------------------------------
- // default constructor, convertion from integer
- // creates Fraction from numerator, denominator
- //------------------------------------------------------
- inline
- Fraction::Fraction( int i, int j=1 ) : num(i), denom(j)
- { if (denom != 1 )
- reduce();
- }
-
- //--------------------------------------------------------------
- //Copy constructor
- //--------------------------------------------------------------
- inline
- Fraction::Fraction( const Fraction& src)
- {
- num = src.num;
- denom = src.denom;
- }
-
- //------------------------------------------------------
- // assignment operator
- //------------------------------------------------------
- inline Fraction&
- Fraction::operator=( const Fraction& src)
- {
- num = src.num;
- denom = src.denom;
- return *this;
- }
-
- //------------------------------------------------------
- // binary operators
- //------------------------------------------------------
-
- //------------------------------------------------------
- // overload operator > == < comparisons
- // ( operators >= != <= would be similar )
- //------------------------------------------------------
-
- inline int
- Fraction::operator<(const Fraction& f)
- {
- return num * f.denom < f.num * denom;
- }
-
- inline int
- Fraction::operator==(const Fraction& f)
- {
- return num * f.denom == f.num * denom;
- }
-
- inline int
- Fraction::operator>( const Fraction& f)
- {
- return num * f.denom > f.num * denom;
- }
-
- //------------------------------------------------------
-
- #endif
-