Postfix expression and four arithmetic

Stack-evaluation of four arithmetic expressions

9+(3-1)×3+10÷2=?
How does the computer figure out that it is equal to 20?
This article explains how computers use stacks to express the four arithmetic operations and evaluations in mathematics. There are two main concepts in the article, namely the suffix notation and how an infix expression is converted into a suffix expression.

1. Suffix notation (reverse Polish notation, RPN)

For any expression, such as "9+(3-1)×3+10÷2", what would it look like if you use the suffix notation: "9 3 1 – 3 * + 10 2 / +", such an expression It is called a postfix expression. The reason for the suffix is ​​that all symbols are after the number to be operated on. Suffix expression calculation process:
rule : traverse each number and symbol of the expression from left to right, push the stack when encountering a number, and pop the top two digits on the stack when encountering a symbol, and perform operations on the two. The result is pushed into the stack; the operation is repeated until the suffix expression is traversed, and the final result will be obtained.

Try to calculate the suffix expression: "9 3 1 – 3 * + 10 2 / +".
1. Initialize an empty stack. Traverse the postfix expression. The first three digits of "9 3 1" are all numbers, which are sequentially stacked...As
Insert picture description here
mentioned above, the suffix expression can smoothly solve the calculation problem. The computer has completed four arithmetic operations through the calculation of the suffix expression.

2. Infix expression to postfix expression

Infix expression is the four arithmetic, the symbol is located between two numbers: "9+(3-1)×3+10÷2", which is called infix expression.
The key question is how infix expressions are converted into postfix expressions in the computer!
Steps for converting the infix expression "9+(3-1)×3+10÷2" to the postfix expression "9 3 1 – 3 * + 10 2 / +":
Rule : Traverse the infix expression from left to right Each number and symbol of;
if it is a number, it is directly output (that is, it becomes a part of the suffix expression);
if it is a symbol, the priority of the symbol and the top symbol of the stack is judged, and the priority is lower than that of the top symbol (or right parenthesis) ), the top element of the stack is popped and output, and the current symbol is pushed onto the stack; (the right parenthesis matches the left parenthesis, and the symbols are popped out of the stack in turn )
until the infix expression is traversed, the stack is empty, and the suffix expression is finally output .
Insert picture description here
The whole conversion process makes full use of the last-in-first-out feature of the stack, and completes the conversion of the four arithmetic infix expressions entered in the program to postfix expressions.

3. References

"Dahua Data Structure" - by Cheng Jie

Guess you like

Origin blog.csdn.net/beauthy/article/details/105375935