Inverse Polish expression

Source: Internet
Author: User
Tags stack pop

The inverse Polish expression is also called the suffix expression. In the usual expression, the two-dollar operator is always placed between the two operands associated with it, and this notation is also known as infix notation. Polish logic J.lukasiewicz in 1929 another way of expressing expressions, in which each operator is placed after its operand, is called a suffix.

expression: inverse Polish expression, its syntax stipulates that the expression must be given in inverse Polish expression. The inverse Polish expression is also called the suffix expression. This knowledge point is presented in the two courses of data structure and compiling principle, here are some examples:

Normal expression---> inverse Polish expression a+b---> a,b,+a+ (b-c)---> A,b,c,-, +a+ (b-c) *d---> A,b,c,-, d,*,+a+d* (b-c)---> A,d,b,c,-, *,+a =1+3---> a=1,3 +http= (smtp+http+telnet)/1024---> http=smtp,http,+,telnet,+, 1024,/ use:The inverse Polish expression is a very useful expression that transforms a complex expression into an expression that can rely on a simple operation to get the result of the calculation. For example (a+b) * (c+d) converted to ab+cd+* Advantages:Its advantage is that only two simple operations, into the stack and out of the stack can be done for any ordinary expression operation. The operation is as follows: If the current character is a variable or a number, then the stack, if it is an operator, the top two elements of the stack pop up for the corresponding operation, the result is then into the stack, and finally when the expression scan, the stack is the result. General algorithm:The general algorithm for converting a normal middle-order expression to an inverse Polish expression is: (1) first constructs an operator stack, which follows the higher precedence of the stack within the stack. (2) read a simple arithmetic expression denoted by infix, for convenience, set the right end of the simple arithmetical expression plus the lowest priority special symbol "#". (3) scan from left to right the arithmetic expression, starting from the first character, if the character is a number, then parse to the end of the number string and output the number directly. (4) If it is not a number, the character is an operator, at which time the precedence relationship needs to be compared. This is done as follows: Compares this character to the precedence of the operator at 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 put into the stack. If not, the top operator is popped out of the stack until the top of the stack operator is lower than the current operator and the character is placed on the stack. (5) Repeat the above operation (3)-(4) until the entire arithmetic expression is scanned, to make sure that all characters are handled correctly, we can convert the simple arithmetic expression of infix notation to a simple arithmetic expression in inverse Polish notation. Algorithm Flow:The following is the programmatic algorithm flow: 1, the establishment of the operator stack stackoperator for the storage of operators, pressing into ' \ '. 2, preprocessing expression, positive, minus sign plus 0 (if a plus sign (minus) appears in front of the first or after the opening parenthesis, then the plus sign (minus) is signed). 3, sequential scan expression, if the current character is a number (priority 0 symbol), the direct output of the number, if the current character is an operator or parenthesis (priority is not a 0 symbol), then the 4th is judged. 4, if the current operator is ' (', directly into the stack, if ') ', out of the stack and sequential output operator until the first ' (', the first encountered ' ('), but not the output, if the arithmetic character, the top element of the stack and the current element precedence: If the top of the stack operator precedence >= the current element priority , out of the stack and sequential output operator until the top element of the stack priority < current element priority, and then the current element into the stack, if the top element of the stack < The current element, directly into the stack. 5, repeat the 3rd until the expression scan is complete. 6, sequentially out of the stack and output operators until the top of the stack element is ' \. ' Priority level:Priority is divided into the intra-stack priority ISP (on stack priority) and the out-of-stack precedence ICP (in coming priorities). In addition to parentheses, the precedence of the operators after the stack has risen by 1, which can be reflected in the infix expression of the same priority operator from left to right computing requirements, so that the operation at the top of the stack Mr. Foo the stack and output. below excerpt from: http://blog.csdn.net/geniusluzh/article/details/8192780The method of converting an in-order expression into an inverse Polish expression is actually simple and a molding algorithm. It is also very simple to calculate the value of an equation by reversing the Polish expression, as long as it is used when encountered.

1. Convert a middle order expression into an inverse Polish expression.

The first thing to maintain is two stacks, and the last thing we call S1 and s2,s1 here is the inverse Polish expression, which will be emptied when the S2 is used to temporarily store the operator and eventually form an inverse Polish expression. Let's look at how to form a counter-Polish expression.

Here we first define the precedence relationship of the operators, from the beginning of the order, the same priority is not separated by commas: (, +-,*\, minus,).

A given middle order expression is traversed from left to right, which is the expression of our regular mathematical calculations.

(1) If a number is encountered, we are directly added to the stack S1;

(2) If an opening parenthesis is encountered, the opening parenthesis is added directly to the stack S2;

(3) If a closing parenthesis is encountered, then the operator in the stack S2 is added to the stack S1, until an opening parenthesis is encountered, but the left bracket S2 not added to the stack S1;

(4) If you encounter an operator, including the monocular operator and the binocular operator, we follow the following rules:

(1) If the stack S2 is empty at this time, the operator is added directly to the stack S2;

(2) If the stack S2 is not empty at this time, the currently traversed operator priority is greater than or equal to the top of the stack operator priority, then directly into the stack S2;

(3) If the stack S2 is not empty at this time, the priority of the currently traversed operator is less than the precedence of the top operator, then the stack top operator is added to the stack S1 until the stack is empty or the precedence of an operator is less than equal to the priority of the currently traversed operator, at which point the operator is added to the stack S2;

(5) until the complete sequence expression is traversed, the operators still exist in the stack S2, then the operators are added to the stack S1 in sequence until the stack is empty.

In accordance with the above five operations repeatedly completed, then the stack S1 is stored in reverse Polish expression.


2. Evaluate with inverse Polish expression

Using inverse Polish expressions to calculate the value of a formula is actually very simple, because of this, the inverse Polish expression is used to calculate the value of an expression in the compilation principle.

The following is a concrete look at how to find the value of an inverse Polish expression:

We maintain a data result stack S3 at this point, and we will see that the last value in the stack is the final expression. We traverse the stack from left to right S1, and then follow the rules below to operate the stack S3.

(1) If the number is encountered, then the number is directly pressed into the S3;

(2) If the monocular operator is encountered, then take an element of the top of the S3 stack for a single-mesh operation, and then press the result again into the stack S3;

(3) If you encounter a binocular operator, then take the top of the S3 stack of two elements, first out of the stack on the left, and then out of the stack on the right of the binocular operator calculation, the results are again pressed into the S3.

According to the above three rules, traversing the entire stack S1, then the value in the last S3 is the inverse of the Polish expression value, so we can see that the inverse Polish expression is very simple to evaluate, only two operations are either direct pressure stack, or the result after the operation of the stack.

It is now clear how to convert a middle order expression into an inverse Polish expression and how to evaluate an inverse Polish expression. Do not know the above description of what is not clear or because of the slip of the tongue and did not describe the correct, please the reader to help correct the eyes, thank you again first!

Inverse Polish 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.