Last talk on how to calculate the suffix expression, in fact, the real difficulty is how to make a normal string expression (infix expression) into a suffix expression. If 6 * (5 + (2 + 3) * 8 + 3) becomes 6 5 2 3 + 8 * + 3 + *
Inverse Polish expression, its syntax stipulates that expressions must be given in inverse Polish expressions. The inverse Polish expression is also called the suffix expression. This knowledge point is presented in the two courses of data structure and compiling principle, here are some examples:
The normal infix expression |
Inverse Polish expression |
A+b |
a,b,+ |
A + (B-C) |
A,b,c,-, + |
A + (B-C) *d |
A,b,c,-, d,*,+ |
a+d* (B-C) |
A,d,b,c,-, *,+ |
First, the precedence of the operators in the expression, from large to small, is: (), * and/, +, and-. Consider only arithmetic for the time being.
Sequential reading of string expressions, rules:
- Read the operation number, the direct output;
- Read is the operator (+-*/), which is a priority comparison to the top of the stack (recorded as top): Read>top,read into the stack, continues reading the next, read≤top,top out the stack, and outputs to the list, Read continues to compare with the new top, top is empty, read directly into the stack, and if Top is "(", read directly into the stack, because "(" the highest priority;
- The processing of parentheses: reads the opening parenthesis "(", presses it directly into the stack, and unless it encounters a closing parenthesis ")", "(" is not ejected; Read the closing parenthesis ")", the "(" above the elements are all sequentially output, and pop "(" but not output;
Preparation: A stack stack staging operator, a list store output, still takes 6 * (5 + (2 + 3) * 8 + 3) as an example.
- Read "6", Direct output "List:6;stack:"
- Read "*", compared to top of the stack, top is empty, "*" Into the stack "list:6;stack:*"
- Read "(", "directly into the stack" list:6;stack:*, ("
- Read "5", direct output "list:6,5;stack:*," ("
- Read "+", compared to top of the stack, "+" < "(", Into the Stack "list:6,5;stack:*, (, +")
- Read "(", "directly into the stack" list:6,5;stack:*, (, +, ("
- Read "2", direct output "list:6,5,2,;stack:*, (, +, ("
- Read "+", with the top of the stack "(" Compare, "+" < "(", Into the Stack "list:6,5,2,;stack:*, (, +, (, +)
- Read "3", direct output "list:6,5,2,3,;stack:*, (, +, (, +")
- Read ")", Output "(" element Above "list:6,5,2,3,+;stack:*, (, +,"
- Read "*", compared with "+" at the top of the stack, "*" > "+", "*" Into the stack "list:6,5,2,3,+;stack:*," (, +,*, "
- Read "8", direct Output "list:6,5,2,3,+,8,;stack:*, (, +,*,"
- Read "+", compared to the top of the stack "*", "+" < "*", "*" Out of the stack and output "list:6,5,2,3,+,8,*;stack:*, (, +,";
- "+", compared with the "+" at the top of the stack, "+" = "+", "+" out of the stack and output "list:6,5,2,3,+,8,*,+;stack:*, (,";
- "+", with the top of the stack "(" comparison, "+" < "(", "+" into the Stack "list:6,5,2,3,+,8,*,+;stack:*, (, +";
- Read "3", direct output, "list:6,5,2,3,+,8,*,+,3;stack:*, (, +")
- Read ")", Output "(" Above the element, "list:6,5,2,3,+,8,*,+,3,+;stack:*,"
- All remaining in the output stack, "List:6,5,2,3,+,8,*,+,3,+,*;stack:,"
- The final suffix expression is: 6 5 2 3 + 8 * + 3 + *
At this point, the infix expression to the suffix expression of the task has been completed!
However, it is important to note that this article only does when the operation data for single-digit processing, when the multi-digit number of what to do?
In addition, when the "/" is encountered, it is necessary to make a non-0 judgment on the divisor;
It is also possible to consider operations that contain negative numbers , with decimals and power operations
That is the data how to deal with the problem, the overall idea should be similar!
Java parsing string expression--inverse Polish expression generation