home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1701B.CPP
-
- //----------------------------------------------------------
- // Example of template container classes implenenting stacks.
- // This version includes templates but no exception handling.
- //----------------------------------------------------------
- //----------------------------------------------------------
- // 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++
- //----------------------------------------------------------
- // Files in this example:
- // %F,15,EX17010B.H%EX17010B.H Stack.H base class stack
- // %F,15,EX17011B.H%EX17011B.H AStack.H derived array stack
- // %F,15,EX17011B.CPP%EX17011B.CPP AStack.CPP
- // %F,15,EX17012B.H%EX17012B.H LLStack.H derived linked list stack
- // %F,15,EX17012B.CPP%EX17012B.CPP LLStack.CPP
- // %F,15,EX17013B.H%EX17013B.H Exception.H
- // %F,15,EX17014B.H%EX17014B.H Iterator.H iterator class
- // EX1701B.CPP this file -- main() without handling
- // %F,15,EX1701.CPP%EX1701.CPP main() with exception handling
- //----------------------------------------------------------
-
- #include <stdlib.h>
- #include <iostream.h>
- #include "EX17011B.h"
- #include "EX17011B.cpp"
- #include "EX17012B.h"
- #include "EX17012B.cpp"
-
- typedef struct {char string[33];} String32;
-
- typedef LLStack<int> iLLStack;
- typedef LLStackItr<int> iLLStackIter;
-
- typedef AStack<String32> s32AStack;
- typedef AStackItr<String32> s32AStackIter;
-
- iLLStack intStack;
- s32AStack stringStack;
-
-
- void Process()
- {
- int done = 0;
- char response = 'q';
- long numeric = 0;
- String32 string;
- cout << "Use lower case (u o r) for stack of integers.";
- cout << endl;
- cout << "User upper case (U O R) for stack of strings.";
- cout << endl;
-
- do
- {
- cout << 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 quit or Q quit " << 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()
- {
- Process();
- }
-