The fourth arithmetic operation suffix expression is also called inverse Polish expression

Suffix representation of arithmetic expressions

rule

The infix expression a + b*c + (d * e + f) * g is converted into a postfix expression as abc * + de * f + g * +.

The conversion process needs to use the stack, the specific process is as follows:

1) If an operand is encountered, we will output it directly.

2) If an operator is encountered, we put it on the stack, and when we encounter an opening parenthesis, we also put it on the stack.

3) If a right parenthesis is encountered, the stack element is popped, and the popped operator is output until the left parenthesis is encountered. Note that the opening parenthesis only pops up and does not output.

4) If you encounter any other operators, such as ("+", "*", "("), etc., pop elements from the stack until you find a lower priority element (or the stack is empty) . After popping these elements, the operators encountered are pushed onto the stack. One thing to note is that we only pop up when we encounter ") "" (", we will not pop out in other cases" ( ".

5) If we read the end of the input, pop all the elements in the stack one by one.

Instance

There are many rules, but it is easier to explain the whole process with examples. Take the above conversion as an example, the input is a + b * c + (d * e + f)*g, the processing process is as follows:

1) First read a and output directly.

2) Read "+" and put it on the stack.

3) When b is read, output directly.

The stack and output at this time are as follows:

 

4) When "*" is read, because the priority of the element "+" on the top of the stack is lower than that of "*", the "*" is directly pushed onto the stack.

5) When c is read, output directly.

The stack and output are as follows:

 

6) When reading "+ ", because the priority of the top element" * "is higher than it," * "is popped and output. Similarly, the next element in the stack" + "Priority and the operator read" + "The same, so pop up and output. Then push the read "+" onto the stack.

The stack and output are as follows:

 

7) The next read is "(", which has the highest priority, so it is put directly on the stack.

8) When d is read, output it directly.

The stack and output are as follows:

 

9) Reading "* ", because the opening parenthesis "(" will only pop up when it encounters") ", so "*" is directly pushed onto the stack.

10) When e is read, output directly.

The stack and output are as follows:

 

11) Read "+ ", pop" * "and output, and then push "+" onto the stack.

12) When f is read, output directly.

Stack and output at this time:

 

 

13) If you read ")" next, pop the elements in the stack directly and output them until you encounter "(". Here, only one operator "+" before the closing bracket is popped and output.

 

14) Read "*" and push it onto the stack. Read g and output directly.

 

15) At this time, the input data has been read to the end, and there are two operators "*" and "+" in the stack, which are directly popped and output.

 

Postfix expression evaluation

Postfix expressions are also called inverse Polish expressions, and their evaluation process can use the stack to assist storage. Assuming that the suffix expression to be evaluated is: 6 5 2 3 + 8 * + 3 + *, the evaluation process is as follows:

1) Traverse the expression, the numbers encountered are first put into the stack, and the stack is as follows:

2) Then read "+", pop 3 and 2, execute 3+2, the calculation result is equal to 5, and push 5 onto the stack.

3) Read 8 and put it directly on the stack.

4) Read "*", pop 8 and 5, execute 8*5, and push the result 40 onto the stack. Then the process is similar, read "+", pop 40 and 5, push the result 45 of 40+5 onto the stack... and so on. The final value is 288.

 

Guess you like

Origin blog.csdn.net/zy103118/article/details/110131610