Question: Modify the postfix evaluator program of Exercise 21.13 so that it can process integer operands larger than 9. Exercise 21.13 Write class PostfixEvaluator that evaluates
Modify the postfix evaluator program of Exercise 21.13 so that it can process integer operands larger than 9.
Exercise 21.13
Write class PostfixEvaluator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / -
The program should read a postfix expression consisting of digits and operators into a StringBuffer. The program should read the expression and evaluate it (assume it’s valid). The algorithm to evaluate a postfix expression is showing in Fig. 21.20.
Fig. 21.20

In lines 4–12 above (based on the sample expression at the beginning of this exercise), if the operator is '/', the top of the stack is 4 and the next element in the stack is 40, then pop 4 into x, pop 40 into y, evaluate 40 / 4 and push the result, 10, back on the stack. This note also applies to other operators. The arithmetic operations allowed in an expression are: + (addition), - (subtraction), * (multiplication), / (division), ^ (exponentiation) and % (remainder).
123 4 Until the right parenthesis is encountered, read the expression from left to right. If the current character is a digit, do the following: 5 6 7 8 9 10 11 Append a right parenthesis ) to the end of the postfix expression. When the right-parenthesis character is encountered, no further processing is necessary. 12 13 14 15 Push its integer value onto the stack (the integer value of a digit character is its value in the Unicode character set minus the value of '0' in Unicode). Otherwise, if the current character is an operator: Pop the two top elements of the stack into variables x and y. Calculate y operator x. Push the result of the calculation onto the stack. When the right parenthesis is encountered in the expression, pop the top value of the stack. This is the result of the postfix expression.
Step by Step Solution
There are 3 Steps involved in it
In the given description for the postfix evaluator the original program assumes operands are singledigit integers To modify the algorithm to handle in... View full answer
Get step-by-step solutions from verified subject matter experts
