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

  1. // \EXAMPLES\EX1502.CPP
  2. //--------------------------------------------------------------
  3. // client code to exercise templates ASTACK and ASTACKITR
  4. //   The Microsoft Visual C++ compiler does not support templates.
  5. //   This program does not compile under Microsoft Visual C++.
  6. //--------------------------------------------------------------
  7.  
  8. //  files in this example:
  9. // %F,15,EX15021.H%EX15021.H      definition of templates ASTACK and ASTACKITR
  10. // %F,15,EX15022.H%EX15022.H      definition of String class
  11. // %F,15,EX15021.CPP%EX15021.CPP    member functions of ASTACK
  12. // EX1502.CPP     this file
  13.  
  14. //--------------------------------------------------------------
  15. #include <iostream.h>
  16.  
  17. #include "EX15021.h"        // definition of Stack and StackIter
  18. #include "EX15021.cpp"      // members of Stack StackIter
  19. #include "EX15022.h"         // definition of String class
  20.  
  21. //--------------------------------------------------------------
  22. // main()    user interactive pops and pushed items on a stack
  23. //--------------------------------------------------------------
  24. void main()
  25. {  AStack<String> MyStack;         // declare a stack
  26.    char buffer[80];                // used to input Stings
  27.    char response = ' ';            // user commands:
  28.    cout << "P(U)SH"  << endl;      //  U or u --> push item
  29.    cout << "P(O)P"   << endl;      //  O or o --> pop item
  30.    cout << "P(R)INT" << endl;      //  R or r --> print stack
  31.    cout << " (Q)uit" << endl;      //  Q or q --> quit
  32.    cout << endl;
  33.    do
  34.    {  cout << "Enter a command: ( U O R or Q ):  ";
  35.       cin >> response;
  36.       switch (response)
  37.       {  case 'u':                 // push - read string and push
  38.          case 'U': { cout << "Enter a word: ";
  39.                      cin >> buffer;
  40.                      String word(buffer);
  41.                      MyStack.Push( word);
  42.                      break; }
  43.          case 'o':                 // pop - pop string and print
  44.          case 'O': { String word = MyStack.Pop();
  45.                       cout << word << endl;
  46.                       break; }
  47.          case 'r':                 // print - iterate and print
  48.          case 'R': { AStackItr<String> next( MyStack);
  49.                      for (int i = MyStack.NumElem(); i > 0; i--)
  50.                      {  String word = next();
  51.                         cout << word << endl; }
  52.                      cout << "*END*" << endl;
  53.                      break; }
  54.          case 'q': response = 'Q'; // quit
  55.          case 'Q': break;
  56.          default : cout << response << " ? ";
  57.                    break;
  58.       }
  59.    } while ( response != 'Q' );
  60. }
  61. //--------------------------------------------------------------
  62.