Prefix infix suffix expression rules

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-6The corresponding prefix expression is- × + 3 4 5 6The evaluation steps for prefix expressions are as follows:

  • Scan from right to left, and6、5、4、3Push on the stack
  • The + operator was encountered, so the3and4(3 is the top element of the stack, 4 is the secondary top element)3+4It’s a good value7Then put 7 on the stack
  • Next up×Operator, so pop up7and5, calculated7×5=35, will35Push
  • Finally, the – Operator calculates the35-6The value of, i.e29The 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

This work adoptsCC agreementReprint must indicate the author and the link of this article

Autumn leaves and summer wind

Recommended Today

Envoy announced alpha version of native support for windows

Author: sunjay Bhatia Since 2016, porting envoy to the windows platform has been an important part of the projectOne of the goalsToday, we are excited to announce the alpha version of envoy’s windows native support. The contributor community has been working hard to bring the rich features of envoy to windows, which is another step […]