Question: Implement a simple stack-based post-fix calculator. Your program should accept integer operands and the +, -, and * operators. Expressions should be entered one item

Implement a simple stack-based post-fix calculator. Your program should accept integer operands and the +, -, and * operators. Expressions should be entered one item per line with = on the final line to trigger the calculation and output of the result. Your program should detect invalid expressions (e.g., too few operands, too many operands).

also here is my stack code:

public class Stack {

int size = 50; int top; int arr[]; public Stack() { top = -1; arr = new int[size]; } public void push(int value) { if (top == size) { System.out.println("Stack is full"); } else { top++; arr[top] = value; } }

public int pop(){ if (top == -1){ System.out.println("Stack is empty"); return -1; } else { top--; return arr[top + 1]; } } public int top() { if (top == -1) { System.out.println("Stack is empty"); return -1; } else{ return arr[top]; } } public int size(){ return top + 1; } public boolean isEmpty() { if (top == -1) { return true; } else { return false; } }

public boolean isFull() { if (top == size) { return true; } else { return false; } } public static void main(String args[]) { Stack cool = new Stack(); cool.push(10); cool.push(20); System.out.println("The stack size is: " + cool.size()); System.out.println("The top element in the stack is: " + cool.top()); System.out.println("Is the stack empty:" + cool.isEmpty()); cool.pop(); System.out.println("The top element in the stack is: " + cool.top()); cool.pop(); System.out.println("The stack size is: " + cool.size()); System.out.println("Is the stack empty:" + cool.isEmpty()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!