Merge pull request #3 from weilinfox/master

bc up
This commit is contained in:
Si-Huan 2020-03-02 20:46:50 +08:00 committed by GitHub
commit 811ebbd8e6

View file

@ -4,6 +4,8 @@
from zzcore import StdAns from zzcore import StdAns
divError = False
#函数段 #函数段
def reportErr (errorNo, ch): def reportErr (errorNo, ch):
@ -17,6 +19,8 @@ def reportErr (errorNo, ch):
return "一个数小数点那么多拿来做什么嘞!" return "一个数小数点那么多拿来做什么嘞!"
elif (errorNo == 5): elif (errorNo == 5):
return "多出来的小数点很孤单的啦~" return "多出来的小数点很孤单的啦~"
elif (errorNo == 6):
return "啊吓死我了0怎么做除数了啊……"
else: else:
return "啊呀呀这是啥错误啊?问问狸去:" + ch return "啊呀呀这是啥错误啊?问问狸去:" + ch
@ -32,10 +36,15 @@ def supportCh (ch):
def exprCal (ch): def exprCal (ch):
#处理不带括号的表达式 #处理不带括号的表达式
global divError
mulflag = False mulflag = False
plusflag = False plusflag = False
for i in range(0, len(ch), 1): if (ch[0] == '-'):
if (ch[i] == "+" or ch[i] == '-'): ch = '0' + ch
ch = ch.replace("--", "+")
ch = ch.replace("+-", "-")
for i in range(1, len(ch), 1):
if ((ch[i] == "+" or ch[i] == '-') and ch[i-1] != '/' and ch[i-1] != '*'):
plusflag = True; plusflag = True;
elif (ch[i] == '*' or ch[i] == '/'): elif (ch[i] == '*' or ch[i] == '/'):
mulflag = True; mulflag = True;
@ -59,6 +68,8 @@ def exprCal (ch):
break; break;
fmul -= 1 fmul -= 1
lmul += 1 lmul += 1
if (ch[lmul] == '-' or ch[lmul] == '+'):
lmul += 1
while (fmul >= 0 and (ch[fmul].isdigit() or ch[fmul] == '.')): while (fmul >= 0 and (ch[fmul].isdigit() or ch[fmul] == '.')):
fmul -= 1 fmul -= 1
while (lmul < len(ch) and (ch[lmul].isdigit() or ch[lmul] == '.')): while (lmul < len(ch) and (ch[lmul].isdigit() or ch[lmul] == '.')):
@ -81,6 +92,9 @@ def exprCal (ch):
i += 1 i += 1
if (lst == 0): if (lst == 0):
num2 = float(ch[fst+1:]) num2 = float(ch[fst+1:])
if (num2 == 0.0):
num2 = 1
divError = True
num1 = float(ch[:fst]) num1 = float(ch[:fst])
if (ch[fst] == '*'): if (ch[fst] == '*'):
return str(num1 * num2) return str(num1 * num2)
@ -90,6 +104,8 @@ def exprCal (ch):
ch = exprCal(ch[:lst]) + ch[lst:] ch = exprCal(ch[:lst]) + ch[lst:]
elif (plusflag): elif (plusflag):
while (1): while (1):
ch = ch.replace("+-", "-")
ch = ch.replace("--", "+")
fst = lst = 0 fst = lst = 0
i = 0 i = 0
for c in ch: for c in ch:
@ -132,6 +148,7 @@ def bracketExprCal (ch):
return bracketExprCal(ch[:fbr] + bracketExprCal(ch[fbr+1:lbr]) + ch[lbr+1:]) return bracketExprCal(ch[:fbr] + bracketExprCal(ch[fbr+1:lbr]) + ch[lbr+1:])
def bcMain(com): def bcMain(com):
global divError
#预处理 #预处理
#字符替换 #字符替换
com = com.replace(" ", "") com = com.replace(" ", "")
@ -142,6 +159,13 @@ def bcMain(com):
com = com.replace("}", ")") com = com.replace("}", ")")
com = com.replace("[", "(") com = com.replace("[", "(")
com = com.replace("]", ")") com = com.replace("]", ")")
com = com.replace("+-", "-")
com = com.replace("--", "+")
com = com.replace("*+", "*")
com = com.replace("/+", "/")
com = com.replace("(-", "(0-")
com = com.replace("(+", "(0+")
com = com.replace("()", "")
com = com.replace("x", "*") com = com.replace("x", "*")
com = com.replace("X", "*") com = com.replace("X", "*")
@ -149,8 +173,11 @@ def bcMain(com):
for i in range(1, len(com)-1, 1): for i in range(1, len(com)-1, 1):
if (com[i] == '(' and (com[i-1].isdigit() or com[i-1] == '.' or com[i-1] == ')')): if (com[i] == '(' and (com[i-1].isdigit() or com[i-1] == '.' or com[i-1] == ')')):
com = com[:i] + "*" + com[i:] com = com[:i] + "*" + com[i:]
elif (com[i] == ')' and (com[i+1].isdigit() or com[i+1] == '.' or com[i+1] == '(')): elif (com[i] == ')' and (com[i+1].isdigit() or com[i+1] == '.' or com[i+1] == '(' or com[i+1] == '-')):
com = com[:i+1] + "*" + com[i+1:] com = com[:i+1] + "*" + com[i+1:]
#部分情况开头加0
if (com[0] == '+' or com[0] == '-' or com[0] == '.'):
com = '0' + com
#print(com) #print(com)
@ -174,7 +201,10 @@ def bcMain(com):
#运算符及小数点位置合法性 #运算符及小数点位置合法性
ncom = com.replace("(", "") ncom = com.replace("(", "")
ncom = ncom.replace(")", "") ncom = ncom.replace(")", "")
if (not ncom[0].isdigit() and ncom[0] != '.'):
if (not ncom[0].isdigit() and ncom[0] != '+' and ncom[0] != '-' and ncom[0] != '.'):
return reportErr(3, ncom[0])
elif ((ncom[0] == '+' or ncom[0] == '-') and (len(ncom) < 2 or not ncom[1].isdigit())):
return reportErr(3, ncom[0]) return reportErr(3, ncom[0])
elif (ncom[0] == '.' and (len(ncom) < 2 or not ncom[1].isdigit())): elif (ncom[0] == '.' and (len(ncom) < 2 or not ncom[1].isdigit())):
return reportErr(5, "") return reportErr(5, "")
@ -183,8 +213,14 @@ def bcMain(com):
elif (ncom[len(ncom)-1] == '.' and (len(ncom) < 2 or not ncom[len(ncom)-2].isdigit())): elif (ncom[len(ncom)-1] == '.' and (len(ncom) < 2 or not ncom[len(ncom)-2].isdigit())):
return reportErr(5, "") return reportErr(5, "")
for i in range(1, len(ncom)-1, 1): for i in range(1, len(ncom)-1, 1):
if (ncom[i] == "+" or ncom[i] == "-" or ncom[i] == "*" or ncom[i] == "/"): #if (ncom[i] == "+" or ncom[i] == "-" or ncom[i] == "*" or ncom[i] == "/"):
if ((not ncom[i-1].isdigit() and ncom[i-1] != '.') or (not ncom[i+1].isdigit() and ncom[i+1] != '.')): # if ((not ncom[i-1].isdigit() and ncom[i-1] != '.') or (not ncom[i+1].isdigit() and ncom[i+1] != '.')):
# return reportErr(3, ncom[i])
if (ncom[i] == "+" or ncom[i] == "-"):
if ((not ncom[i-1].isdigit() and ncom[i-1] != '.' and ncom[i-1] != "*" and ncom[i-1] != "/") or (not ncom[i+1].isdigit() and ncom[i+1] != '.')):
return reportErr(3, ncom[i])
if (ncom[i] == "*" or ncom[i] == "/"):
if ((not ncom[i-1].isdigit() and ncom[i-1] != '.') or (not ncom[i+1].isdigit() and ncom[i+1] != '.' and ncom[i+1] != "-" and ncom[i+1] != "+")):
return reportErr(3, ncom[i]) return reportErr(3, ncom[i])
elif (ncom[i] == '.'): elif (ncom[i] == '.'):
if (not ncom[i-1].isdigit() and not ncom[i+1].isdigit()): if (not ncom[i-1].isdigit() and not ncom[i+1].isdigit()):
@ -207,7 +243,11 @@ def bcMain(com):
if (dotNum > 1): if (dotNum > 1):
return reportErr(4, ".") return reportErr(4, ".")
return bracketExprCal(com) ans = str(bracketExprCal(com))
if (divError):
divError = False
return reportErr(6, "")
return ans
#代码段 #代码段
class Ans (StdAns): class Ans (StdAns):