Postfix expression computing and Reverse Polish Notation

A calculation according postfix infix

https://blog.csdn.net/lcl497049972/article/details/83061274

Infix expression "9 + (3-1) * 3 + 10/2" into postfix "931-3 * 2 + 10 / +"

Rule: traversing from left to right in each of the numbers and symbols infix expressions, if the digital outputs, i.e., become part of the postfix expression; sign if it is determined that the priority of the symbol stack, is a right parenthesis or preferentially Get the top level is less than symbol (priority subtraction, multiplication and division) to find out the top element and sequentially output and the current symbol into the stack, up to the final output until the postfix expression.

Let's take a look at this specific process.

1. Initialize a blank stack out of the stack to the symbols used.

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

3. The third character is "(", is still the symbol, because it just left parenthesis, has not been matched, so the push.

4. The fourth character is a number 3, the output, the total expression of 93, followed by "-" push.

5. Next is the number 1, the output, the total expression of 931, followed by the symbol ")" In this case, we need to match the previous "(" the stack so that the stack in sequence, and outputs until " ( "stack up to the upper left bracket is only at this time." - ", the output" - ", expressed as total output 931--

6. Next, the symbol "*", because the top of the stack is the symbol "+" sign, a lower priority than "*", and therefore is not output. The "*" push; then the number 3, the output, the total expression of 931--3.

7. After a "+" sign, then the current top of the stack is higher than the "+" in the priority, so the stack and the stack output element (not priority than "+" lower number, so all the stack ), expressed as total output 931--. + 3 * and the current symbol "+" into the stack. In other words, the stack before the bottom of Figure 6 "+" means 9 after the opening of the infix expression "+", while the bottom of the stack in the figure below (also top of the stack) the "+" means " 9+ (3-1) * + 3 "in the last" +. "

8. Then 10 digital output, the total expression becomes + 10 * 931-3.

9. Finally a number 2, the output, the total expression is 931-3 + 102 *

10. 因已经到最后,所以将栈中符号全部出栈并输出。最终输出的后缀表达式结果为 9 3 1-3*+ 10 2/+

计算机运算的步骤是:

  1. 将中缀表达式转化为后缀表达式(栈用来进出运算的符号)。
  2. 将后缀表达式进行运算得出结果(栈用来进出运算的数字)。

二、后缀表达式的计算过程

https://www.cnblogs.com/jiu0821/p/6692878.html

后缀表达式:9 3 1-3*+ 10 2/+

  • 规则:从左到右遍历表达式的每个数字和符号,遇到是数字就进栈,遇到是符号,就将处于栈顶两个数字出栈,进行运算,运算结果进栈,一直到最终获得结果。

详细步骤:

1. 初始化一个空栈。此桟用来对要运算的数字进出使用。

2. 后缀表达式中前三个都是数字,所以9、3、1进栈。

3. 接下来是减号“-”,所以将栈中的1出栈作为减数,3出栈作为被减数,并运算3-1得到2,再将2进栈。

4. 接着是数字3进栈。

5. 后面是乘法“*”,也就意味着栈中3和2出栈,2与3相乘,得到6,并将6进栈。

6. 下面是加法“+”,所以找中6和9出找,9与6相加,得到15,将15进栈。

7. 接着是10与2两数字进栈。

8. 接下来是符号因此,栈顶的2与10出栈,10与2相除,得到5,将5进栈。

9. 最后一个是符号“+”,所以15与5出找并相加,得到20,将20进栈。

10. 结果是20出栈,栈变为空。

 

Guess you like

Origin blog.csdn.net/orangefly0214/article/details/93756214