Created
February 13, 2017 15:33
-
-
Save MagicHen25/4c2ae0ef1a510e5dbd0b41e7c9e22983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) ) | |
''' | |
import re | |
calcStr = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )" | |
calcStr = re.sub("\ ","",calcStr) # 清除输入中的空格 | |
sepStr = "" | |
def multiplyDivideFunc(inStr): | |
outList = list() | |
if "*" in inStr: | |
# print("Multiply in inStr") | |
outList = inStr.split("*") | |
# print(float(outList[0])*float(outList[1])) | |
return float(outList[0])*float(outList[1]) | |
elif "/" in inStr: | |
# print("Divide in inStr") | |
outList = inStr.split("/") | |
# print(float(outList[0]) / float(outList[1])) | |
return float(outList[0]) / float(outList[1]) | |
else: | |
# print("Only a digit:", float(inStr)) | |
return float(inStr) | |
def strExecute(inStr): #查找字符串中的最高优先级算式,并返回字符串 | |
# print(inStr) | |
outStr = "" | |
outStr = re.search("\([\(\)\+\-\*/\d]+",inStr) | |
if ((outStr is not None) and ("(" in inStr)): | |
outStr = strExecute(outStr.group(0)[1:]) | |
# return outStr | |
return re.search("[^\(\)]*",outStr).group(0) | |
else: | |
# return inStr | |
return re.search("[^\(\)]*",inStr).group(0) | |
def plusMinusDiv(inStr): | |
ret = re.split("[\-\+]",inStr) | |
if ret[0] == '': | |
ret.pop(0) | |
ret[0] = '-1' + ret[0] | |
return ret | |
sepStr = strExecute(calcStr) | |
print("[Final]:",sepStr) | |
r = plusMinusDiv("-9-2*5/3+7/3*99/4*2998+10*568/14") | |
print(r) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment