# Inverse Polish algorithm

Source: Internet
Author: User

Recently in the swift course of learning Stanford, a calculator was developed that requires the addition of an infix suffix algorithm. So in this review under the algorithm thought.

A polynomial can generally be written in infix expressions and suffix expressions (not many prefixes), infix is actually the normal context of people in the logical way, give two examples:

A1. 1+2*3+ (4+5) *6

B1. ((1+2) *3+4) *5+6

Infix expression machine is not easy to understand, the suffix expression is relatively friendly to the machine, postfix expression is also known as the inverse of the Polish style, defined as follows

The suffix form of an expression E can be defined as follows: (1) If E is a variable or constant, then the suffix of e is e itself. (2) If E is an expression in the form of E1 op E2, where OP is the two-dollar operator, then the suffix of e is E1 ' E2 ' op, where E1 ' and E2 ' are respectively E1 and E2 suffixes. (3) If E is an expression in the form of (E1), then the suffix of E1 is the suffix of e. Such as: We usually write a+b, this is infix expression, written suffix expression is: ab+ (a+b) *c-(a+b)/E suffix expression is: (A+b) *c-(a+b)/e→ ((a+b) *c) ((a+b)/E)-→ ((a+b) c*) ((a+b) e/)- → (ab+c*) (ab+e/)-→ab+c*ab+e/-  above a bunch of how to form a infix suffix, used in A1 is A1. 1+2*3+ (4+5) *6a1*. (1+ (2*3)) + ((4+5) *6), (1+ (2,3,*)) + ((4,5,+) *6), (1, (2,3,*), +) + ((4,5,+), 6,*), (1,2,3,*,+) + (4,5,+,6,*)- > (1,2,3,*,+), (4,5,+,6,*),+->1,2,3,*,+,4,5,+,6,*,+  program on how to implement it? A simple way is to use the stack (with the tree traversal also line, is to create a tree trouble point) generally with two temporary stack, S1 used to put operators, subtraction, brackets and so on. Placing a # operator on the bottom of the stack represents the lowest precedence operator. S2 is used to put the output suffix, the operand is also directly pressed into here.   Assuming that only subtraction and parentheses are included, the algorithm rules are as follows: Reading infix expressions from left to right, such as "1+2*3+ (4+5) *6", pops up "1" and finally to "6" 1. Read the operand and press it directly into the top of the S2 stack. 2. Read the non-parenthesis operator by a finite-level operation if the operator has precedence over the top operator of the S1 stack, it is pressed directly into the top of the stack. (here can explain why put a # at the bottom of the stack, because the first operator can compare with the #, the program is normalized) if the operator is less than the S1 stack top operator priority, then pop the S1 stack top and press into S2 until the current operator is above the top of the S1 stack and then press the current operator into the top of the S1 stack. 3. Read (press directly into the top of the S1 stack. 4. Read), then pops up from the top of the S1 stack (all the operators to the top of the stack are pressed into the S2, then the (. 5. When the prefix expression is read, the remaining operators in the S1 are popped out one by one and pressed into the S26.S2, which is the suffix expression (from bottom to top, if the S2 is reversed by the stack)    A1. 1+2*3+ (4+5) *6s1.#,s2. s1.#,+s2.1,2 s1.#,+,*s2.1,2,3 s1.#,+,s2.1,2,3,*,+ s1.#,+, (, S2.1,2,3, *,+ s1.#,+, (, +s2.1,2,3,*,+,4,5 s1.#,+,s2.1,2,3,*,+,4,5,+ s1.#,+,*s2.1,2,3,*,+,4,5,+6 s1.# s2.1,2,3,*,+,4,5,+,6,*,+ (end)  b1. ((1+2) *3+4) *5+6 s1.#, (+s2.1,2 s1.#, (s2.1,2,+ s1.#, (, *s2.1,2,+,3 s1.#, (, +S2.1,2,+,3,*,4  s1. #S2.1,2,+,3,*,4,+, s1.#,*s2.1,2,+,3,*,4,+,5, s1.#,+s2.1,2,+,3,*,4,+,5,*,6 s1. #S2.1,2,+ , 3,*,4,+,5,*,6,+ (end)

Inverse Polish algorithm

Related Keywords:

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.