ZH cheese: Python infix expression conversion suffix expression

Source: Internet
Author: User

A program that implements an infix expression conversion suffix expression that can handle the addition and subtraction of multiplier operations:

An input infix expression inorder

A pool of output pools

A stack of cache stacks

Read Inorder verbatim from front to back

First look at the brackets that do not include:

(1) Operand: direct output to Pool

(2) Operator: Determines the precedence of the current operator and the Stack[top] operator

<1> current operator precedence higher than stack[top]: Adds the current operator to the stack;

<2> the current operator priority is less than or equal to Stack[top]: start the stack from stack[top], until Stack[top] takes precedence over the current operator and then the current operator into the stack;

(3) When the Inorder traversal is complete, if the stack is not empty, reverse the stack and add to the pool tail

1 __author__='Zhanghe'2 defIn2post (inorder):3Pool ="'4stack ="'5      forIinchinorder:6RET =proc (I,switch (i), Stack,pool)7stack = ret['Stack']8Pool = ret['Pool']9     ifStack! ="':Tenstack = stack[::-1] One     returnpool+Stack A  - defswitch (c): -Operator1 ='+-' theOperator2 ='*/' -num ='ABCDEFG' -     ifCinchNum: -         return0 +     ifCinchOperator1: -         return1 +     ifCinchOperator2: A         return2 at defproc (c,op,stack,pool): -top = len (stack)-1 -  -     ifOp = = 0:#ABCDEFG -Pool + =C -     ifOP = = 2orOp==1:#* in         iftop = =-1: -Stack + =C to         elifSwitch (Stack[top]) <op: +Stack + =C -         Else: the              whileTop! =-1 andSwitch (Stack[top]) >=op: *Pool + =Stack[top] $Top-= 1Panax Notoginseng             ifTop! =-1: -stack = stack[0:top+1] the             Else: +stack ="' AStack + =C theRET = { +         'Stack': Stack, -         'Pool':p ool $     } $     returnret -Inorder ='a+b*c-d*e+f/g'print In2post (inorder)
What factors should be considered when enclosing parentheses?

On the basis of the previous procedure, consider the following points further:

(4) After the parentheses, "(" has the highest priority, that is, encountered "(" Put in the stack is OK;

(5) ")" operation is also relatively simple, directly to the stack of elements popped into the pool, until the popup "(";

A new operator rule is required:

(5) Operator: Determines the precedence of the current operator and the Stack[top] operator

<1> the current operator precedence is higher than stack[top] or stack[top]== ' (': adds the current operator to the stack;

<2> the current operator priority is less than or equal to Stack[top]: from Stack[top] start out of the stack until Stack[top] priority is higher than the current operator or Stack[top] is "(", then the current operator into the stack;

1 __author__='Zhanghe'2 defIn2post (inorder):3Pool ="'4stack ="'5      forIinchinorder:6RET =proc (I,switch (i), Stack,pool)7stack = ret['Stack']8Pool = ret['Pool']9     ifStack! ="':Tenstack = stack[::-1] One     returnpool+Stack A  - defswitch (c): -Operator1 ='+-' theOperator2 ='*/' -Operator3 =')' -Operator4 ='(' -num ='ABCDEFG' +     ifCinchNum: -         return0 +     ifCinchOperator1: A         return1 at     ifCinchOperator2: -         return2 -     ifCinchOperator3: -         return3 -     ifCinchOperator4: -         return4 in defproc (c,op,stack,pool): -top = len (stack)-1 to  +     ifOp = = 0:#ABCDEFG -Pool + =C the     ifOP = = 2orOp==1:#* *         iftop = =-1: $Stack + =CPanax Notoginseng         elifSwitch (Stack[top]) < oporStack[top] = ='(': -Stack + =C the         Else: +              whileTop! =-1 andSwitch (Stack[top]) >= op andSwitch (Stack[top]) <switch (')'): APool + =Stack[top] theTop-= 1 +             ifTop! =-1: -stack = stack[0:top+1] $             Else: $stack ="' -Stack + =C -     ifOP = = 3:#) the          whiletop!=-1 andStack[top]! ='(': -Pool + =Stack[top]WuyiTop-= 1 thestack =Stack[0:top] -     ifOP = = 4:#( WuStack + =C -RET = { About         'Stack': Stack, $         'Pool':p ool -     } -     returnret - #inorder = ' a+b*c-d*e+f/g ' AInorder ='a+b*c+ (d*e+f) *g' + PrintIn2post (inorder)

Output:

abc*+de*f+g*+

ZH cheese: Python infix expression conversion suffix 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.