home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1701.CPP - main()
-
- //----------------------------------------------------------
- // Example of template container classes implenenting stacks.
- // This version includes exception handling and templates.
- //----------------------------------------------------------
- //----------------------------------------------------------
- // Exception handling is supported only by
- // the IBM C++ Set/2 compiler
- // Templates are not supported by
- // the Microcoft Visual C++ compiler
- // Two versions of this program are included:
- // EX1701I.EXE - for IBM CSet II
- // EX1701B.EXE - for Borland Turbo C++
- //----------------------------------------------------------
- // To run this program on an OS/2 machine:
- // move to an OS/2 window
- // [type] cd:\EXAMPLES\EX1701I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
- // Files in this example:
- // %F,15,EX17010.H%EX17010.H Stack.H base class stack
- // %F,15,EX17011.H%EX17011.H AStack.H derived array stack
- // %F,15,EX17011.CPP%EX17011.CPP AStack.CPP
- // %F,15,EX17012.H%EX17012.H LLStack.H derived linked list stack
- // %F,15,EX17012.CPP%EX17012.CPP LLStack.CPP
- // %F,15,EX17013.H%EX17013.H Exception.H
- // %F,15,EX17014.H%EX17014.H Iterator.H iterator class
- // EX1701.CPP this file -- main() with exception handling
- // %F,15,EX1701B.CPP%EX1701B.CPP main() without exception handling
- //----------------------------------------------------------
-
- #include <iostream.h>
- #include "EX17011.h"
- #include "EX17011.cpp"
- #include "EX17012.h"
- #include "EX17012.cpp"
-
- typedef struct {char string[33];} String32;
-
- typedef LLStack<int> iLLStack;
- typedef LLStackIter<int> iLLStackIter;
-
- typedef AStack<String32> s32AStack;
- typedef AStackIter<String32> s32AStackIter;
-
- static s32AStack stringStack;
- static iLLStack intStack;
-
-
- void Process()
- {
- int done = 0;
- char response = 'q';
- long numeric = 0;
- String32 string;
-
- do
- {
- cout << endl << endl;
- cout << " P(u)sh Int P(U)SH String" << endl;
- cout << " P(o)p Int P(O)P String" << endl;
- cout << " P(r)int Int stack P(R)INT String stack"
- << endl;
- cout << " (Q)uit" << endl;
- cout << endl << endl;
- cin >> response;
-
- switch (response)
- {
- case 'U':
- cout << "Enter a valid string: ";
- cin >> string.string;
- stringStack.Push( string);
- break;
-
- case 'u':
- cout << "Enter an integer: ";
- cin >> numeric;
- if ( ! cin) // test for cin error
- { cin.clear(); // reset cin error state
- cin >> response; // skip offending input
- break;
- }
- intStack.Push( numeric);
- break;
-
- case 'O':
- string = stringStack.Pop();
- cout << string.string << endl;
- break;
-
- case 'o':
- cout << intStack.Pop() << endl;
- break;
-
- case 'R':
- {
- s32AStackIter next( stringStack);
- for (int i = stringStack.NumElem(); i > 0; i--)
- {
- string = next();
- cout << string.string << endl;
- }
- }
- break;
-
- case 'r':
- {
- iLLStackIter next( intStack);
- for (int i = intStack.NumElem(); i > 0; i--)
- {
- cout << next() << endl;
- }
- }
- break;
-
- case 'Q': case 'q':
- done = 1;
- break;
- }
- } while (!done);
- }
-
-
- void main()
- {
- try {
- Process();
- }
- catch( AStack<String32>::FullExcept)
- {
- cout << "Caught aa AStack<String32>::Full exception."
- << endl;
- }
- catch( AStack<String32>::EmptyExcept)
- {
- cout << "Caught an AStack<String32>::Empty exception."
- << endl;
- }
- catch( AStack<String32>::MemAlloc)
- {
- cout << "Caught an AStack<String32>::MemAlloc exception."
- << endl;
- }
- catch( LLStack<int>::FullExcept)
- {
- cout << "Caught an LLStack<int>::Full exception."
- << endl;
- }
- catch( LLStack<int>::EmptyExcept)
- {
- cout << "Caught an LLStack<int>::Empty exception."
- << endl;
- }
- catch( LLStack<int>::MemAlloc)
- {
- cout << "Caught an LLStack<int>::MemAlloc exception."
- << endl;
- }
- catch( ...)
- { }
- }
-