// This is file STAKDEMO.CPP. To make it easier to try out this demo // program, the Stack class specification and implementation files are // included in this file so that it is not necessary to create a project. /* Stack.h provides a Stack class. * * Basic operations: * Constructor: Constructs an empty stack * empty: Checks if a stack is empty * push: Modifies a stack by adding a value at the top * top: Accesses the top stack value; leaves stack unchanged * pop: Modifies a stack by removing the value at the top * display: Displays all the stack elements * Class Invariant: * 1. The stack elements (if any) are stored in positions * 0, 1, . . ., myTop of myArray. * 2. -1 <= myTop < STACK_CAPACITY ------------------------------------------------------------------*/ #include using namespace std; template class Stack { /***** Function Members *****/ public: /* --- Constructor --- * * Precondition: A stack has been declared. * Postcondition: The stack has been constructed as an * empty stack. ************************************************************/ Stack(); /* --- Is the Stack empty? --- * Receive: Stack containing this function (implicitly) * Returns: True iff the Stack containing this function is empty *****************************************************************/ bool empty() const; /* --- Is the Stack full? --- * Receive: Stack containing this function (implicitly) * Returns: True iff the Stack containing this function is full *****************************************************************/ bool full() const; /* --- Add a value to the stack --- * * Receive: The Stack containing this function (implicitly) * A value to be added to a Stack * Return: The Stack (implicitly), with value added at its * top, provided there's space * Output: "Stack full" message if no space for value *************************************************************/ void push(const StackElement & value); /* --- Display values stored in the stack --- * * Receive: The Stack containing this function (implicitly) * The ostream out * Output: The Stack's contents, from top down, to out *************************************************************/ void display(ostream & out) const; /* --- Return value at top of the stack --- * * Receive: The Stack containing this function (implicitly) * Return: The value at the top of the Stack, if nonempty; * else a "garbage value" * Output: "Stack empty" message if stack is empty *************************************************************/ StackElement top() const; /* --- Remove value at top of the stack --- * * Receive: The Stack containing this function (implicitly) * Return: The Stack containing this function (implicitly) * with its top value (if any) removed * Output: "Stack-empty" message if stack is empty. *************************************************************/ void pop(); /***** Data Members *****/ private: StackElement myArray[STACK_CAPACITY]; int myTop; }; // end of class declaration //--- Definition of Class Constructor template inline Stack::Stack() { myTop = -1; } //--- Definition of empty template inline bool Stack::empty() const { return (myTop == -1); } /* Stack.cpp -- implementation file for Stack.h */ //#include "Stack.h" #include using namespace std; //--- Definition of push() template void Stack :: push(const StackElement & value) { if (myTop < STACK_CAPACITY - 1) // Preserve stack invariant { ++myTop; myArray[myTop] = value; } // or simply, myArray[++myTop] = value; else cerr << "*** Stack is full -- can't add new value ***\n" << "Must increase value of STACK_CAPACITY in Stack.h\n"; } //--- Definition of display() template void Stack::display(ostream & out) const { for (int i = myTop; i >= 0; i--) out << myArray[i] << endl; } //--- Definition of top() template StackElement Stack::top() const { if (myTop >= 0) return myArray[myTop]; cerr << "*** Stack is empty ***\n"; } //--- Definition of pop() template void Stack::pop() { if (myTop >= 0) // Preserve stack invariant myTop--; else cerr << "*** Stack is empty -- can't remove a value ***\n"; } //--- Definition of full() template bool Stack :: full ( ) const { return !(myTop < STACK_CAPACITY - 1); } //----------------------------------------------------------------------------- // This program simulates stack operations interactively. The user can insert // a number into the stack or delete a number from the stack to trace the // operation of a stack. The stack declaration and operations are provided in // the class Stack, as shown in your text. // This is file STAKDEMO.CPP. //----------------------------------------------------------------------------- #include #include // contains definition of toupper () //#include "stack.h" // stack class from the text using namespace std; const int SIZE = 5; void PrintStack (Stack s); void main () { Stack s; char command; bool pgmInProgress = true; int number; do { cout << "Stack Simulation\n\n"; cout << "Type A to push a number onto the stack\n"; cout << "Type D to pop a number off of the stack\n"; cout << "Type P to print the stack\n"; cout << "Type Q to quit\n"; cout << "Command ? : "; cin >> command; cin.get(); // eat the newline switch (toupper (command)) { case 'A' : if (s.full()) { cout << "\nError -- stack is full\n"; break; } cout << "\nType an integer : "; cin >> number; cin.get(); // eat the newline cout << endl; s.push(number); break; case 'D' : if (s.empty()) { cout << "\nError -- stack is empty\n"; break; } number = s.top(); s.pop(); cout << "\nThe number removed is " << number << endl; break; case 'P' : PrintStack(s); break; case 'Q' : pgmInProgress = false; break; default : ; // do nothing } // end of switch on command cout << endl; } while (pgmInProgress); // end of while program in progress loop } // end of main function //-------------------------------- P R I N T ---------------------------------- // // POST: Logical contents of stack array are displayed horizontally with the // top of the stack to the left. Since the stack parameter is passed only by // value, any changes made to the stack are strictly local. // ---------------------------------------------------------------------------- void PrintStack (Stack s) { if (s.empty()) { cout << "\nStack is empty\n"; return; } cout << "\ntop ------------------ bottom\n"; while (! s.empty()) { cout << s.top() << " "; s.pop(); } cout << endl; }