Algorithm title: Inverse polish expression (mathematical inverse Polish expression and orthogonal set inverse Polish expression)

Source: Internet
Author: User
Tags arithmetic

First, the preface

In the usual expression, the two-dollar operator is always placed between the two operational objects associated with it, so this notation is also known as infix notation. Each operator is placed after its operation object, called a suffix expression, and the suffix expression is also called an inverse Polish expression. Its advantage is that with only two simple operations, the stack and the stack can handle any ordinary expression of the operation. The operation mode is as follows: If the current character is a variable or a number, then the stack, if the operator, the stack top two elements pop-up for the corresponding operation, the results into the stack, and finally when the expression scan, the stack is the result.

Second, the general algorithm

The general algorithm for converting an ordinary middle-order expression to an inverse Polish expression is:

(1) First constructs an operator stack, which follows the principle of higher priority to the top of the stack.

(2) read a simple arithmetic expression with infix, for convenience, set the right end of the simple arithmetic expression plus the lowest priority of the special symbol "#".

(3) Scan the arithmetic expression from left to right, starting from the first character, and if the character is a number, the end of the number string is parsed and the number string is output directly.

(4) If it is not a number, the character is an operator, and you need to compare priority relationships at this time. The practice is as follows: Compare this character to the precedence of the operator on the top of the operator stack. If the character precedence is higher than the operator at the top of this operator stack, the operator is pushed into the stack. If not, the operator on the top of the stack is popped out of the stack until the top operator's priority is lower than the current operator, and the character is pushed into the stack.

(5) Repeat the above operation (3)-(4) until a simple arithmetic expression is scanned to make sure that all characters are handled correctly, we can convert the simple arithmetic expression of infix expression into a simple arithmetic expression in the inverse Polish representation.

Third, the algorithm flow

Programmed Algorithm Flow:

1, the establishment of operator stack stackoperator used for operators of storage, press into the ' \ '.

2, preprocessing expression, positive and negative sign plus 0 (if a plus sign (minus sign) appears after the front or left parenthesis, the plus sign (minus) is the positive number).

3. Sequential scan expression, if the current character is a number (a symbol with a priority of 0), output the number directly; if the current character is an operator or bracket (a symbol with a priority of not 0), then the 4th is judged.

4, if the current operator is ' (', directly into the stack;

For the arithmetic character, compare the top element of the stack with the priority of the current element: if the top element operator priority >= The current element priority, the stack and the sequential output operator are up to the top element priority < The current element priority, and then the current element is pushed into the stack, if the top element < current element is directly in the stack.

5, repeat the 3rd until the expression scan is complete.

6, order out of the stack and output operators until the top of the stack element is '.

Iv. Related class diagrams

This article mainly includes Rpnexpression base class, Mathexpression and Intersectionunionexpresion.

Mathexpression is mainly used to compute simple mathematical operations (+ 、-、 * and/).

Intersectionunionexpresion is mainly used to compute orthogonal set operations (|, &).

Lazy to use UML to paint, directly using the VS2012 generated class diagram, as follows:

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.