Inorder expression to postfix expression

For an arithmetic expression we generally write it like this

(3 + 4) × 5 - 6

This way of writing is an inorder expression 
and a postorder expression puts the operator after the operand, such as

3 4 + 5 × 6 -

It can be seen that there are no parentheses in the post-sequence expression, only the order of calculation is expressed, and this order happens to be the general calculation order in the calculator.

The specific method of using postfix expressions to calculate:

Build a stack S. Read the expression from left to right. If the operand is read, it will be pushed into the stack S. If the n-ary operator (that is, the operator that requires the number of parameters is n) is read, it will be taken from the top of the stack. The items are operated according to the operator, and then the result of the operation is pushed into the stack S instead of the n items at the top of the original stack. If the suffix expression has not been read, the above process is repeated, and the value at the top of the stack is finally output.

Example: 6 5 2 3 + 8 * + 3 + *

  • Push the previous number onto the stack first

    Stack: 6 5 2 3

  • When encountering "+", take the two operands at the top of the stack for addition, 2 + 3 = 5, push the stack

    Stack: 6 5 5

  • Encountered "8" on the stack

    Stack: 6 5 5 8

  • When encountering "*", take the two operands at the top of the stack for multiplication, 5 * 8 = 40, and push the stack

    Stacks: 6 5 40

  • When encountering "+", add the two operands at the top of the stack, 5 + 40 = 45, and push the stack

    Stacks: 6 45

  • Encountered "3" on the stack

    Stack: 6 45 3

  • When encountering "+", take the two operands at the top of the stack for addition, 45 + 3 = 48, push the stack

    Stacks: 6 48

  • When encountering "*", take the two operands at the top of the stack for addition, 6 * 48 = 288, push the stack

    Stacks: 288

Convert an inorder expression to a postorder expression

Since subsequent expressions are easier for computers to solve, we must first convert them to post-order when computing arithmetic expressions. Methods as below

  1. build symbol stack
  2. Sequential scanning in-order expression 
    a) is a number, directly output 
    b) is operator 
    i : "(" directly onto the stack 
    ii : ")" The elements in the symbol stack are popped out of the stack and output in turn, until "(", "( "Only pop the stack, do not output  iii: other symbols, pop the elements in the symbol stack and output in turn, until a symbol with a lower
    priority than the current symbol is encountered or "(". Push the current symbol onto the stack.
  3. After scanning, output the remaining symbols in the stack in turn

Example: 3+ (2-5) * 6/3

  • encounter 3 is a digital output

Expression: 3 
Symbol Stack:

  • When encountering a "+" sign, using rule iii, there is no lower priority symbol in the stack, and it is directly pushed into the stack

Expression: 3 
Symbol Stack: +

  • When encountering "(", use rule i to push directly onto the stack

Expression: 3 
Symbol Stack: + (

  • Encountered "2" output

Expression: 3 2 
Symbol Stack: + (

  • Encounter "-", use rule iii, encounter "(", there is no pop symbol, push directly to the stack

Expression: 3 2 
Symbol Stack: + ( -

  • Encountered "5" output

Expression: 3 2 5 
Symbol Stack: + ( -

  • When ")" is encountered, use rule ii to pop "-" out of the stack, and "(" out of the stack

expression: 3 2 5 
-symbol stack: +

  • When encountering "*", use rule ii, "*" has a lower priority than "+", so when encountering a symbol with a lower priority, do not pop the "*" onto the stack 
    Expression: 3 2 5 
    -symbol stack : + *
  • Encountered "6" output

表达式 : 3 2 5 - 6 
符号栈 : + *

  • 遇到”/” 利用法则ii , “/” 与”*”的优先相同, 就是说”*”不是优先级更低的符号, 所以出栈输出, 继续 “+”比”/”的优先级低, 不用出栈, 将”/”入栈

表达式 : 3 2 5 - 6 * 
符号栈 : + /

  • 遇到”3” 输出

表达式 : 3 2 5 - 6 * 3 
符号栈 : + /


  • 扫描完成 将符号栈内的符号依次输出 

表达式 : 3 2 5 - 6 * 3 / +

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012507347/article/details/52245233

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325821579&siteId=291194637