home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX0802.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-30  |  2.7 KB  |  68 lines

  1. // \EXAMPLES\EX0802.H
  2. // definition of the Fraction class
  3. //--------------------------------------------------------
  4. // files in this example:
  5. // EX0802.H        this file
  6. // %F,15,EX0802.CPP%EX0802.CPP      main program
  7. //---------------------------------------------------------
  8. #include <iostream.h>
  9.  
  10. class Vector;
  11. //--------------------------------------------------------
  12. // Defintion of the Fraction class
  13. //--------------------------------------------------------
  14. class Fraction
  15. {
  16. private:
  17.    int   num;                 // numerator signed
  18.    int   denom;              // demoninator always positive
  19.    void  reduce ();          // reduces Fraction to simplest form
  20.    int gcd( int n, int d);   // greatest common divisor
  21. public:
  22.    //------------------------------------------------------
  23.    // default constructor, convertion from integer
  24.    //------------------------------------------------------
  25.    Fraction( int i=0, int j=1) : num(i), denom(j)
  26.    { reduce(); }
  27.    //------------------------------------------------------
  28.    // three ways to output a fraction
  29.    //-------------------------------------------------------
  30.    void display()
  31.    { cout << num << "/" << denom; };
  32.  
  33.    friend
  34.    void print( const Fraction& F);
  35.  
  36.    friend
  37.    ostream& operator<<(ostream& os, const Fraction& f);
  38.    //-------------------------------------------------------
  39.    // one function to input a fraction
  40.    //-------------------------------------------------------
  41.    friend void read(Fraction& F);
  42.    //-------------------------------------------------------
  43.    // multiply a vector by a fraction
  44.    //-------------------------------------------------------
  45.    friend
  46.    Vector& mpy(const Fraction& F, Vector& vect);
  47. };
  48. //---------------------------------------------------------
  49. // private member function reduce --> to simlest form
  50. //---------------------------------------------------------
  51. void Fraction::reduce()
  52. { if ( denom == 0 )                     // if demoninator 0
  53.   {  num = 0; denom = 1; }                 //  set to 0/1
  54.   if ( denom < 0 )                      // if demoninator
  55.   { num = - num; denom = - denom; }        //  move sing to num
  56.   int g = gcd(num, denom);              // find greatest common
  57.   if (g > 1)                               //  denominator
  58.   { num = num/g; denom = denom/g;}      // simplify fraction
  59. }
  60. //---------------------------------------------------------
  61. // Euclids algorithm for greatest common demoninator
  62. //---------------------------------------------------------
  63. int Fraction::gcd(int n, int d)
  64. { n = n < 0 ? -n : n;                      // use absolute value
  65.   return n % d == 0 ? d : gcd( d, n % d);  // recursive call gcd
  66. }
  67. //---------------------------------------------------------
  68.