Converting infix to postfix 1. 2. 3. Set postfix string to empty string Create an empty operator stack Repeat 3.

1 Get the next token in the infix string 3.2 If next token is an operand (begins with digit), append it to postfix string 3.3 If next token is an operator 3.3.1 Process the next operator Until at the end of the string Process the next operator (next op)

3.3.1

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.

b c / + .Convert: x – y * a + b / c becomes operator precedence: *. .1 x y a * . / 2 +.

Pop off all operators and append them to postfix string until you reach a '(' and then pop '(' -.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.0 precedence.3. 3. set done to true Else append operator popped to postfix string Until done . 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 ‘(‘. give parentheses the lowest precedence (.don't append ')' and '(' to postfix string. ) . To have this happen seamlessly in the algorithm.1 Process the next operator (with parentheses) Set done to false Repeat If operator stack is empty or next op is ‘(‘. ')' indicates the end of a subexpression.

postfix is “3 5 6 * + 7”. push * next character is (. postfix is “3 5”. postfix is “3 5 6 * +”. (. next character is -. ) postfix string is null string. pop *pop + postfix is “3 5 6 *”. push it onto stack | . postfix is “3”.* ( + next character is 5. next token is +. | + next character still -.Convert: 3 + 5 * 6 – 7 * (8 + 5) to postfix 2 1 0 Precedence: *. next character is *. / +. pop and append all operators to postfix string until matching ‘(‘ is on top of stack. postfix is “3 5 6 * + 7 8” next character is ‘+’. push | next character is 7. push ( onto stack but give it lowest precedence | . operator stack is empty. postfix is “3 5 6”. then pop ‘(‘ and set done to true postfix is “3 5 6 * + 7 8 5 +” | . keep popping stack until stack is empty Postfix form: “3 5 6 * + 7 8 5 + * -“ . push * | + * next character is 6. pop +. next character is *. push it onto empty stack | + next character is 5.* ( next character is 8. operator stack is empty next character is 3.* At end of string. postfix is “3 5 6 * + 7 8 5“ next character is ‘)’.

3.2 If next token is an operand. Create an empty operator stack 2.Expression evaluation assignment.1 Evaluate the operator (next op) Algorithm Set done to false Repeat If operator stack is empty or next op is ‘(‘. Else pop right and left from operand stack evaluate left operator right push result onto operand stack until done . while operator stack is not empty pop operator and operands (left and right). Repeat 3.1 Get the next token in the infix string 3. evaluate left operator right and push result onto operand stack 5.1 Evaluate the operator (next op) Until at the end of the string 4. Using 2 stacks you can evaluate an infix expression in 1 pass without converting to postfix first: Algorithm 1. Create an empty operand stack 3. 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 ‘(‘. place it on the operand stack 3.3 If next token is an operator 3.3. Pop result from operator stack 3. set done to true.

* ( | 33 7 13 |. evaluate 3 + 30. 8 popped. pop * (at end of string) pop 13. 5 popped. push * 6. push + 5. action 3. Method processDigit() is called each time a digit character key is pressed. Stack affected |3. the current character to be processed. 7.* ( | 33 7 8 |.Evaluate: 3 + 5 * 6 – 7 * (8 + 5) = using an operator stack and an operand stack. Both methods take a character argument. push 3 next op is +. If the character just pressed is not a digit and the previous character was a digit. push * next op is (. 33 popped. pop ‘(‘ and discard At end of string. push – (stack is empty) 6. pop + 30. evaluate 33 – 91.Because its precedence is < anything else. evaluate 7 * 13. If the next character is a digit you should add it to the number being accumulated in numValue (10 * numValue + (int) token . pop * 6. pop + 5. method processOperator() should push the number in numValue onto the operand stack and then process the operator .* next character. Assignment: Do this using the applet provided for expression evaluation.* |. Operator = denotes the end of the expression and has precedence of –1. push 13 next op is ‘)’. |+ |3 5 |+ * |3 5 6 |+ | 3 30 | | 33 || 33 7 |. push 6 next op is -. 3 popped. push + 5. push = pop result –-51 Pop operator stack – it should be empty or there is an error. The applet has calls to method processOperator() and processDigit() which are part of class Calculator. push 91 next op is =. push 5 next op is *. One way to “detect” end of string is to use an operator symbol = with precedence of –1. operator stack is empty. push 8 next op is +. evaluate 8 + 5. push -58 next op is =. push 33 next op is -. push ( 8. evaluate 5 * 6. . Method processOperator() is called each time a non-digit key is pressed. pop 91. || 33 91 | | -58 |= | next op is ‘=’. all operators will be popped from the operator stack and evaluated automatically. push 5 next op is ‘)’. push 30 next op is -.* ( + | 33 7 8 5 |.(int) '0'). push 7 next op is *.

add(instruct). '1'.*. // add to panel buttons[r][c]. // Create and label each button. char [][] opMap = { {'0'. // Label for operand stack protected Label operatorLabel = new Label(). // Create an array of buttons of the proper dimensions. map. '+'}.length].applet. . r++) for (int c = 0. import java. add it to applet. '7'. for (int r = 0.awt. '='}. {'*'. // Label for result private Label infix = new Label(). // a panel in our applet // characters to be used as button labels. // Create the 6 x 3 grid of buttons map. '5'}. // and register applet as listener. public class ExpressionEval extends Applet implements ActionListener { private Button[][] buttons. c++) { // create labelled button .addActionListener(this). Calculator calc = new Calculator().take label from array opMap buttons[r][c] = new Button("" + opMap[r][c]).length. import java. // Label for each char of expression private String infixStr = "". '4'.awt. '/'. buttons[0]. ')'} }.length][opMap[0].Stack. {'-'.add(buttons[r][c]).*.event. // expression being evaluated protected Label operandLabel = new Label().length. '2'}. // array of Button objects. Panel map = new Panel(). } add(map). {'9'. r < buttons.length. import java. // Label for expression private Label nextChar = new Label(). {'3'.setLayout(new GridLayout(buttons. c < buttons[r].length)). '('. // Label for operator stack private Button reset. '8'}.util. {'6'.*. // Calculator object public void init() { Label instruct = new Label("Press = to end expression"). private Label result = new Label(). buttons = new Button[opMap. ' '.import java.

setText("Expression so far: " + infixStr).processOperator(token. // Append it to expression nextChar. add(infix). reset = new Button("Reset"). reset.addActionListener(this). else { String message = calc.setText(message). if (b == reset) { return.setText("________________________________________"). add(nextChar).nextChar.setText("________________________________________"). } char token = (b. result.charAt(0).setText("___________________________________________").getLabel()). // Gets the button pressed. operandLabel.processDigit(token).setText("___________________________________________"). add(result). result. add(operatorLabel). } } } . operatorLabel. add(reset).setText("Character is " + token). add(operandLabel).getSource(). // token is label character infixStr = infixStr + token. this).setText("___________________________________________"). infix. } // init public void clear() { } public void actionPerformed(ActionEvent e) { Button b = (Button)e. infix. // Process the token if (token >= '0' && token <= '9') calc.