Implement the code already given in ArrayStack and build a postfix calculator us
ID: 2246926 • Letter: I
Question
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 <stack>
#include "StackInterface.h"
#include <cmath>
#include <iostream>
using namespace std;
const int MAX_STACK = 5;
//const int MAX_STACK = maximum-size-of-stack;
template<class ItemType>
class ArrayStack : public StackInterface<ItemType>
{
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 <cassert> // For assert
#include <assert.h>
//#include "ArrayStack.h" // Header file
template<class ItemType>
ArrayStack<ItemType>::ArrayStack() : top(-1)
{
}
template<class ItemType>
bool ArrayStack<ItemType>::isEmpty() const // Copy constructor and destructor are supplied by the compiler
{
return top < 0; // end isEmpty
}
template<class ItemType>
bool ArrayStack<ItemType>::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<class ItemType>
bool ArrayStack<ItemType>::pop()
{
bool result = false;
if (!isEmpty())
{
top--;
result = true;
} // end if
return result;
} // end pop
template<class ItemType>
ItemType ArrayStack<ItemType>::peek() const
{
assert(!isEmpty()); // Enforce precondition
return items[top]; // Stack is not empty; return top
} // end peek
// End of implementation file.
template<class ItemType>
void ArrayStack<ItemType>::setexpr(char *str)
{
s = str ;
}
/*************StackInterface.h**************/
#ifndef _STACK_INTERFACE
#define _STACK_INTERFACE
template<class ItemType>
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
Explanation / Answer
<INCLUDE ALL NESSESARY FILES>
int main() {
ArrayStack<int> stack;
char* str = new char[100];
cout << "Enter postfix expr: ";
cin >> str;
for(int i = 0; str[i] != ''; i++) {
if (str[i] >= '0' && str[i] <= '9')
stack.push(str[i] - '0');
else {
int o2 = stack.peek();
stack.pop();
int o1 = stack.peek();
stack.pop();
int result;
if (str[i] == '+')
result = o1 + o2;
else if (str[i] == '-')
result = o1 - o2;
else if (str[i] == '*')
result = o1 * o2;
else if (str[i] == '/')
result = o1 / o2;
stack.push(result);
}
}
cout << "Result = " << stack.peek() << endl;
}
This code will do the trick...