Application stack - postfix infix expression turn

All data structures, algorithms and applications curricular templates please click: https://blog.csdn.net/weixin_44077863/article/details/101691360

In fact, the infix expression turn suffix (or prefix expression) is not a curricular focus

Teachers talk about how to write a handwritten code did not mention

But I want to emphasize this application

Learn this, discrete mathematics paradigm shift problem, you can solve the

Learn this, most of the application stack, you can handy

Learn this, compiler theory and computer systems you will learn better (eh .. I do not know the teacher said so, when bloggers write this on just a sophomore)

No bullshit, will speak about what it means all kinds of expressions

Infix expression: 23 + 34 * 45 / (5 + 6 + 7) (an expression that we usually use, the operator in the middle)

Prefix expression: + 23 / * 34 + 45 + 567 (also known as Polish)

Postfix expression: 233 445 * 56 + 7 + / + (called Reverse Polish Notation)

Teaching it, I did not say a prefix, suffix said only, then we are mainly talking about the suffix

And then, the code is very cumbersome, not on the code, they talk about the idea, and the idea figured out what came out of code

How to talk about before the suffix expression values ​​calculated

In fact, the prefix, the machine is not known, it is to use a prefix or suffix and use the stack count

Prefix: things constantly pushed to the stack when the stack is a number, the following is a operator, and when a number have to come

                The operator put the top of the stack and the current digital and digital to be taken out is calculated, and the result back into the stack

                This continuous cycle of operations until a final result only

The suffixes: continuously things onto the stack, when the operator encounters, put the top of the stack is taken out two numbers

                Do a calculation, and then sends the results back into the stack, so that the cycle of operations until a final result only

Then say is decorated in turn how the suffix (prefix is ​​similar, so we just say a)

Infix transfer postfix method:

First, two stacks, the stacks S1 temporarily stored encountered operators, storage stack S2 Reverse Polish

Then in the following steps can be realized in turn conjugated suffix

(1) If the fetched character is an operand, a complete analysis of the operand, the operand stack directly into S2

(2) If the fetched character is operator, and the operator stack top element S1, and if the operator precedence (not including the bracket operator) is larger than S1 stack stack operator precedence, then the S1 operator into the stack, otherwise, the operators S1 stack pop stack, the stack into the S2, S1 stack until the stack below the operator (not equal to) the priority of the operator, and finally sent to the operator S1 into the stack.

(3) If the fetched character is "(", directly into the top of the stack S1.

(4) If the fetched character is ")", the nearest distance S1 stack stack "(" between the operator, one by one out of the stack, the stack S2 is fed successively, then discard "(."

 

Published 49 original articles · won praise 0 · Views 1717

Guess you like

Origin blog.csdn.net/weixin_44077863/article/details/102217208