home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX1002.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-25  |  4.4 KB  |  112 lines

  1. // \EXAMPLES\EX1002.CPP
  2. //  main program to exercise Fraction classs
  3. //---------------------------------------------------------------
  4.  
  5. //  files in this example:
  6. // %F,15,EX10021.H%EX10021.H   definition of the class Fraction
  7. // %F,15,EX10021.CPP%EX10021.CPP member functions of the class Fraction
  8. // EX1002.CPP  this file
  9. //-------------------------------------------------------------
  10.  
  11. #include "EX10021.H"
  12. #include <iostream.h>
  13.  
  14. //-------------------------------------------------------------
  15. const int nfrac = 4;                // 4 fraction objects
  16.  
  17. void print1(Fraction* F, int n);    // print in various formats
  18.  
  19. void sort1(Fraction* F, int n);     // sort and print
  20.  
  21. //--------------------------------------------------------------
  22.  
  23. void main()
  24. {  Fraction f[nfrac];               // in array for convenience
  25. // user input values for Fraction objects
  26.    int i;
  27.    cout << "Enter 4 fractions (e.g.  1/2  -4/6  3  8/5 ):  ";
  28.    for ( i = 0; i < nfrac; i++)
  29.      cin >> f[i];                         // overloaded >>
  30.    Fraction a = f[0];               // save for future use
  31.    Fraction b = f[1];               // save for future use
  32.    Fraction c = f[2];               // save for future use
  33.    Fraction d = f[3];               // save for future use
  34. // manipulate and print array of Fractions
  35.    print1(f, nfrac);
  36.    sort1(f, nfrac);
  37. // arithmetic on Fractions a, b, c, d
  38.    cout << "ARITHMETIC EXPRESSIONS:"<< endl;
  39.    cout << "binary +      : a + b \t\t\t= "
  40.         << (a + b) << endl;
  41.    cout << "binary *      : c * d \t\t\t= "
  42.         << (c * d) << endl;
  43.    cout << "binary * - /  : a * b - c / d\t\t= "
  44.         << (a * b - c / d) << endl;
  45.    cout << "mixed mode    : a + 2.5 - d\t\t= "
  46.         << (a + 2.5 - d) << endl;
  47.    cout << "unary -       : -b\t\t\t= "
  48.         << (-b) << endl;
  49.    cout << "prefix ++     : ++d\t\t\t= "
  50.         << (++d) << "\t\td =\t"
  51.         << d << endl;
  52.    cout << "postfix --    : d--\t\t\t= "
  53.         << (d--) << "\t\td =\t"
  54.         << d << endl;
  55.    cout << "unary +       : +d\t\t\t= "
  56.         << (+d) << endl;
  57.    cout << "mixed mode    : 2 / b + 0.125 * c \t= "
  58.         << (2 / b + 0.125 * c) << endl;
  59.    cout << "average       : (a + b + c + d)/4\t= "
  60.         << ((a + b + c + d)/4) << endl;
  61.    cout << "END";
  62. }
  63.  
  64. //--------------------------------------------------------------
  65. // Print elements of array f of n Fractions:
  66. //   as improper and proper fractions
  67. //   inverted, converted to floating point and truncated to int
  68. //--------------------------------------------------------------
  69. void print1(Fraction* f, int n)
  70. {  int i;
  71.    cout << endl << "\t\t\ta\tb\tc\td" << endl;
  72.    cout << "IMPROPER FRACTION:\t";
  73.    for( i = 0; i < n; i++)               // output
  74.      cout << f[i] << "\t";                      // overloaded <<
  75.    cout << endl <<"  PROPER FRACTION:\t";
  76.    for( i = 0; i < n; i++)               // display as proper
  77.      f[i].display(cout) << "\t";               // member function
  78.    cout << endl <<"          INVERSE:\t";
  79.    for( i = 0; i < n; i++)               // print the inverse
  80.    {  print(f[i].invert(), cout);        // friend function
  81.       cout << "\t";
  82.       f[i].invert(); }                        // member function
  83.    cout << endl << "             REAL:\t";
  84.    for( i = 0; i < n; i++)               // convert to double
  85.      cout << real(f[i]) << "\t";              // friend function
  86.    cout << endl << "            TRUNC:\t";
  87.    for( i = 0; i < n; i++)               // truncate to int
  88.      cout << trunc(f[i]) << "\t";             // friend function
  89.    cout << endl << endl;
  90. }
  91.  
  92. //--------------------------------------------------------------
  93. // A bubble sort to order array f of n Fractions
  94. // and print the elements in ascending order
  95. //--------------------------------------------------------------
  96. void sort1(Fraction* f, int n)
  97. {  int i,j;
  98.    Fraction swap;                           // local object
  99.    for( i = 0; i < n-1; i++)                // bubble sort
  100.      for( j = 1; j < n-i; j++)                 // overloaded
  101.        if( f[j-1] > f[j] )                     // operators <  =
  102.        { swap = f[j]; f[j] = f[j-1]; f[j-1] = swap; }
  103.    cout << " SORTED ASCENDING:\t";
  104.    cout << f[0];
  105.    for( i = 1; i < n; i++)                    // overloaded > <<
  106.      cout << ( f[i] > f[i-1] ? " <\t" : " =\t" )
  107.            << f[i];
  108.    cout << endl << endl;
  109. };
  110.  
  111. //---------------------------------------------------------------
  112.