首页 > 代码库 > 用python实现简单的调度场算法
用python实现简单的调度场算法
#因本人水平有限,目前只是简单实现了调度场算法,只支持+,-,*,/,%操作。其他待补充
#!/usr/bin/env python3 # -*- coding:utf-8 -*- from sys import argv from decimal import * def delBlank(str): """ Delete all blanks in the str """ ans = "" for e in str: if e != " ": ans += e return ans def getPriority(c): """ 操作符中:"+、-优先级低,*、/、%优先级高" ,^待续 """ if (c==‘+‘ or c==‘-‘): return 0 elif(c == ‘*‘ or c == ‘/‘ or c == ‘%‘): return 1 elif (c==‘^‘): return 2 def infix2postfix(str): """ 把中值表达式转化为后值表达式。 """ postfix = [] stackOfOperator = [] index,pos,length = -1,0,len(str) while(index < length-1):#python for index in range(length):不行,不馆循环内是否更改index,index都是会从1到length-1。 index +=1 char = str[index] if(char in "0123456789"):#如果是数字,直接输出 pos = index while(index <length and str[index] in "0123456789"): index +=1 postfix.append(str[pos:index]) index -= 1 continue elif(char == ‘)‘):#)右括号,将栈中元素弹出只至左括号,且左括号和右括号不加入到后缀表达式 while(True): tmp = stackOfOperator.pop() if(tmp=="("): break postfix.append(tmp) elif(char == "("):#左括号直接压入栈中 stackOfOperator.append(char) else: if len(stackOfOperator) == 0:#如果栈为空,则直接压入 stackOfOperator.append(char) continue top = stackOfOperator[-1] if (top == ‘(‘):#操作符栈顶为左括号(,则将当前操作符入栈。 stackOfOperator.append(char) continue if(getPriority(char)<=getPriority(top)): #如果此操作符(+,-,*,/,%)的优先级小于或等于栈顶的元素 #则将栈中元素弹出至(1)遇到左括号(2)栈顶元素为更低优先级(3)栈为空为止 #并将弹出的元素加入后缀表达式中,将单曲操作数压入栈中 while(True): tmp = stackOfOperator[-1] if(tmp == ‘(‘ or tmp == getPriority(tmp) < getPriority(char)): break postfix.append(tmp) stackOfOperator.pop() if len(stackOfOperator)==0: break stackOfOperator.append(char) else: #如果当前操作符优先级大于此时当前栈顶操作数,则讲当前操作数压入栈 stackOfOperator.append(char) while(len(stackOfOperator)): postfix.append(stackOfOperator.pop()) return postfix def 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 ‘‘‘ 通过后缀表达式计算数值 1、从左到有遍历表达式的每个数字和符号,若是遇到数字则进栈,遇到运算符则将栈顶两个元素出栈,进行运算并将运算结果进栈 2、遍历完后缀表达式,此时栈中剩余的数字就是运算结果 ‘‘‘ def calculateByPostfix(postifix): stackOfNumber =[] index,length = 0,len(postifix) while index < 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 stackOfNumber if __name__ == ‘__main__‘: #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))
用python实现简单的调度场算法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。