Data structure--infix expression to suffix expression (inverse Polish expression) __ data structure

Source: Internet
Author: User

Infix expression is a general method of arithmetic or logical formula representation. Operator is in infix form in the middle of the operand. For example: 3*4+3-1;

The suffix expression does not contain parentheses, the operator is placed behind two operand objects, all calculations are strictly left-to-right (the precedence rules for operators are no longer considered in the order in which operators appear) such as: (2+1) *3, 2 1 + 3 *

(above from Baidu Encyclopedia)

Specific code implementation: code implementation infix suffix expression

1. infix expression to the suffix of expression

Want to complete this process requires an array arr storage infix expression, a ret store suffix expression, but also with the aid of a secondary stack stack1 store operator, Stack2 store the results of the operation.

Rules:

From left to right to iterate through the infix expression, if the number is directly stored in the suffix expression;

If the operator, for example, operator A, needs to compare a to the stack top operator of the operation Fu Yi Stack1, if the stack1 is empty or the stack top operator takes precedence over a, the A is pressed into the stack1.

If A's priority is less than or equal to the priority of the operator at the top of the Stack1 stack, pop the top operator out of the stack, save it in a RET, and then continue to compare with a, repeating the previous action until a is in the stack.

Note:

Operators ' (' and ') ' are more special. If it is ' (' directly into the stack stack1; if it is ') ' You need to pop the stack1 operator into the RET until it pops up ' (';

Repeat the above work until the operator in the operation Fu Yi Stack1 is ejected into the RET, so the infix expression is converted to the suffix expression;

For example:

Infix expression: 3*1+ (4+6)/2-1

The first step: Take 3,ret[3], stack1[];

Step Two: Take *, ret[3], stack1 [*];

Step three: Take 1, ret[3,1], stack1 [*];

The fourth step, take +,ret[3,1,*], stack1 [+];

Fifth step: Take (, ret[3,1,*], stack1 [+, (];

Sixth step: Take 4, ret[3,1,*,4], stack1 [+, (];

Seventh Step: Take +, ret[3,1,*,4], stack1 [+, (, +];

Eighth step: Take 6,ret[3,1,*,4, 6], Stack1 [+, (, +];

Nineth step: Take), ret[3,1,*,4, 6,+], stack1 [+];

Tenth step: Take/, ret[3,1,*,4, 6,+], stack1 [+,/];

11th Step: Take 2,ret[3,1,*,4, 6,+,2], stack1 [+,/];

12th step: Take-, ret[3,1,*,4,6,+,2,/, +], stack1 [-];

13th step: Take 1,ret[3,1,*,4,6,+,2,/,+,1], stack1 [-];

Processing of stack1:ret[3,1,*,4, 6,+,2,/, +,1,-], stack1 [];

So the suffix expression is: 3,1,*,4, 6,+,2,/, +,1,-;

2. Using suffix expressions to calculate results

rules : The list of stored suffix expressions, the elements in turn into the stack, when the operator, the continuous stack two elements, operation, and then the results into the stack, the final stack of elements left behind is the results of the calculation



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.