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

  1. // \EXAMPLES\EX1701B.CPP
  2.  
  3. //----------------------------------------------------------
  4. // Example of template container classes implenenting stacks.
  5. // This version includes templates but no exception handling.
  6. //----------------------------------------------------------
  7. //----------------------------------------------------------
  8. //  Exception handling is supported only by
  9. //                      the IBM C++ Set/2 compiler
  10. //  Templates are not supported by
  11. //                      the Microcoft Visual C++ compiler
  12. //  Two versions of this program are included:
  13. //                      EX1701I.EXE - for IBM CSet II
  14. //                      EX1701B.EXE - for Borland Turbo C++
  15. //----------------------------------------------------------
  16. // Files in this example:
  17. // %F,15,EX17010B.H%EX17010B.H       Stack.H       base class stack
  18. // %F,15,EX17011B.H%EX17011B.H       AStack.H      derived array stack
  19. // %F,15,EX17011B.CPP%EX17011B.CPP     AStack.CPP
  20. // %F,15,EX17012B.H%EX17012B.H       LLStack.H     derived linked list stack
  21. // %F,15,EX17012B.CPP%EX17012B.CPP     LLStack.CPP
  22. // %F,15,EX17013B.H%EX17013B.H       Exception.H
  23. // %F,15,EX17014B.H%EX17014B.H       Iterator.H       iterator class
  24. // EX1701B.CPP      this file -- main() without handling
  25. // %F,15,EX1701.CPP%EX1701.CPP       main() with exception handling
  26. //----------------------------------------------------------
  27.  
  28. #include <stdlib.h>
  29. #include <iostream.h>
  30. #include "EX17011B.h"
  31. #include "EX17011B.cpp"
  32. #include "EX17012B.h"
  33. #include "EX17012B.cpp"
  34.  
  35. typedef struct {char string[33];} String32;
  36.  
  37. typedef LLStack<int> iLLStack;
  38. typedef LLStackItr<int> iLLStackIter;
  39.  
  40. typedef AStack<String32> s32AStack;
  41. typedef AStackItr<String32> s32AStackIter;
  42.  
  43. iLLStack intStack;
  44. s32AStack stringStack;
  45.  
  46.  
  47. void Process()
  48. {
  49.    int done = 0;
  50.    char response = 'q';
  51.    long numeric = 0;
  52.    String32 string;
  53.    cout << "Use lower case (u o r) for stack of integers.";
  54.    cout << endl;
  55.    cout << "User upper case (U O R) for stack of strings.";
  56.    cout << endl;
  57.  
  58.    do
  59.    {
  60.       cout << endl;
  61.       cout << " P(u)sh  Int            P(U)SH  String" << endl;
  62.       cout << " P(o)p   Int            P(O)P   String" << endl;
  63.       cout << " P(r)int Int stack      P(R)INT String stack"
  64.            << endl;
  65.       cout << "   q     quit       or    Q     quit   " << endl;
  66.       cin >> response;
  67.  
  68.       switch (response)
  69.       {
  70.          case 'U':
  71.             cout << "Enter a valid string: ";
  72.             cin >> string.string;
  73.             stringStack.Push( string);
  74.          break;
  75.  
  76.          case 'u':
  77.             cout << "Enter an integer: ";
  78.             cin >> numeric;
  79.             if ( ! cin)        // test for cin error
  80.             { cin.clear();     // reset cin error state
  81.               cin >> response; // skip offending input
  82.               break;
  83.             }
  84.             intStack.Push( numeric);
  85.          break;
  86.  
  87.          case 'O':
  88.             string = stringStack.Pop();
  89.             cout << string.string << endl;
  90.          break;
  91.  
  92.          case 'o':
  93.             cout << intStack.Pop() << endl;
  94.          break;
  95.  
  96.          case 'R':
  97.          {
  98.             s32AStackIter next( stringStack);
  99.             for (int i = stringStack.NumElem(); i > 0; i--)
  100.             {
  101.                string = next();
  102.                cout << string.string << endl;
  103.             }
  104.          }
  105.          break;
  106.  
  107.          case 'r':
  108.          {
  109.             iLLStackIter next( intStack);
  110.             for (int i = intStack.NumElem(); i > 0; i--)
  111.             {
  112.                cout << next() << endl;
  113.             }
  114.          }
  115.          break;
  116.  
  117.          case 'Q': case 'q':
  118.              done = 1;
  119.          break;
  120.       }
  121.    } while (!done);
  122. }
  123.  
  124.  
  125. void main()
  126. {
  127.     Process();
  128. }
  129.