title | date | draft | tags | categories | |||
---|---|---|---|---|---|---|---|
Algorithm4 Java Solution 1.3.10 |
2019-07-04 05:47:10 +0800 |
false |
|
|
Write a filter InfixToPostfix that converts an arithmetic expression from infix to postfix
1. new two stacks: stack_operator, stack_operand
2. scan expression from left to right
3. for ele in exprs:
case num: stack_operand.push(ele)
case ( : stack_operator.push(ele)
case ) : pop ele from stack_operator, until meet (, push ele stack_operand
case +,-: compare operator priority
case *,/: compare operator priority
4. compare operator priority
while true:
if stack_operator.isEmpty or stack_operator.peek.eq (
stack_operator.push ele
break
else if ele.pri > stack_operand.peek.pri
stack_operator.push(ele)
break
else // ele.pri <= stack_operand.peek.pri
op ele from stack_operator, push ele stack_operand