1. infix expression to suffix expression
Infix: A/(B-c) + D * E
Suffix: ABC-/De * +
Conversion steps:
(1) scan the infix expression from left to right. If it encounters # conversion (6 );
(2) output directly when an operand is encountered; (not on the stack)
(3) When ")" is encountered, the stack is output continuously until "(") "is encountered (" out of the stack but not output); otherwise
(4) If it is another operator, it is compared with the operator on the top of the stack. If the priority is smaller than the priority on the top of the stack, such as a * B + C, note that the two numbers a and B required by "*" have been output, and "*" is popped up until the off-stack priority is higher than the intra-stack priority, and "+" is pushed to the stack. if the priority is higher than the top priority of the stack, for example, A + B * C, it indicates that there is no output for the two numbers B and C required for Number *, so the number * is pushed to the stack.
(5) Forwarding (1 );
(6) remaining operators in the output stack ().
2. Key to conversion: determining the operator priority
The priority determines whether the operator is in or out of the stack.
The operators inside and outside the stack should have different priorities to reflect the computing requirements of the infix expression with the same priority operator from left to right. "(" Has the highest priority outside the stack, but it should be lower than the # Operator after the stack is added, so that other operators in the brackets can be added to the stack.
ISP-intra-stack priority
ICP -- off-stack priority
Operator # (*/+ -)
ICP 0 7 4 2 1
ISP 0 1 5 3 7