From 76c70a3f13e03e983ed15c244cf70b9ae2e38bb1 Mon Sep 17 00:00:00 2001 From: weilinfox Date: Sat, 7 Mar 2020 09:08:59 +0800 Subject: [PATCH 1/4] bc add sys mode --- worker/bc.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/worker/bc.py b/worker/bc.py index 1228be8..705dd4e 100644 --- a/worker/bc.py +++ b/worker/bc.py @@ -1,9 +1,11 @@ # by weilinfox~ # 支持四则运算,括号和浮点数,可以省略部分乘号 # 支持语法检查 +# sys 模式只支持 Linux from zzcore import StdAns import math +import subprocess divError = False overflowError = False @@ -361,6 +363,78 @@ def funMain (ch): else: return reportErr(8, flag) +def sysbc (arg): + try: + arg = "echo \"" + arg + "\" | bc" + ret = subprocess.run(arg, shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, check = True, timeout = 2) + retmes = str(ret.stdout) + retmes = retmes[2:len(retmes)-3] + retmes = retmes.replace('\\n', ". ") + retmes = retmes.replace('\\', "") + return retmes + except subprocess.TimeoutExpired: + return "你干了啥怎么半天算不完啦!服务器爆了打你哦~" + except: + return "啊呀呀好像出错了,一定是 SiHuan 的服务器爆了" + +def sysMain (lst): + scaleFlag = False + ibaseFlag = False + obaseFlag = False + scaleSet = False + ibaseSet = False + obaseSet = False + scaleN = 0 + ibaseN = 0 + obaseN = 0 + ans = "" + try: + for s in lst: + if (s == "bc" or s == "sys"): + continue; + elif (s == "ibase"): + ibaseFlag = True + elif (s == "obase"): + obaseFlag = True + elif (s == "scale"): + scaleFlag = True + else: + dig = True + for c in s: + if (not s.isdigit()): + dig = False + break + if (ibaseFlag or obaseFlag or scaleFlag): + if (dig): + if (ibaseFlag): + ibaseSet = True + ibaseFlag = False + ibaseN = int(s) + elif (obaseFlag): + obaseSet = True + obaseFlag = False + obaseN = int(s) + elif (scaleFlag): + scaleSet = True + scaleFlag = False + scaleN = int(s) + else: + ans += "\n" + s + " ==> 这里好像有问题哎小老弟~" + return ans + else: + opt = "" + if (scaleSet): + opt += "scale=" + str(scaleN) + ";" + if (ibaseSet): + opt += "ibase=" + str(ibaseN) + ";" + if (obaseSet): + opt += "obase=" + str(obaseN) + ";" + opt += s + ans += "\n" + opt + " ==> " + sysbc(opt) + return ans + except: + return ans + "\n啊嘞嘞怎么肥四,一定是 SiHuan 的服务器爆了" + #代码段 class Ans (StdAns): @@ -370,7 +444,8 @@ class Ans (StdAns): ans = "召唤我有什么事咩?有啥不懂输 help 哦~" elif (self.parms[1] == "help"): ans = "你喂给我式子我算呀,我只会四则运算哦,还不会就问狸吧~\n" - ans += "如果输入fun会进入函数模式哦,funhelp可以查看支持的函数嘿嘿" + ans += "如果输入fun会进入函数模式哦,funhelp可以查看支持的函数嘿嘿\n" + ans += "输入sys允许我开挂哦,syshelp能查看开挂的方法~" elif (self.parms[1] == "funhelp"): ans = "fun支持这些哦:\n" ans += " + ==> 大整数加法\n" @@ -382,6 +457,15 @@ class Ans (StdAns): ans += " ! ==> 阶乘\n" ans += "sqrt() ==> 开平方\n" ans += "别搞太大的数哦,服务器爆了打你哦~" + elif (self.parms[1] == "sys"): + ans += sysMain(self.parms) + elif (self.parms[1] == "syshelp"): + ans = "sys可是允许我开挂哦~\n" + ans += "我还支持三个指令哦:\n" + ans += "scale ==> 指定保留小数位数\n" + ans += "ibase ==> 指定输入进制\n" + ans += "obase ==> 指定输出进制\n" + ans += "别搞太大的数哦,服务器爆了打你哦~" else: if (self.parms[1] == "fun"): funMod = True From 212d47b93f2a432442ad7a5af97e329b7a27a4fe Mon Sep 17 00:00:00 2001 From: weilinfox Date: Sat, 7 Mar 2020 18:45:44 +0800 Subject: [PATCH 2/4] fix bc bug --- worker/bc.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/worker/bc.py b/worker/bc.py index 705dd4e..f20b9ab 100644 --- a/worker/bc.py +++ b/worker/bc.py @@ -365,13 +365,10 @@ def funMain (ch): def sysbc (arg): try: - arg = "echo \"" + arg + "\" | bc" - ret = subprocess.run(arg, shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, check = True, timeout = 2) - retmes = str(ret.stdout) - retmes = retmes[2:len(retmes)-3] - retmes = retmes.replace('\\n', ". ") - retmes = retmes.replace('\\', "") - return retmes + arg = arg.replace("\'", "") + arg = "echo \'\'\'" + arg + "\'\'\' | bc" + ret = subprocess.run(arg, shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, check = True, timeout = 1) + return bytes.decode(ret.stdout).replace("\n", "") except subprocess.TimeoutExpired: return "你干了啥怎么半天算不完啦!服务器爆了打你哦~" except: From 0997d2763b39a2933ea6b7c60573e369cb60b843 Mon Sep 17 00:00:00 2001 From: weilinfox Date: Wed, 11 Mar 2020 18:16:42 +0800 Subject: [PATCH 3/4] add sys mode for bc --- worker/bc.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/worker/bc.py b/worker/bc.py index f20b9ab..bf5f3ec 100644 --- a/worker/bc.py +++ b/worker/bc.py @@ -366,7 +366,8 @@ def funMain (ch): def sysbc (arg): try: arg = arg.replace("\'", "") - arg = "echo \'\'\'" + arg + "\'\'\' | bc" + arg = "echo '''" + arg + "''' | bc" + #print(arg) ret = subprocess.run(arg, shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, check = True, timeout = 1) return bytes.decode(ret.stdout).replace("\n", "") except subprocess.TimeoutExpired: From 7dbbbd0b389ef8c3be816aabb88c9aeeac9aefea Mon Sep 17 00:00:00 2001 From: weilinfox <54651938+weilinfox@users.noreply.github.com> Date: Thu, 12 Mar 2020 21:20:55 +0800 Subject: [PATCH 4/4] Update bc.py --- worker/bc.py | 1 + 1 file changed, 1 insertion(+) diff --git a/worker/bc.py b/worker/bc.py index bf5f3ec..a00e0f9 100644 --- a/worker/bc.py +++ b/worker/bc.py @@ -436,6 +436,7 @@ def sysMain (lst): #代码段 class Ans (StdAns): + AllowGroup = [959613860] def GETMSG(self): ans = "汝以为咱不会算算数的嘛!" if (len(self.parms) < 2):