First, infix turn suffix
1, infix expression to postfix expression process similar to the compilation process
(1), the parentheses in the arithmetic expression must match
(2), conversion based on operator precedence
(3), no parentheses in the converted Expression
(4), after the conversion can be sequentially calculated the final result
2. Conversion process
(1), when the element e is a number: output
(2), when the element e is an operator:
A, the precedence comparison with the top of the stack operator
B, less than equals: Output the top element of the stack, go to a execution
C, greater than: the current element e into the stack
(3), the current element is E for the left parenthesis: into the stack
(4), the current element e is a closing parenthesis:
A, eject the top element of the stack and output until the top of the stack is left bracket
B, pop the left parenthesis of the top of the stack from the stack.
Two, key points: bracket matching algorithm
(1), parentheses appear in pairs
(2), opening parenthesis must precede the closing parenthesis
Third, summary
(1), suffix expression is the basis for a program to calculate complex expressions
(2), infix suffix is based on the stack data structure
(3), the conversion process can find errors in the expression
13th lesson, calculator core analytic algorithm (middle)