Illustration of infix expression and postfix expression

Illustration of infix expression and postfix expression

The computer realizes the four arithmetic operations mainly in two steps:

  1. Convert the given infix form expression to postfix expression form
  2. Perform calculations using postfix expressions
The following is the graphic process of the above two steps (this is actually the graphic process in the book "Dahua Data Structure")

Infix expression to postfix expression

The infix expression "9+(3-1)*3+10/2" is transformed into the postfix expression "9 3 1-3*+ 10 2/+"

Rule: Traverse each number and symbol in the infix expression from left to right. If it is a number, it will be output and it will become a part of the postfix expression; if it is a symbol, it will be judged as a priority with the top symbol of the stack, which is the right parenthesis or priority If the level is lower than the top-finding symbol (multiplication and division priority addition and subtraction), the top elements of the stack will be found and output in turn, and the current symbol will be pushed onto the stack until the final suffix expression is output.

Let's take a look at this process in detail.

1. Initialize an empty stack to use symbols in and out of the stack.

2. The first character is the number 9, output 9, followed by the symbol "+", into the stack.

3. The third character is "(", which is still a symbol, because it is only a left parenthesis and has not been matched yet, so it is pushed into the stack.

4. The fourth character is the number 3, output, the total expression is 9 3, followed by "-" into the stack.

5. Next is the number 1, output, the total expression is 9 3 1, followed by the symbol ")", at this time, we need to match the previous "(", so the top of the stack is popped out and output until " (" until it is popped out of the stack. At this time, there is only "-" above the left bracket, so "-" is output, and the total output expression is 9 3 1-

6. Next is the number 3, output, the total expression is 9 3 1-3. The symbol "*" is followed immediately, because the symbol at the top of the stack at this time is the "+" sign, and the priority is lower than the "*", so it is not output and pushed into the stack.

7. After the symbol "+", the current stack top element has a higher priority than this "+", so the elements in the stack are popped out and output (there is no lower priority than the "+" sign, so all are popped out ), the total output expression is 9 3 1-3 * +. Then the current symbol "+" is pushed onto the stack. In other words, the "+" at the bottom of the stack in the first 6 pictures refers to the "+" after the 9 at the beginning of the infix expression, and the "+" at the bottom of the stack (also the top of the stack) in the following figure refers to " The last "+" in 9+(3-1)*3+".

8. Immediately after the number 10, output, the total expression becomes 9 3 1-3 * + 10.

9. The last number 2, output, the total expression is 9 3 1-3*+ 10 2

10. Since it has reached the end, all symbols in the stack are popped out of the stack and output. The final output of the suffix expression result is 9 3 1-3*+ 10 2/+

  • From the derivation just now, you will find that in order for a computer to have the ability to process our usual standard (infix) expressions, the most important two steps are:
  1. Convert infix expressions into postfix expressions (the symbols used by the stack to enter and exit operations).
  2. Operate the postfix expression to get the result (the number used by the stack to enter and exit the operation).

The whole process makes full use of the last-in-first-out feature of the search. To understand it well is to understand the data structure of the stack.

Postfix expression evaluation

Suffix expression: 9 3 1-3*+ 10 2/+

  • Rule: traverse each number and symbol in the expression from left to right, push the stack when it encounters a number, and pop the two numbers at the top of the stack when it encounters a symbol, perform operations, and push the result of the operation until Finally get the result.

Here are the detailed steps:

1. Initialize an empty stack . This 桟 is used to enter and exit the number to be calculated.

2. The first three in the suffix expression are all numbers, so 9, 3, and 1 are pushed onto the stack.

3. Next is the minus sign "-", so pop 1 from the stack as the subtract, and pop 3 as the subtract, and calculate 3-1 to get 2, and then push 2 into the stack.

4. Then the number 3 is pushed onto the stack.

5. The multiplication "*" follows, which means that 3 and 2 are popped from the stack, and 2 is multiplied by 3 to get 6, and 6 is pushed into the stack.

6. The following is the addition "+", so find 6 and 9 to find, 9 and 6 are added to get 15, and 15 is put on the stack.

7. Then the two numbers 10 and 2 are pushed onto the stack.

8. Next is the symbol. Therefore, the top 2 and 10 are popped from the stack, 10 is divided by 2 to get 5, and 5 is pushed onto the stack.

9. The last one is the symbol "+", so 15 and 5 are found and added to get 20, and 20 is pushed onto the stack.

10. The result is 20 pops from the stack and the stack becomes empty.





Guess you like

Origin blog.csdn.net/qq_44378854/article/details/109170047