Suffix (reverse Polish) expression
Infix expression : it is the standard four arithmetic expressions we usually use, the operator is in the middle of the operand, for example: 9+(3-1)*3+10/2
Postfix expression : also known as reverse Polish expression, it is an expression in which the operator is written after the operand. For example, the postfix expression of the above formula is: 9 3 1-3 * + 10 2 / +
Function : For computers, infix expressions are relatively complicated structures, while inverse Polish expressions are relatively simple and easy to understand from computers. Because the memory structure commonly used by computers is a stack structure, it executes first-in-last-out order
Infix expression to postfix expression
Example : Infix expression 9+(3-1)*3+10/2 is converted to postfix expression 9 3 1-3 * + 10 2 / +
Human eye observation method : because a + b is converted to a suffix expression is ab +, first find the first operation in the expression and convert it to a suffix expression. As shown in the figure below, first calculate 3-1, and convert the suffix to 31- , And then consider 31- as a whole A, then the next operation A * 3, the suffix is A 3 *, that is, 3 1-3 *, and so on, the final converted suffix expression is 9 3 1-3 * + 10 2 / +
Program algorithm idea : traverse each number and symbol in the infix expression from left to right. If it is a number, it will be output and become a part of the suffix expression; if it is a symbol, the priority with the top symbol of the stack is judged, which is the right parenthesis Or the priority is lower than the top symbol of the stack (multiplication and division priority addition and subtraction), the top elements of the stack are sequentially popped out and output, and the current symbol is pushed into the stack until the final output suffix expression is summarized as follows:
The detailed process is as follows : 9+(3-1)*3+10/2 is converted to the postfix expression 9 3 1-3 * + 10 2 / +
-
Initialize an empty stack to use for symbols in and out of the stack, as shown on the left side of the figure below
-
The first is the number 9, output directly, followed by the sign +, into the stack, as shown on the right side of the figure below
-
The third character is (, the left parenthesis has not been paired yet, and is pushed into the stack, as shown on the left side of the figure below
-
The fourth character is the number 3, output directly, the total expression is 9 3, and then -, into the stack, as shown on the right side of the figure below
-
Next is the number 1, output, the total expression is 9 3 1, and the following is). At this time, it is necessary to match the left parenthesis before, so the top of the stack is output in turn until (pop. The total expression is now 9 3 1 -, + is left in the stack, as shown on the left
-
Immediately followed by *, because the top of the stack is a + sign, the priority is lower than the multiplication sign, so it is not output, the multiplication sign is pushed into the stack, and then the number 3 is output. The total expression is 9 3 1-3, as shown on the right side
-
Then there is +. At this time, the top of the stack is the multiplication sign, and the priority of the top of the stack is high, so the elements in the stack are popped out of the stack and output (there is no lower priority than +, so all pop out of the stack), the total output expression is 9 3 1- 3 * +. Then push the current symbol + into the stack, as shown on the left side of the figure below
-
Next is the number 10, output directly, the total expression becomes 9 3 1-3 * + 10. Then there is /, the top of the stack + priority is lower than the division sign, and / is pushed into the stack, as shown on the right side of the figure below
-
Next is the last number 2, output, the expression is 9 3 1-3 * +10 2, as shown on the left side of the figure below
-
The traversal of the infix expression ends, and all the symbols in the stack are popped out of the stack and output. The final output suffix expression is: 9 3 1-3 * +10 2 / +
The C code is as follows :
//中缀表达式转后缀表达式
//中缀表达式存储在字符串中,为便于表示,代码使用9+(3-1)*3+8/2为例子
#include "stack.h"
#include <stdio.h>
int main()
{
Stack s;
InitStack(&s);
const char mid[] = "9+(3-1)*3+8/2"; //中缀表达式
char back[14]; //后缀表达式
int i = 0;
int j = 0;
char useless;
for (i = 0; i < 13; i++)
{
if (mid[i] <= '9'&&mid[i] >= '0') //是数字,直接输出
{
back[j] = mid[i];
j++;
}
else if (mid[i] == '(') //是左括号,直接入栈
{
Push(&s, mid[i]);
}
else if (mid[i] == ')') //是右括号,栈中出栈直到第一个左括号出栈
{
while (GetTop(&s) != '(')
{
Pop(&s, &back[j]);
j++;
}
Pop(&s, &useless);
}
else if (mid[i] == '*' || mid[i] == '/') //是*/,出栈,直到栈空或者栈中遇到左括号或+-,当前符号入栈
{
while (!IsEmpty(&s) && (GetTop(&s) != '+' && GetTop(&s) != '-'))
{
Pop(&s, &back[j]);
j++;
}
Push(&s, mid[i]);
}
else if (mid[i] == '+' || mid[i] == '-') //是+-,出栈,直到栈空或者左括号当前符号入栈
{
while (!IsEmpty(&s)&&GetTop(&s) != '(')
{
Pop(&s, &back[j]);
j++;
}
Push(&s, mid[i]);
}
}
//中缀表达式遍历结束,要将栈中剩余符号依次弹出
while (!IsEmpty(&s))
{
Pop(&s, &back[j]);
j++;
}
back[j] = '\0';
printf("中缀表达式: %s\n\n", mid);
printf("后缀表达式: %s\n\n", back);
return 0;
}
Code running results:
How the computer uses the postfix expression to evaluate
For example : the suffix expression of the infix expression 9+(3-1)*3+10/2 is 9 3 1-3 * + 10 2 / + , how does the computer use the suffix expression to perform operations?
Rule : Traverse each digit and symbol of the suffix expression from left to right , and push the stack when it encounters a number, and push the two digits at the top of the stack when it encounters a symbol, perform operations, and push the result of the operation into the stack until Finally get the result
The detailed process is as follows : 9 3 1-3 * + 10 2 / +
- Initialize an empty stack, which is used to enter and exit the numbers to be calculated
- The first three in the suffix expression are all numbers, so 9 3 1 are pushed into the stack in turn, as shown in the figure below
- Next is the-operator, so pop 1 from the top of the stack as the subtract, and pop 3 as the subtract, and operate 3-1 to get 2, and then push 2 into the stack, as shown on the left
- Then the number 3 is pushed into the stack, as shown on the right side of the picture below
- Next is *, which means that 3 and 2 in the stack are popped, multiplied to get 6, and then 6 is pushed into the stack, as shown on the left side of the figure below
- Next is +, pop 6 and 9 from the stack, add 15 to get 15, and then put 15 into the stack, as shown on the right side of the figure below
- Then the two numbers 10 and 2 are pushed into the stack, as shown on the left side of the figure below
- Next is /, so 2 and 10 on the top of the stack are popped, 10 and 2 are divided to get 5, and 5 is pushed into the stack, as shown on the right side of the figure below
- The last is the operator +, so 5 and 15 are popped and added to get 20, and then 20 is pushed into the stack, as shown on the left side of the figure below
- The traversal of the suffix expression ends, and the final result in the stack is 20 pops, the stack becomes empty, and the calculation ends, as shown on the right side of the figure below
It can be seen from the above that the suffix expression can solve the calculation problem smoothly, so the suffix expression is very important
In summary, in order for a computer to have the ability to process our usual standard (infix) expressions, the most important steps are two major steps :
- Convert an infix expression to a postfix expression (the symbol used by the stack to enter and exit operations)
- Operate the postfix expression to get the result (the stack is used to enter and exit the number for the operation)
The above process makes full use of the last-in-first-out feature of the stack, which is an important application of the data structure of the stack