Calculate postfix infix expression turn and postfix expression

1. turn postfix infix expression

Here, for manual changeover pair of chestnuts:
PS: The following are all red brackets add effects after!

Infix expression: (5 * 3 + 1 + 20) / 14

  1. Operator Precedence by all operators and operands its parentheses (do not add the original parentheses)
    ( ( ( 5 + 20 ) + ( 1 * 3 ) ) / 14 )

  2. The operator to move to the corresponding rear bracket (Note here that the change in parentheses)
    ( ( ( 520 ) + ( 13 ) *) + 14 ) /

  3. Remove the brackets

Postfix expression: + 13 520 +14 * /



2. postfix expression

Postfix expression is also called reverse Polish notation, which can be used in the evaluation process stack to secondary storage. Postfix expression is assumed to be evaluated as: 6523 * 8 + + + * 3, it is evaluated as follows:

  1. Traversal expression, digital encountered first placed on the stack, when the stack is as follows:

Here Insert Picture Description

  1. Then read "+", the pop-up 2 and 3, 3 + 2 performed, the calculation result is equal to 5, and 5 is pressed into the stack.
    Here Insert Picture Description

  2. Read 8, which was directly placed on the stack.
    Here Insert Picture Description

  3. Read, "*", and the pop-8 5, 8 * 5 performed, and the result is pushed onto the stack 40. Then a similar process, to read "+", the pop-up 40 and 5, the result 40 + 45 5 ... and so on into the stack. Finally, find the value of 288.

Guess you like

Origin blog.csdn.net/weixin_40688217/article/details/93503496