infix expression converted to suffix expression

Source: Internet
Author: User

Convert infix to suffix expression

First, the suffix expression evaluates

The suffix expression is also called inverse Polish expression, and its evaluation process can be used to assist the storage of the stack. Assuming that the suffix expression to be evaluated is: 6 5 2 3 + 8 * + 3 + *, the evaluation process is as follows:

1) Traversing the expression, the number encountered is first put into the stack, and the stack looks like this:

2) then reads "+", then pops up 3 and 2, executes 3+2, calculates the result equals 5, and presses the 5 into the stack.

3) Read to 8 and put it directly into the stack.

4) Read "*", eject 8 and 5, execute 8*5, and press the result 40 into the stack. Then the process is similar, read "+", will be 40 and 5 pop, the result of 40+5 45 press into the stack ... And so on The last value to be evaluated is 288.

Second, infix expression to suffix expression 2.1) rules

Infix expression A + b*c + (d * e + f) * g, which is converted to a suffix expression is a b c * + D e * f + g * +.

The conversion process needs to use the stack, the process is as follows:

1) If the operand is encountered, we will output it directly.

2) If an operator is encountered, we put it on the stack and we put it in the stack when we encounter the opening parenthesis.

3) If a closing parenthesis is encountered, the stack element pops up and the output of the operator is ejected until an opening parenthesis is encountered. Note that the left parenthesis only pops up and does not output.

4) If any other operator is encountered, such as ("+", "*", "("), and so on, pops the element from the stack until it encounters an element that finds a lower priority (or the stack is empty). After the elements are ejected, the operators encountered are pressed into the stack. One thing to note is that we only pop up "(", in other cases, we won't pop Up "(").

5) If we read the end of the input, all the elements in the stack are popped in turn.

2.2) Example

There are many rules, and it is easier to tell the whole process with examples. As an example of the above conversion, the input is a + b * C + (d * e + f) *g, the process is as follows:

1) First read a, direct output.

2) Read "+" and put it into the stack.

3) Read B, direct output.

At this point the stack and output are as follows:

4) Read "*" because the top element of the stack "+" priority is lower than "*", so the "*" is pressed directly into the stack.

5) Read C, direct output.

At this point the stack and output are as follows:

6) Read "+", because the top element of the stack "*" priority higher than it, so Pop "*" and output, similarly, the next element of the stack "+" priority and read the operator "+", so also to eject and output. The read "+" is then pressed into the stack.

At this point the stack and output are as follows:

7) The next read is "(", it has the highest priority, so put it directly into the stack.

8) Read D and output it directly.

At this point the stack and output are as follows:

9) Read "*", because only encountered ")" when the opening parenthesis "(" will only pop up, so "*" directly into the stack.

10) Read E, direct output.

At this point the stack and output are as follows:

11) Read "+", pop "*" and output, then press "+" into the stack.

12) Read F, direct output.

At this point the stack and output conditions:

13) Next read "), the elements in the stack will be ejected and output until" (") is encountered. Here, only one operator "+" is ejected and output before the right parenthesis.

14) Read "*" and press into the stack. Read to G, direct output.

15) At this time the input data has been read to the end, the stack has two operators "*" and "+", directly eject and output.

This completes the entire conversion process. Temporarily only provides the idea, does not provide the code.

Reproduced in original: https://www.cnblogs.com/hantalk/p/8734511.html

infix expression converted to suffix expression

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.