#因本人水平有限, It is simple to implement the scheduling field algorithm, only support +,-, *,/,% Operation. Other additions
#!/usr/bin/env python3#-*-coding:utf-8-*-from sys import argvfrom decimal import *def delblank (str): "" "Delete A ll blanks in the str "" "ans =" "for e in str:if e! =" ": ans + = e return ansdef Getprio rity (c): "" "operator:" +,-priority low, *,/,% Priority High ", ^ to be continued" "" if (c== ' + ' or c== '-'): return 0 elif (c = = ' * ' or c = = '/' or c = = '% '): return 1 elif (c== ' ^ '): return 2def infix2postfix (str): "" "converts The median expression to a post-value Expression. "" "postfix = [] Stackofoperator = [] index,pos,length = -1,0,len (str) while (index < length-1): #python for Index in range (length): no, no, The change of index,index in the library loop is from 1 to length-1. Index +=1 char = str[index] if (char in "0123456789"): #如果是数字, Direct Output pos = index while (ind Ex <length and str[index] in "0123456789"): index +=1 postfix.append (str[pos:index]) index-= 1 Continue elif (char = = ') ': #) right parenthesis, pops the stack element to the left parenthesis, and the opening and closing parentheses are not joined to the suffix expression while (True): tmp = Stackofoperator.pop () if (tmp== "("): Break Postfix.append (tmp) elif (char = = "("): #左括号直接压入栈中 stackofoperator.append (char) else:if len (stackofoperator) = = 0: #如果栈为空, press directly into Stackofoperator.append (char) Continue top = stackofoperator[-1] if (top = = '): #操作符栈顶为左括号 (, The current operator is put into the stack.) Stackofoperator.append (char) Continue if (getpriority (char) <=getpriority (top)): #如果 The precedence of this operator (+,-, *,/,%) is less than or equal to the element at the top of the stack #则将栈中元素弹出至 (1) when an opening parenthesis (2) is encountered, the top element of the stack is empty until the lower priority (3) stack is null #并将弹出的元素加入后缀表达式中, and the single operand is pressed into the stack While (True): tmp = stackofoperator[-1] if (tmp = = ' (' or TMP = = Getprior ity (tmp) < getpriority (char)): break Postfix.append (tmp) S Tackofoperator.pop () If Len (stackofoperator) ==0:break stackofoperator.append (char) Else: #如果当前操作符优先级大于此时当前栈顶操作数, The current operand is pressed into the stack stackofoperator.append (char) while (len (stackofop erator)): postfix.append (stackofoperator.pop ()) return postfixdef operate (num1,num2,operator): if operator = = "+": return NUM1 + num2 elif operator = = "-": return num1-num2 elif operator = = "*": return NUM1 * num2 elif operator = = "/": return num1/num2 elif operator = = "%": return num1% num2 elif operator = = "^": return NUM1 * * num2 "is calculated by the suffix expression value 1, from left to the traversal expression of each number and symbol, if the number is encountered in the stack, encountered the operator will stack top two elements out of the stack, the operation and the result of the stack 2, after traversing Prefix expression, the remaining number in the stack is the result of the operation "def calculatebypostfix (postifix): stackofnumber =[] index,length = 0,len (postifix) while I Ndex < Length:e = postifix[index] if E in "+-*/%": #后进先出 num2 = float (stackofnumber.pop ()) NUM1 = float (staCkofnumber.pop ()) stackofnumber.append (operate (num1,num2,e)) print (stackofnumber) else: Stackofnumber.append (e) Print (stackofnumber) index + = 1 return stackofnumberif __name__ = = ' __m ain__ ': #get the EXP exp = "" for i in range (len (argv)-1): exp + = argv[i+1] exp = Delblank (exp) postfix= Infix2postfix (exp) print (postfix) print (calculatebypostfix (postfix))
Using Python to implement a simple scheduling field algorithm