Inverse Polish expression algorithm principle record

Source: Internet
Author: User

Original inverse Polish algorithm (negative number, function call condition not considered)

1, from left to right scan an infix expression.
2. If the operand is read, the type of the operand is judged and the operand is deposited into the operand stack
3, if the operator is read
(1) The operator is an opening parenthesis "(", which is directly deposited into the operator stack.)
(2) The operator is a closing parenthesis ")", then the operator on the operator stack is output to the operand stack until an opening parenthesis is encountered, and the opening parenthesis is discarded at this time.
(3) The operator is a non-parenthesis operator:
(a) if the operator on the top of the stack stack is an opening parenthesis, it is directly deposited into the operator stack.
(b) If the operator at the top of the stack stack has a higher precedence, it is directly deposited into the operator stack.
(c) If the operator at the top of the stack stack is lower or equal, the output stack top operator is to the operand stack until the Fu top operator is below (not equal to) the operator precedence, or an opening parenthesis,
and presses the current operator into the operator stack.
4. When there are operators in the operator stack after the expression reads are complete, the operator is removed sequentially to the operand stack until the operator stack is empty. improved inverse Polish algorithm (considering negative numbers, function call cases)

1. Build two stacks operand (operand stacks) and operator (Operation Fu Yi) and a last_token tag field.

2. Scan the given string, and when scanning, take care to skip the space and extract the complete operand, operator, and function.

3. If you get an operator (in lieu of B) to construct the operator of the response first, the construct needs to compare the Last_token record field to determine the meaning of the overloaded operator, such as if Last_token is a number, then "-" is a mathematical operator if last_ Token is an operator, then it depends on "-" is not a number, if it is a number, then "-" is the "minus". After constructing the operator and operator stack top elements (replaced with a) comparison:

1) If a does not exist, the B is pressed into the operator stack;

2) If B is an opening parenthesis, then the priority comparison of A and B is ignored, and B is pressed into the operator stack.

3) If B is a comma, it also ignores A and B comparisons and presses B into the operator stack.

4) If B is a closing parenthesis, then put the operator stack sequence out of the stack, and then put the popup element order into the operand stack, out of the stack is the need to determine the comma case, if it is a comma, then not into the stack, record the number of commas, until the top of the stack is left parenthesis, brackets are not in the operand stack After looking at the operator stack top element, if the stack top element is a function type, then the function is operator out of the stack and then the number of comma number +1 constructs the number operand into the operand stack, and then the function operator also pressed into the operand stack.

5) If a is an opening parenthesis, press B directly into the operator stack.

6) If B priority is higher than a, press B directly into the operator stack.

7) If B priority is less than or equal to the priority of a, then put a out stack and then press into the operand stack, repeat this step until the top of the stack priority is higher than the priority of B or the top of the stack is a parenthesis.

4. After the scan is complete, put the elements of the operator stack out of the stack sequentially, then press into the operand stack.

Inverse Polish expression algorithm principle record

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.