Merge pull request #5 from weilinfox/master

fix some bug and add some function
This commit is contained in:
Si-Huan 2020-03-02 22:55:32 +08:00 committed by GitHub
commit c66679b07d

View file

@ -3,8 +3,10 @@
# 支持语法检查
from zzcore import StdAns
import math
divError = False
overflowError = False
#函数段
@ -21,6 +23,12 @@ def reportErr (errorNo, ch):
return "多出来的小数点很孤单的啦~"
elif (errorNo == 6):
return "啊吓死我了0怎么做除数了啊……"
elif (errorNo == 7):
return "数字太大了,小身板撑不住啊……"
elif (errorNo == 8):
return "好像有奇奇怪怪的东西ummm, \"" + ch + "\"好像不能有……"
elif (errorNo == 9):
return "运算符太多了,算不过来啊……"
else:
return "啊呀呀这是啥错误啊?问问狸去:" + ch
@ -37,6 +45,7 @@ def supportCh (ch):
def exprCal (ch):
#处理不带括号的表达式
global divError
global overflowError
mulflag = False
plusflag = False
if (ch[0] == '-'):
@ -97,6 +106,9 @@ def exprCal (ch):
divError = True
num1 = float(ch[:fst])
if (ch[fst] == '*'):
if (num1 * num2 >= 1e16):
overflowError = True
return '1'
return str(num1 * num2)
elif (ch[fst] == '/'):
return str(num1 / num2)
@ -120,6 +132,9 @@ def exprCal (ch):
num2 = float(ch[fst+1:])
num1 = float(ch[:fst])
if (ch[fst] == '+'):
if (num1 + num2 >= 1e16):
overflowError = True
return '1'
return str(num1 + num2)
elif (ch[fst] == '-'):
return str(num1 - num2)
@ -149,6 +164,7 @@ def bracketExprCal (ch):
def bcMain(com):
global divError
global overflowError
#预处理
#字符替换
com = com.replace(" ", "")
@ -166,6 +182,7 @@ def bcMain(com):
com = com.replace("(-", "(0-")
com = com.replace("(+", "(0+")
com = com.replace("()", "")
com = com.replace("%", "/100")
com = com.replace("x", "*")
com = com.replace("X", "*")
@ -213,9 +230,6 @@ def bcMain(com):
elif (ncom[len(ncom)-1] == '.' and (len(ncom) < 2 or not ncom[len(ncom)-2].isdigit())):
return reportErr(5, "")
for i in range(1, len(ncom)-1, 1):
#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] != '.')):
# 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])
@ -247,17 +261,108 @@ def bcMain(com):
if (divError):
divError = False
return reportErr(6, "")
elif (overflowError):
overflowError = False
return reportErr(7, "")
return ans
def funMain (ch):
ch = ch.replace(" ", "")
if (ch[0:5] == 'sqrt(' and ch[len(ch)-1] == ")"):
ch = ch[5:len(ch)-1]
#print(ch)
dotNum = 0
for c in ch:
if (c == '.'):
dotNum += 1
elif (not c.isdigit()):
return reportErr(8, c)
if (dotNum > 1):
return reportErr(4, "")
return str(math.sqrt(float(ch)))
elif (ch[len(ch)-1] == '!'):
ch = ch[:len(ch)-1]
#print(ch)
for c in ch:
if (not c.isdigit()):
return reportErr(8, c)
n = int(ch)
if (n == 0):
return '1'
else:
ans = 1
for i in range(2, n+1, 1):
ans *= i
return str(ans)
else:
#print(ch)
dotNum = 0
for c in ch:
if (not c.isdigit()):
flag = c
dotNum += 1
if (dotNum > 1):
return reportErr(9, "")
elif (dotNum == 0):
return ch
else:
ch = ch.replace(flag, " ")
if (flag == '+'):
num1, num2 = map(int, ch.split())
return str(num1 + num2)
elif (flag == '-'):
num1, num2 = map(int, ch.split())
return str(num1 - num2)
elif (flag == '*'):
num1, num2 = map(int, ch.split())
return str(num1 * num2)
elif (flag == '/'):
num1, num2 = map(int, ch.split())
return str(num1 / num2)
elif (flag == '\\'):
num1, num2 = map(int, ch.split())
return str(num1 // num2) + " R = " + str(num1 % num2)
elif (flag == '^'):
num1, num2 = map(int, ch.split())
ans = 1
for i in range(0, num2, 1):
ans *= num1
return str(ans)
else:
return reportErr(8, flag)
#代码段
class Ans (StdAns):
def GETMSG(self):
ans = "汝以为咱不会算算数的嘛!"
for s in self.parms:
if (s == "help"):
ans = "你喂给我式子我算呀,还不会问狸吧~"
break
elif (s != "bc"):
ans += "\n" + s + " ==> " + bcMain(s)
if (len(self.parms) < 2):
ans = "召唤我有什么事咩?有啥不懂输 help 哦~"
elif (self.parms[1] == "help"):
ans = "你喂给我式子我算呀,我只会四则运算哦,还不会就问狸吧~\n"
ans += "如果输入fun会进入函数模式哦funhelp可以查看支持的函数嘿嘿"
elif (self.parms[1] == "funhelp"):
ans = "fun支持这些哦:\n"
ans += " + ==> 大整数加法\n"
ans += " - ==> 大整数减法\n"
ans += " * ==> 大整数乘法\n"
ans += " / ==> 大整数除法\n"
ans += " \\ ==> 大整数取余除法\n"
ans += " ^ ==> 整数幂\n"
ans += " ! ==> 阶乘\n"
ans += "sqrt() ==> 开平方\n"
ans += "别搞太大的数哦,服务器爆了打你哦~"
else:
if (self.parms[1] == "fun"):
funMod = True
else:
funMod = False
for s in self.parms:
if (s != "bc" and s != "help" and s != "fun" and s != "funhelp"):
if (funMod):
ans += "\n" + s + " ==> " + funMain(s)
else:
ans += "\n" + s + " ==> " + bcMain(s)
return ans