Converts an infix expression to a suffix expression

Source: Internet
Author: User

Original from: http://www.nowamagic.net/librarys/veda/detail/2307

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

Infix expression "9+ (3-1) *3+10/2" translates to the suffix expression "9 3 1-3*+ 10 2/+" rule: from left to right to traverse the infix expression of each number and symbol, if the number on the output, that is, as part of the suffix expression, if the symbol, the top of the symbol to determine the priority is a closing parenthesis or priority lower than the top sign (multiplication and division plus minus) then the top element of the stack is sorted out and exported, and the current symbol is put into the stack until the final output suffix expression.

Let's take a concrete look at the process below.

1. Initialize an empty stack, used for symbols in and out of the stack.

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

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

4. The fourth character is the number 3, the output, the total expression is 9 3, and then 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 top of the stack sequentially out of the stack, and output until "() stack." At this point there is only "-" above the left 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 is higher than this "+" priority, so the stack of elements out of the stack and output (no more than the "+" number of lower priority, so all out of the stack), the total output expression is 9 3 1-3 * +. And then the current symbol "+" into the stack. In other words, the top 6 of the bottom of the stack "+" is the infix expression in the beginning of the 9 "+", and the bottom of the stack (also the top of the stack) "+" refers to "9+ (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, the output, the total expression is 9 3 1-3*+ 10 2

10. Due to the end, so the stack of symbols all out of the stack and output. The final output suffix expression result is 9 3 1-3*+ 10 2/+ from the derivation you will find that in order for the computer to have the ability to handle our usual standard (infix) expressions, the most important thing is two steps: converting infix expressions into suffix expressions (symbols used by stacks to enter and leave operations). The suffix expression is used to calculate the result (the number the stack uses to enter and leave the operation).

The whole process, all take full advantage of looking for the LIFO characteristics to deal with, understand well it actually also understand the stack of this data structure.

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.