/** @file StackInterface.h */
#ifndef STACK_INTERFACE_ #define STACK_INTERFACE_
template class StackInterface { public: /** Sees whether this stack is empty. @return True if the stack is empty, or false if not. */ virtual bool isEmpty() const = 0; /** Adds a new entry to the top of this stack. @post If the operation was successful, newEntry is at the top of the stack. @param newEntry The object to be added as a new entry. @return True if the addition is successful or false if not. */ virtual bool push(const ItemType& newEntry) = 0; /** Removes the top of this stack. @post If the operation was successful, the top of the stack has been removed. @return True if the removal is successful or false if not. */ virtual bool pop() = 0; /** Returns the top of this stack. @pre The stack is not empty. @post The top of the stack has been returned, and the stack is unchanged. @return The top of the stack. */ virtual ItemType peek() const = 0; /** Destroys object and frees memory allocated by object. */ virtual ~StackInterface() { } }; // end StackInterface #endif
/** ADT stack: Array-based implementation. Listing 7-1 @file ArrayStack.h */
#ifndef ARRAY_STACK_ #define ARRAY_STACK_
#include "StackInterface.h"
const int MAX_STACK = 5;
template class ArrayStack : public StackInterface { private: ItemType items[MAX_STACK]; // Array of stack items int top; // Index to top of stack public: ArrayStack(); // Default constructor bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); ItemType peek() const; }; // end ArrayStack
/** Listing 7-1 @file ArrayStack.cpp */ #include // For assert
template ArrayStack::ArrayStack() : top(-1) { } // end default constructor
// Copy constructor and destructor are supplied by the compiler
template bool ArrayStack::isEmpty() const { return top template bool ArrayStack::push(const ItemType& newEntry) { bool result = false; if (top template bool ArrayStack::pop() { bool result = false; if (!isEmpty()) { top--; result = true; } // end if return result; } // end pop
template ItemType ArrayStack::peek() const { assert(!isEmpty()); // Enforce precondition // Stack is not empty; return top return items[top]; } // end peek // End of implementation file.
#endif
#include #include #include "ArrayStack.h" using namespace std; int main() { ArrayStack stack; string items[5] = {"one", "two", "three", "four", "five"}; for (int i = 0; i Submit all files containing C++ code from Part II of the assignment. This should include:
your application file
the StackInterface file
the StackArray class template file
Write a postfix ealculator application a5 described in section 6.3.1 of you texcbook. Part I Initially. yve do not need un dos any error checking - you can assume that the irpet pastlix expression ix syntactically coirrect. Luput - the speralss ' + ', '-'. 'z', and 'r - the dipits '0' through 9 ' - Lhe spare (talanki) chiarater" " Example input: 243++ I'rucessing if (el la a ianik) ingore if expression. Your program should include a loop so that the user can evaluate multiple pastif expressions. Your cutput might lookk like: Yoar progran should perforn iateger calcalations only. Do not use floating-point (lloat or double) variables for your calculations. Stack code inte a single file. Ihis roduces the total number of files that you need and makes it casier for you to add all the bibes to your lroyect or Makefile. Imporbunt: You should increase tha maximum siye of the stack to sonething like 32 . Note an the C-+ assert statement Note on writhug applicatious Hint: ehar token; int value; if ( taken ia an aigit