Question: create an ADT Stack class called myStack that will be used to implement a postfix calculator. class myStack { public: myStack (); myStack(const myStack &);
create an ADT Stack class called myStack that will be used to implement a postfix calculator.
class myStack { public: myStack (); myStack(const myStack &); const myStack& operator =(const myStack &); ~myStack (); void push(int); void pop (); void pop(int &); int top() const; bool isEmpty () const; private: size_t length; int * stk; };
size_t length - the size of the stack int * stk - is a pointer that points to a dynamic array which contains the contents of the stack myStack::myStack() - default constructor that sets stk to NULL and length to 0 myStack::myStack(const myStack& copy) - copy constructor, will copy the content of copy.length into length and will perform a deep copy of copy.stk into stk const myStack& myStack::operator=(const myStack& rhs) - overloads the assignment operator which will copy rhs.length into length and perform a deep copy of rhs.stk into stk 1 myStack::~myStack() - the destructor which will deallocate stk void myStack::push(int item) - implements the push operation of the ADT Stack, this operation will increase the array stk by 1 element, then increment length by 1, and then assign the value of item to the end of the array stk void myStack::pop() - implements the pop operation of the ADT Stack, it will shrink the array stk by 1 element, i.e. the end element (top element) will be removed, and length will be decremented by 1 (shrinking an array is quite similar to lengthening an array) void myStack::pop(int& item) - implements the pop operation of the ADT Stack, it will insert the top element into item parameter, and then will shrink the array stk by 1 element, i.e. the end element (top element) will be removed, and length will be decremented by 1 (shrinking an array is quite similar to lengthening an array) int myStack::top() const - returns the top element on the stack without removing it, i.e. it returns stk[length] bool myStack::isEmpty() const - returns true if the stack is empty and false otherwise
Contents of main In main, you will perform the following steps to implement the stack based postfix calculator Read in the string, using getline(cin, line) would be a good idea Traverse each character from the string line one at a time, for each possible character do the following ignore and skip to the next character if it is a whitespace push the character onto myStack if it is a digit pop two numbers off myStack and perform the operation on the two elements and push the result onto myStack (if you are unable to pop two elements output an error and terminate the program) output an error and terminate the program if the character is not a digit or an operator symbol + - * / Once you parsed the entire string, the answer will be on top of the stack, and this item must be the ONLY element on the stack, thus is there are more than one item on the stack after parsing the input string, out the error
Example output:
bash -3.2$ ./a.out
Enter a postfix string: 34+2 -8*
The answer is 40
bash -3.2$ ./a.out
Enter a postfix string: 34+T
Error! Invalid character
bash -3.2$ ./a.out
Enter a postfix string: 3456126++++++
The answer is 27
bash -3.2$ ./a.out
Enter a postfix string: 34
Invalid postfix expression
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
