Infix expression into postfix notation (reprint)

First, the postfix expression

Postfix expression is also called reverse Polish notation, which can be used in the evaluation process stack to secondary storage. Postfix expression is assumed to be evaluated as: 6523 * 8 + + + * 3, it is evaluated as follows:

1) a traversal expression, digital encountered first placed on the stack, when the stack is as follows:

2) Next, read "+", the pop-up 2 and 3, 3 + 2 performed, the calculation result is equal to 5, and 5 is pressed into the stack.

3) read 8, which was directly placed on the stack.

4) read, "*", and the pop-8 5, 8 * 5 performed, and the result is pushed onto the stack 40. Then a similar process, to read "+", the pop-up 40 and 5, the result 40 + 45 5 ... and so on into the stack. Finally, find the value of 288.

 

 


 

Second, turn postfix infix expression

2.1) Rules

Infix expression a + b * c + (d * e + f) * g, which was converted into the postfix expression abc * + de * f + g * +.

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

1) If you encounter an operand, we will direct its output.

2) If you encounter operator, then we will put it onto the stack, we encountered left parenthesis also be placed on the stack.

3) If you encounter a right parenthesis, then pop the stack elements, the pop-up operator output until it encounters a left parenthesis so far. Note that the left parenthesis is not only the pop-up output.

4) If you experience any other operators, such as ( "+", "*", "(") and the like, pop-up element from the stack hard against the lower priority element (or stack is empty) found. after the case of pop these elements, it will encounter operator pushed onto the stack. it is important to note that only in the face ")" we only pop-up "(" otherwise we will not pop-up "( . "

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

 

2.2) Examples

Many rules, or use examples relatively easy to clear the entire process. To convert the above example, the input to a + b * c + (d * e + f) * g, the process is as follows:

1) First read a, direct output.

2) read, "+", which is placed onto the stack.

3) read b, direct output.

In this case where the output stack is as follows:

 

4) read, "*", because "*" Low top element "+" priority than, so the "*" directly onto the stack.

5) reading c, direct output.

At this time, the stack and output as follows:

 

6) read "+", because the top of the stack "*" than it is high priority, the pop-up "*", and outputs the same token, the next element in the stack "+" and the read priority operator " + "the same, so it will pop and outputs. Then read "+" onto the stack.

At this time, the stack and output as follows:

 

For 7) a read is "(", it is the highest priority, it is placed directly into the stack.

8) read d, which is directly output.

At this time, the stack and output as follows:

 

9) read, "*", because the only encounter ")" when the left bracket "(" will pop up, so "*" directly onto the stack.

10) to read e, direct output.

At this time, the stack and output as follows:

 

11) read the "+" pop-up "*" and outputs, then the "+" onto the stack.

12) read f, is directly output.

At this time, the output stack and conditions:

 

 

13) Next, to read ")", then the element directly to the stack and pop outputs until it encounters to "(" here is only a front-right bracket operator "+" is ejected and output.

 

14) read, "*", onto the stack. Read g, direct output.

 

15) In this case the input data has come to the end, there are two stacks operator "*" and "+", and outputs the direct pop.

At this point the entire conversion process is complete. Program implementation code subsequent re-add.

 

 Another approach 2.3) convert

1) according to the operator priority infix expression parentheses, becomes ((a + (b * c)) + (((d * e) + f) * g))

2) The operator moved to the back of the bracket, into ((a (bc) *) + (((de) * f) + g) *) +

3) removal of the brackets, to give abc * + de * f + g * +

 

Article Source:

https://www.cnblogs.com/hantalk/p/8734511.html

 

Guess you like

Origin www.cnblogs.com/xwh-blogs/p/12456136.html