Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the code already given in ArrayStack and build a postfix calculator using templates and stack. Evaluate expression 234+*. #ifndef _ARRAY_STACK #define _ARRAY_STACK #include #include

Implement the code already given in ArrayStack and build a postfix calculator using templates and stack. Evaluate expression 234+*.

#ifndef _ARRAY_STACK #define _ARRAY_STACK #include #include "StackInterface.h" #include #include using namespace std;

const int MAX_STACK = 5; //const int MAX_STACK = maximum-size-of-stack;

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 #endif

/***********cpp file****************/

//#include // For assert #include //#include "ArrayStack.h" // Header file

template ArrayStack::ArrayStack() : top(-1) { } template bool ArrayStack::isEmpty() const // Copy constructor and destructor are supplied by the compiler { return top < 0; // end isEmpty }

template bool ArrayStack::push(const ItemType& newEntry) { bool result = false; if (top < MAX_STACK - 1) // Does stack have room for newEntry? { top++; items[top] = newEntry; result = true; } // end if return result; } // end push 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 return items[top]; // Stack is not empty; return top } // end peek // End of implementation file.

template void ArrayStack::setexpr(char *str) { s = str ; }

/*************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 stackhas 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; }; // end StackInterface #endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

6. Describe to a manager the different types of distance learning.

Answered: 1 week ago