-------------------------------- suffix turn infix ----------------------------------------------
1, set up a stack, from left to right scan the suffix expression, encountered the operation arithmetic is pressed into the stack;
2, encountered the operator on the stack top two elements out of the stack, perform operations, the resulting results as a new operator and then press into the stack;
3, go to the end of the expression in turn;
Example: Convert the inverse polish (that is, suffix expression) ab+c* to infix expression:
1) A into the stack (0 position)
2) b into the stack (1 position)
3) Encounter operator "+", will A and b out of the stack, perform a+b operation, get the result d=a+b, and then d into the stack (0 position)
4) C into the stack (1 position)
5) Encounter Operator "*", D and C out of the stack, perform d*c operation, get result E, and then e into the stack (0 position)
Get Results (a+b) *c;
----------------------- convert infix expressions to prefix expressions----------------------------------
(1) Initialize two stacks: operator stack S1 and stack S2 for storing intermediate results;
(2) Scanning infix expression from right to left;
(3) When the operand is encountered, it is pressed into the S2;
(4) When an operator is encountered, compare its precedence with the top operator of the S1 stack:
(4-1) If the S1 is empty, or the top operator of the stack is the closing parenthesis ")", the operator is placed directly into the stack;
(4-2) Otherwise, if the priority is higher or equal than the top operator of the stack, the operator is also pressed into the S1;
(4-3) Otherwise, the S1 stack top operator pops up and presses into the S2, and again goes to (4-1) compared with the new stack top operator in S1;
(5) When the parentheses are encountered:
(5-1) If the closing parenthesis ")", then press directly into the S1;
(5-2) If the opening parenthesis "(", then pops up the operator at the top of the S1 stack, and presses into the S2 until the closing parenthesis is encountered, this pair of parentheses is discarded;
(6) Repeat steps (2) to (5) until the leftmost side of the expression;
(7) The remaining operators in the S1 are popped and pressed into the S2;
(8) The elements in the S2 pop-up and output, the result is the infix expression corresponding to the prefix expression.
For example, the process of converting infix expression "1+ ((2+3) x4)-5" to a prefix expression is as follows:
The element that is scanned |
S2 (Stack bottom, top of stack) |
S1 (Stack bottom, top of stack) |
Description |
5 |
5 |
Empty |
Numbers, directly into the stack |
- |
5 |
- |
S1 is empty, operator directly into stack |
) |
5 |
- ) |
Right parenthesis directly into the stack |
4 |
5 4 |
- ) |
Digital direct into the stack |
X |
5 4 |
-) x |
S1 stack top is the right parenthesis, directly into the stack |
) |
5 4 |
-) x) |
Right parenthesis directly into the stack |
3 |
5 4 3 |
-) x) |
Digital |
+ |
5 4 3 |
-) x) + |
S1 stack top is the right parenthesis, directly into the stack |
2 |
5 4 3 2 |
-) x) + |
Digital |
( |
5 4 3 2 + |
-) x |
Opening parenthesis, popup operator until a closing parenthesis is encountered |
( |
5 4 3 2 +x |
- |
Ditto |
+ |
5 4 3 2 +x |
- + |
Priority vs.-Same, in-stack |
1 |
5 4 3 2 +x1 |
- + |
Digital |
Reach the left End |
5 4 3 2 +x1 +- |
Empty |
The remaining operators in the S1 |
So the result is "-+ 1x+ 2 3 4 5".
----------------------------- infix expression to postfix expression --------------------------------------
Similar to converting to prefix expressions, follow these steps:
(1) Initialize two stacks: operator stack S1 and stack S2 for storing intermediate results;
(2) Scanning infix expression from left to right;
(3) When the operand is encountered, it is pressed into the S2;
(4) When an operator is encountered, compare its precedence with the top operator of the S1 stack:
(4-1) If the S1 is empty, or the top operator of the stack is an opening parenthesis "(", the operator is put directly into the stack;
(4-2) Otherwise, if the precedence is higher than the top of the stack operator, the operator is also pressed into the S1 (note that the conversion to a prefix expression is higher or the same priority, but this does not include the same situation);
(4-3) Otherwise, the S1 stack top operator pops up and presses into the S2, and again goes to (4-1) compared with the new stack top operator in S1;
(5) When the parentheses are encountered:
(5-1) If the opening parenthesis "(", then press directly into the S1;
(5-2) If the closing parenthesis ")", then pops the operator at the top of the S1 stack and presses it into the S2 until the opening parenthesis is encountered, discarding the parentheses at this time;
(6) Repeat steps (2) to (5) until the rightmost side of the expression;
(7) The remaining operators in the S1 are popped and pressed into the S2;
(8) Pop-up the elements in S2 and output, the reverse of the result is the infix expression corresponding to the suffix expression (converted to prefix expression without reverse order).
For example, the process of converting infix expression "1+ ((2+3) x4)-5" to a suffix expression is as follows:
The element that is scanned |
S2 (Stack bottom, top of stack) |
S1 (Stack bottom, top of stack) |
Description |
1 |
1 |
Empty |
Numbers, directly into the stack |
+ |
1 |
+ |
S1 is empty, operator directly into stack |
( |
1 |
+ ( |
Left parenthesis, directly into the stack |
( |
1 |
+ ( ( |
Ditto |
2 |
1 2 |
+ ( ( |
Digital |
+ |
1 2 |
+ ( ( + |
S1 Stack top is left parenthesis, operator directly into stack |
3 |
1 2 3 |
+ ( ( + |
Digital |
) |
1 2 3 + |
+ ( |
Closing parenthesis, popup operator until an opening parenthesis is encountered |
X |
1 2 3 + |
+ (x |
S1 Stack top is left parenthesis, operator directly into stack |
4 |
1 2 3 + 4 |
+ (x |
Digital |
) |
1 2 3 + 4x |
+ |
Closing parenthesis, popup operator until an opening parenthesis is encountered |
- |
1 2 3 + 4x+ |
- |
-Same as + priority, so eject +, press in- |
5 |
1 2 3 + 4x+ 5 |
- |
Digital |
Reach the right End |
1 2 3 + 4x+ 5- |
Empty |
The remaining operators in the S1 |
So the result is "1 2 3 + 4x+ 5-" (Note the need for reverse output).
[Goto] infix expression, prefix expression, suffix expression of mutual conversion