Write a java program called RevePol.java to calculate the value of an integer ex
ID: 670447 • Letter: W
Question
Write a java program called RevePol.java to calculate the value of an integer expression in Reverse Polish Notation using a linked stack data structure to store intermediate values. Reverse Polish Notation is an alternative way of writing arithmetic expressions where the operator is supplied after the 2 arguments. For example, "3 2 +" is equivalent to the traditional "3+2" and "3 2 * 1 +" is equivalent to the traditional "(3*2)+1". Use the built-in Java data structures.
The basic algorithm is to scan over the expression (a space-separated list of symbols) from left to right and: when an integer is encountered, push it onto the stack;
when an operator is encountered, pop 2 integers off the stack, perform the operation and push the result back onto the stack; and
when no more symbols are left, pop the answer off the stack. The operators to be supported are +/*-. Note that "4 2 -" evaluates to 2 and "4 2 /" evaluates to 2. Your program must also cater for the following errors: Insufficient arguments for , where less than 2 arguments are provided. For example, "3 +".
Integer expected but not found, where a value is not an integer. For example, "3 f +".
Extra symbols in expression, where the expression has been fully processed but more than one value remains on the stack. For example, "3 2 + 4".
Insufficient symbols in expression, where the expression has been fully processed but there is no answer left on the stack. For example, "".
Sample Input/Output
3 3 * 4 2 - /
Answer: 4
Sample Input/Output
3 2 + *
Insufficient arguments for *
Sample Input/Output
Insufficient symbols in expression
Sample Input/Output
3 f +
Integer expected but not found
Sample Input/Output
3 2
Extra symbols in expression
Explanation / Answer
ReversePolish.java
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.