Converting infix expressions to suffix expressions

Source: Internet
Author: User

We use the usual standard arithmetic expression, namely "+ (3-1) *3+10/2" is called infix expression. Because all the arithmetic symbols are in the middle of the two numbers, now our problem is the conversion of infix to suffix.

Infix expression "+ + (3-1) *3+10/2" converted to postfix expression "9 3 1-3*+ 10 2/+"

    • Rule: The number and symbol of the infix expression is traversed from left to right, and if the number is output, it becomes part of the suffix expression, and if the symbol is the priority of the top symbol, the right parenthesis or priority is lower than the top-up symbol (multiplication precedence plus minus), then the top element of the stack is searched and output and the current symbol Until the final output suffix expression.

Let's take a look at this process in detail.

1. Initialize an empty stack to use for symbols in and out of the stack.

2. The first character is the number 9, the output 9, followed by the symbol "+", into the stack.

3. The third character is "(", is still a symbol, because it is only the left parenthesis, not paired, so the stack.

4. The fourth character is the number 3, the output, the total expression is 9 3, followed by the "-" into the stack.

5. Next is the number 1, the output, the total expression is 9 3 1, followed by the symbol ")", at this time, we need to match the previous "(", so the stack top sequentially out of the stack, and output, until "(" out of the stack. At this point there is only "-" above the opening parenthesis, so the output "-", the total output expression is 9 3 1-

6. Then the number 3, the output, the total expression is 9 3 1-3. followed by the symbol "*", because at this time the top of the stack symbol is "+" number, the priority is lower than "*", so do not output, into the stack.

7. Then the symbol "+", at this time the current stack top element than this "+" priority, so the stack of elements out of the stack and output (there is no lower priority than the "+" number, so all out of the stack), the total output expression is 9 3 1-3 * +. Then the current symbol "+" into the stack. That is, the bottom of the top 6 of the stack "+" refers to the infix expression in the beginning of the 9 after the "+", and the bottom of the stack (also the top of the stack) "+" refers to "+ + (3-1) *3+" in the Last "+".

8. Immediately following the number 10, the output, the total expression becomes 9 3 1-3 * + 10.

9. The last digit 2, output, the total expression is 9 3 1-3*+ 10 2

10. Because it has reached the end, all the symbols in the stack are stacked and output. The final output suffix expression results in 9 3 1-3*+ 10 2/+

    • From the derivation, you will find that the most important thing to do is to have the ability of the computer to handle our usual standard (infix) expressions, two steps:
    1. The infix expression is converted to a suffix expression (the symbol used by the stack to enter and exit the operation).
    2. The suffix expression is used to calculate the result (the number that the stack uses to enter and exit the operation).

The whole process, take full advantage of the last-first-out feature to deal with, understand good it actually also understand the stack of this data structure.

Converting infix expressions to suffix expressions

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: 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.