Professional Documents
Culture Documents
repeat
If operator stack is empty, push next op onto stack.
Else if precedence(next op) > precedence(top operator)
Push next op onto the stack (ensures higher precedence operators evaluated first)
Else
Pop the operator stack and append operator to postfix string
Until next op is pushed onto the stack.
Convert:
x – y * a + b / c becomes x y a * - b c / +
operator precedence: *, / 2
+, - 1
Let’s consider the effect of parentheses:
'(' indicates the start of an expression that is evaluated separately – just push it on the
stack –Acts like a fence to separate the subexpression.
')' indicates the end of a subexpression. Pop off all operators and append them to postfix
string until you reach a '(' and then pop '(' -- don't append ')' and '(' to postfix string.
To have this happen seamlessly in the algorithm, give parentheses the lowest precedence
(, ) - 0 precedence.
Precedence: *, / 2
+, - 1
(, ) 0
next character is 3, postfix is “3”, next token is +, push it onto empty stack
| +
| + *
| -
| - * (
| - * ( +
postfix is “3 5 6 * + 7 8 5 +”
| - *
At end of string, keep popping stack until stack is empty
Postfix form: “3 5 6 * + 7 8 5 + * -“
Expression evaluation assignment.
Using 2 stacks you can evaluate an infix expression in 1 pass without converting to
postfix first:
Algorithm
1. Create an empty operator stack
2. Create an empty operand stack
3. Repeat
3.1 Get the next token in the infix string
3.2 If next token is an operand, place it on the operand stack
3.3 If next token is an operator
3.3.1 Evaluate the operator (next op)
Until at the end of the string
4. while operator stack is not empty
pop operator and operands (left and right),
evaluate left operator right
and push result onto operand stack
5. Pop result from operator stack
Algorithm
Set done to false
Repeat
If operator stack is empty or next op is ‘(‘,
push next op onto stack and set done to true
Else if precedence(next op) > precedence(top operator)
Push next op onto the stack (ensures higher precedence operators evaluated first)
and set done to true
Else
Pop the operator stack
If operator popped is ‘(‘,
set done to true.
Else
pop right and left from operand stack
evaluate left operator right
push result onto operand stack
until done
Evaluate: 3 + 5 * 6 – 7 * (8 + 5) = using an operator stack and an operand stack.
Operator = denotes the end of the expression and has precedence of –1.
At end of string. One way to “detect” end of string is to use an operator symbol = with
precedence of –1.Because its precedence is < anything else, all operators will be popped
from the operator stack and evaluated automatically.
Assignment: Do this using the applet provided for expression evaluation. The applet has
calls to method processOperator() and processDigit() which are part of class Calculator.
Both methods take a character argument, the current character to be processed. Method
processDigit() is called each time a digit character key is pressed. If the next character is
a digit you should add it to the number being accumulated in numValue (10 * numValue
+ (int) token - (int) '0'). Method processOperator() is called each time a non-digit key is
pressed. If the character just pressed is not a digit and the previous character was a digit,
method processOperator() should push the number in numValue onto the operand stack
and then process the operator .
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Stack;
return;
}