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

  1. // \EXAMPLES\EX1701.CPP       - main()
  2.  
  3. //----------------------------------------------------------
  4. // Example of template container classes implenenting stacks.
  5. // This version includes exception handling and templates.
  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. //  To run this program on an OS/2 machine:
  17. //                      move to an OS/2 window
  18. //              [type]  cd:\EXAMPLES\EX1701I
  19. //                      where cd: is the drive of the CD-ROM
  20. //----------------------------------------------------------
  21. // Files in this example:
  22. // %F,15,EX17010.H%EX17010.H       Stack.H       base class stack
  23. // %F,15,EX17011.H%EX17011.H       AStack.H      derived array stack
  24. // %F,15,EX17011.CPP%EX17011.CPP     AStack.CPP
  25. // %F,15,EX17012.H%EX17012.H       LLStack.H     derived linked list stack
  26. // %F,15,EX17012.CPP%EX17012.CPP     LLStack.CPP
  27. // %F,15,EX17013.H%EX17013.H       Exception.H
  28. // %F,15,EX17014.H%EX17014.H       Iterator.H       iterator class
  29. // EX1701.CPP      this file -- main() with exception handling
  30. // %F,15,EX1701B.CPP%EX1701B.CPP     main() without exception handling
  31. //----------------------------------------------------------
  32.  
  33. #include <iostream.h>
  34. #include "EX17011.h"
  35. #include "EX17011.cpp"
  36. #include "EX17012.h"
  37. #include "EX17012.cpp"
  38.  
  39. typedef struct {char string[33];} String32;
  40.  
  41. typedef LLStack<int> iLLStack;
  42. typedef LLStackIter<int> iLLStackIter;
  43.  
  44. typedef AStack<String32> s32AStack;
  45. typedef AStackIter<String32> s32AStackIter;
  46.  
  47. static s32AStack stringStack;
  48. static iLLStack intStack;
  49.  
  50.  
  51. void Process()
  52. {
  53.    int done = 0;
  54.    char response = 'q';
  55.    long numeric = 0;
  56.    String32 string;
  57.  
  58.    do
  59.    {
  60.       cout << endl << 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)uit" << endl;
  66.       cout << endl << endl;
  67.       cin >> response;
  68.  
  69.       switch (response)
  70.       {
  71.          case 'U':
  72.             cout << "Enter a valid string: ";
  73.             cin >> string.string;
  74.             stringStack.Push( string);
  75.          break;
  76.  
  77.          case 'u':
  78.             cout << "Enter an integer: ";
  79.             cin >> numeric;
  80.             if ( ! cin)        // test for cin error
  81.             { cin.clear();     // reset cin error state
  82.               cin >> response; // skip offending input
  83.               break;
  84.             }
  85.             intStack.Push( numeric);
  86.          break;
  87.  
  88.          case 'O':
  89.             string = stringStack.Pop();
  90.             cout << string.string << endl;
  91.          break;
  92.  
  93.          case 'o':
  94.             cout << intStack.Pop() << endl;
  95.          break;
  96.  
  97.          case 'R':
  98.          {
  99.             s32AStackIter next( stringStack);
  100.             for (int i = stringStack.NumElem(); i > 0; i--)
  101.             {
  102.                string = next();
  103.                cout << string.string << endl;
  104.             }
  105.          }
  106.          break;
  107.  
  108.          case 'r':
  109.          {
  110.             iLLStackIter next( intStack);
  111.             for (int i = intStack.NumElem(); i > 0; i--)
  112.             {
  113.                cout << next() << endl;
  114.             }
  115.          }
  116.          break;
  117.  
  118.          case 'Q': case 'q':
  119.              done = 1;
  120.          break;
  121.       }
  122.    } while (!done);
  123. }
  124.  
  125.  
  126. void main()
  127. {
  128.     try {
  129.          Process();
  130.     }
  131.     catch( AStack<String32>::FullExcept)
  132.     {
  133.        cout << "Caught aa AStack<String32>::Full exception."
  134.             << endl;
  135.     }
  136.     catch( AStack<String32>::EmptyExcept)
  137.     {
  138.        cout << "Caught an AStack<String32>::Empty exception."
  139.             << endl;
  140.     }
  141.       catch( AStack<String32>::MemAlloc)
  142.     {
  143.        cout << "Caught an AStack<String32>::MemAlloc exception."
  144.             << endl;
  145.     }
  146.     catch( LLStack<int>::FullExcept)
  147.     {
  148.        cout << "Caught an LLStack<int>::Full exception."
  149.             << endl;
  150.     }
  151.     catch( LLStack<int>::EmptyExcept)
  152.     {
  153.        cout << "Caught an LLStack<int>::Empty exception."
  154.             << endl;
  155.     }
  156.      catch( LLStack<int>::MemAlloc)
  157.     {
  158.        cout << "Caught an LLStack<int>::MemAlloc exception."
  159.             << endl;
  160.     }
  161.     catch( ...)
  162.     { }
  163.    }
  164.