Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1.02 KB

Ex_1_3_10.md

File metadata and controls

49 lines (36 loc) · 1.02 KB
title date draft tags categories
Algorithm4 Java Solution 1.3.10
2019-07-04 05:47:10 +0800
false
JAVA
TECH
archives

1.3.10

Problem:

Write a filter InfixToPostfix that converts an arithmetic expression from infix to postfix

Solution:

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

Reference:

zhihu