Java parsing string expression--inverse Polish expression generation

Source: Internet
Author: User

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:

    1. Read the operation number, the direct output;
    2. 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;
    3. 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.

  1. Read "6", Direct output "List:6;stack:"
  2. Read "*", compared to top of the stack, top is empty, "*" Into the stack "list:6;stack:*"
  3. Read "(", "directly into the stack" list:6;stack:*, ("
  4. Read "5", direct output "list:6,5;stack:*," ("
  5. Read "+", compared to top of the stack, "+" < "(", Into the Stack "list:6,5;stack:*, (, +")
  6. Read "(", "directly into the stack" list:6,5;stack:*, (, +, ("
  7. Read "2", direct output "list:6,5,2,;stack:*, (, +, ("
  8. Read "+", with the top of the stack "(" Compare, "+" < "(", Into the Stack "list:6,5,2,;stack:*, (, +, (, +)
  9. Read "3", direct output "list:6,5,2,3,;stack:*, (, +, (, +")
  10. Read ")", Output "(" element Above "list:6,5,2,3,+;stack:*, (, +,"
  11. Read "*", compared with "+" at the top of the stack, "*" > "+", "*" Into the stack "list:6,5,2,3,+;stack:*," (, +,*, "
  12. Read "8", direct Output "list:6,5,2,3,+,8,;stack:*, (, +,*,"
  13. Read "+", compared to the top of the stack "*", "+" < "*", "*" Out of the stack and output "list:6,5,2,3,+,8,*;stack:*, (, +,";
  14. "+", compared with the "+" at the top of the stack, "+" = "+", "+" out of the stack and output "list:6,5,2,3,+,8,*,+;stack:*, (,";
  15. "+", with the top of the stack "(" comparison, "+" < "(", "+" into the Stack "list:6,5,2,3,+,8,*,+;stack:*, (, +";
  16. Read "3", direct output, "list:6,5,2,3,+,8,*,+,3;stack:*, (, +")
  17. Read ")", Output "(" Above the element, "list:6,5,2,3,+,8,*,+,3,+;stack:*,"
  18. All remaining in the output stack, "List:6,5,2,3,+,8,*,+,3,+,*;stack:,"
  19. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.