Time：2021-4-19

expression

Prefix expression (Polish expression)

1. Prefix expressions are also called Polish expressions, and the operators of prefix expressions precede the operands
2. Examples: (3 + 4) ×5-6 The corresponding prefix expression is- × + 3 4 5 6

Prefix expression evaluation

Computer evaluation of prefix expression

Scan the expression from right to left. When encountering a number, push the number onto the stack. When encountering an operator, pop up two numbers at the top of the stack, use the operator to calculate them (top element and secondary top element), and put the result into the stack. Repeat the above process until the left end of the expression, and the final value is the result of the expression

For example:`(3+4)×5-6`The corresponding prefix expression is`- × + 3 4 5 6`The evaluation steps for prefix expressions are as follows:

• Scan from right to left, and`6、5、4、3`Push on the stack
• The + operator was encountered, so the`3`and`4`(3 is the top element of the stack, 4 is the secondary top element)`3+4`It’s a good value`7`Then put 7 on the stack
• Next up`×`Operator, so pop up`7`and`5`, calculated`7×5=35`, will`35`Push
• Finally, the – Operator calculates the`35-6`The value of, i.e`29`The final result is obtained

Infix expression

Infix expression

Infix expression is a common operation expression, such as`(3+4)×5-6`

Infix expression evaluation is most familiar to us, but it is not easy to operate for the computer (we can see this problem in the case mentioned above). Therefore, when calculating the result, infix expression is often converted to other expressions (generally converted to suffix expression.)

Infix expression is easy for us. It’s ridiculous whether he understands computer or not

The computer doesn’t know how to calculate this priority

Suffix expression (inverse Polish expression)

Postfix Expression

Suffix expression, also known as inverse Polish expression, is similar to prefix expression except that the operator is after the operands

The suffix expression corresponding to (3 + 4) × 5-6 is 34 + 5 × 6 –

Another example is:

Normal expression Reverse Polish notation
a+b a b +
a+(b-c) a b c – +
a+(b-c)*d a b c – d * +
a+d*(b-c) a d b c – * +
a=1+3 a 1 3 + =

Computer evaluation of suffix expression

Scan the expression from left to right. When encountering a number, push the number onto the stack. When encountering an operator, pop up two numbers at the top of the stack, use the operator to calculate them (secondary top element and top of the stack element), and put the result into the stack. Repeat the above process until the right end of the expression, and the final value is the result of the expression

For example: (3 + 4) ×5-6 The corresponding suffix expression is 3, 4 + 5 × 6 – , The evaluation steps for suffix expressions are as follows:

1. Scan from left to right and push 3 and 4 onto the stack;
2. Encounter the + operator, so pop up 4 and 3 (4 is the top element of the stack, 3 is the secondary top element), calculate the value of 3 + 4, get 7, and then put 7 on the stack;
3. Put 5 on the stack;
4. Next is the X operator, so pop up 5 and 7, calculate 7 × 5 = 35, and put 35 on the stack;
5. Put 6 on the stack;
6. Finally, the – Operator calculates the value of 35-6, which is 29, and the final result is obtained