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

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