home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 7-2.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  975b  |  38 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. #include <iostream.h>
  7.  
  8. class Complex {
  9. public:
  10.     int operator<(const Complex&);
  11. };
  12.  
  13. ostream& operator<<(ostream&, const Complex&);
  14. istream& operator>>(istream&, const Complex&);
  15.  
  16. template <class S>
  17. void sort(S elements[], const int nelements) {
  18.     int flip = 0, sz = nelements - 1;
  19.     do {
  20.         for (int j = 0, flip = 0; j < sz; j++) {
  21.             if (elements[j] < elements[j+1]) {
  22.                 S t = elements[j+1];
  23.                 elements[j+1] = elements[j];
  24.                 elements[j] = t;
  25.                 flip++;
  26.             }
  27.         }
  28.     } while (flip);
  29. }
  30.  
  31. int main() {
  32.     Complex cvec[12];
  33.     for (int i = 0; i < 12; i++) cin >> cvec[i];
  34.     sort(cvec, 12);    // calls sort(Complex[], const int)
  35.     for (i = 0; i < 12; i++) cout << cvec[i] << endl;
  36.     return 0;
  37. }
  38.