用python實現簡單的調度場演算法

來源:互聯網
上載者:User

標籤:post   補充   []   調度   length   python   blog   let   env   

#因本人水平有限,目前只是簡單實現了調度場演算法,只支援+,-,*,/,%操作。其他待補充

#!/usr/bin/env python3# -*- coding:utf-8 -*-from sys import argvfrom decimal import *def delBlank(str): """ Delete all blanks in the str """ ans = "" for e in str: if e != " ": ans += e return ansdef getPriority(c): """ 操作符中:"+、-優先順序低,*、/、%優先順序高" ,^待續 """ if (c==‘+‘ or c==‘-‘): return 0 elif(c == ‘*‘ or c == ‘/‘ or c == ‘%‘): return 1 elif (c==‘^‘): return 2def 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 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‘‘‘通過尾碼運算式計算數值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 stackOfNumberif __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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.