Converting infix expressions to suffix expressions using stacks

Source: Internet
Author: User

Transfer from http://blog.csdn.net/mvpsendoh/article/details/6440559

Objective: To convert infix expression (the standard form of expression) to a suffix.

Example: a+b*c+ (d*e+f) *g converted to abc*+de*f+g*+

Conversion principle:

1. When a operand is read, it is immediately placed in the output. The operator is not immediately output and is placed in the stack. The opening parenthesis is also pushed into the stack.

2. If a closing parenthesis is encountered, the stack element pops up and the symbol is written out until a corresponding opening parenthesis is encountered. But this left parenthesis is only ejected, not output.

3. When the operator is read, if at this point the top operator precedence is greater than or equal to this operator, the top operator pops up until a lower priority element position is found. In addition to processing), never remove the "(" from the stack. operator, +--priority is the lowest, () is the highest priority.

4. If the end of the input is read, the stack element pops up until the stack becomes empty, and the symbol is written to the output.

The solution is as follows:

First, read a, and send to the output, and then + is read into and pressed into the stack. Next, B reads in and sends to the output, at which point the status is as follows:

Stack: output: a B

+

Back Top

Next read into the *, because the priority is higher than the stack top element + Large (principle 3), so is pressed into the top of the stack, and then read into C, and sent to the output:

Stack: output: a b C

+ *

Back Top

And then read into the +, because at this time the top element of the stack is *, the priority is greater than +, so will * pop up, after the original + into the stack top element, because the priority of + and the current reading of the + priority equal, so also be ejected (principle 3), and finally read into the + press into the stack. So the status is as follows:

Stack: output: a b c * +

+

Back Top

The next read-in symbol is (because it has the highest priority, so put it in the stack, and then read into D:

Stack: output: a b c * + D

+ (

Back Top

Continue to read in, at this point read *, unless processed), otherwise (never eject (principle 3), so * is pressed into the stack, then read into E, and sent to the output:

Stack: output: a b c * + D E

+ ( *

Back Top

The symbol to be read back is +, which will eject and output. (principle 3, similar to the previous step), then press + into the stack, then read into F and send to the output:

Stack: output: a b c * + d e * f

+ ( +

Back Top

Now read in), so the stack element pops up until it Encounters "(" (Principle 2):

Stack: output: a b c * + D e * f +

+

Back Top

The following reads A *, is pressed into the stack, then reads into G and outputs:

Stack: output: a b c * + D e * f + g

+ *

Back Top

Now the input is empty, and all the elements in the pop-up stack:

Stack: output: a b c * + D e * f + G * +

Empty

Complete it all from here. (Summary of the data structure and algorithm introduction C + + version of the third edition)

Converting infix expressions to suffix expressions using stacks

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.