python 一个可耻的计算器,百行以内
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 一个可耻的计算器,百行以内相关的知识,希望对你有一定的参考价值。
1 面对这些代码是压缩的,,但是很low这些代码,不过功能还是实现。
2 import re
3 def add(*args): #加法运算
4 res = 0
5 for i in args:
6 res += float(i)
7 return res
8 def mult(*args): #乘法运算
9 res = 1
10 for i in args:
11 res = res * i
12 return res
13 def divod(finum,*args): #除法运算
14 for i in args:
15 res = float(finum)/float(i)
16 return res
17 def subd(firnu,*args): #减法运算
18 res = firnu
19 for i in args:
20 res = res -i
21 return res
22 def filterfuter(*args): #特殊运算符处理
23 for i in args:
24 i= i.replace(‘+-‘, ‘-‘).strip()
25 i = i.replace(‘--‘, ‘+‘).strip()
26 return i
27 def funct(rs): #入口函数
28 sur = re.sub(‘\s+‘, ‘‘, rs)
29 tmp1 = re.findall("\([^()]+\)", sur)[0]
30 tmp = re.sub("[()]", ‘‘, tmp1)
31 x, y = tmp.split("/",1)
32 if (re.sub(tmp1, str(divod(x, y)), sur, 1)):
33 return (re.sub(tmp1, str(divod(x, y)), sur, 1))
34 rs = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ) ) - (-4*3)/ (16-3*2) )"
35 sur = re.sub(‘\s+‘, ‘‘, funct(rs))
36 tmp1 = re.findall("\([^()]+\)", sur)[1]
37 a, b,d,*_,z = tmp1.split("*",)
38 tmp = re.sub("[()]", ‘‘, tmp1)
39 d1,d2=str(d).split("/")
40 z1,z2 = str(re.sub(‘[)]‘,"",z)).split("/")
41 sur= re.sub(str(d), str(divod(d1,d2)), sur, 1)
42 sur= re.sub(str(re.sub(‘[)]‘,"",z)), str(divod(z1,z2)), sur, 1)
43 sur = re.sub(‘\s+‘, ‘‘, sur)
44 tmp1 = re.findall("\([^()]+\)", sur)[1]
45 a, b,d,*_,z = tmp1.split("*",)
46 tmp = re.sub("[()]", ‘‘, tmp1)
47 tm1,tm2 =re.findall("(\d/\d)",tmp)
48 x,y = tm1.split("/")
49 x1,y1=tm2.split("/")
50 sur= re.sub(str(tm2), str(divod(x1,y1)), re.sub(str(tm1), str(divod(x,y)), sur, 1), 1)
51 sur = re.sub(‘\s+‘, ‘‘, sur)
52 tmp1,tmp2,tmp3 = [re.findall("\(+[^()]+\)", sur)[1],re.findall("\(+[^()]+\)", sur)[2],re.findall("\(+[^()]+\)", sur)[3]]
53 tmp2=re.sub(‘[()]‘,"",tmp2)
54 ta,tb = re.split(‘\*‘,tmp2)
55 ta1,ta2 = re.split(‘\-‘,tmp3)
56 ta2=re.sub(‘[()]‘,"",ta2)
57 taa,tab = re.split(‘\*‘,ta2)
58 ta = re.sub(‘\-‘,"",ta)
59 tmp1 =re.findall(r"\-(.*)\+(.*)\+(.*)",tmp1)
60 t1,t2 = re.split("\*",str(tmp1[0][0]))
61 t3,t4,t5=re.split("\*",str(tmp1[0][1]))
62 t6,t7=re.split("\*",str(tmp1[0][2],))
63 t7 = re.sub(‘\)‘,"",t7)
64 sur = re.sub(str(taa+"\*"+tab),str(mult(int(taa),int(tab))), 65 re.sub(str(ta+"\*"+tb),str(mult(int(ta),int(tb))), 66 re.sub(str(t6+"\*"+t7),str(mult(float(t6),float(t7))), 67 re.sub(str(t3+"\*"+t4+"\*"+t5),str(mult(float(t3),float(t4),float(t5))), 68 re.sub(str(t1+"\*"+t2),str(mult(float(t1),float(t2))),sur,1),1),1),1),1)
69 #1-2*((60-30+(-8)*(9-3.3333333333333335+173134.50000000003+405.71428571428567))-(-12)/(16-6))
70 t1,t2 = re.split("\-",re.sub(‘[()]‘,"",re.findall("\(+[^()]+\)", sur)[3]))
71 sur = re.sub(str(t1+"\-"+t2),str(subd(int(t1),int(t2))),sur,1)
72 tmp1,tmp2,tmp3,tmp4 = re.findall("\(+[^()]+\)", sur)
73 tmp3 ,tmp4= [re.sub(‘[()]‘,"",tmp3),re.sub(‘[()]‘,"",tmp4)]
74 sur = re.sub("\("+tmp3+"\)/\("+tmp4+"\)",str(tmp3)+"/"+str(tmp4), 75 re.sub(str(tmp3+"\/"+tmp4),str(divod(float(tmp3),float(tmp4))),sur,1))
76 tm1,*_,tm5,tm6,tm7,tm8,tm9= re.findall("[^*(\/)]+",sur)
77 sur = re.sub(str(tmp3+"/"+tmp4),"-"+str(divod(float(filterfuter(tm8)),float(tm9))),sur)
78 t1,t2,t3,t4 = re.split(‘[\+\-]‘,tm7)
79 tk = str(subd(float(t1),float(t2)))
80 sur = re.sub(str(tk+"\+"+t3+"\+"+t4),str(add(float(tk),float(t3),float(t4))), 81 re.sub(t1+"\-"+t2,str(subd(float(t1),float(t2))),sur))
82 tm1,*_,tm3,tm4,tm5,tm6,tm7,tm8 = re.findall("[^\(\/\-)]+",sur)
83 tm4 = re.sub(‘\+‘,"",tm4)
84 sur = re.sub(str(tm3+"\-"+tm4),str(subd(float(tm3),float(tm4))),sur,1)
85 tmp = re.findall("[^\(\/\+\*)]+",sur)
86 tmp[2]= re.sub("-","",tmp[2])
87 sur = re.sub("\+([^\d])","",re.sub(tmp[2]+"\)\*\(" + tmp[3]+‘\)‘ 88 ,str(mult(float(tmp[2]),float(tmp[3]))),sur,1))#1-2*((30.0+(-1388352.0)--1.2
89 sur = filterfuter(sur) #1-2*((30.0-1388367.0476190478)+1.2) #2776670.695238094
90 tmp = re.findall("\([^()]+\)", sur)[0]#1-2*((30.0-1388367.0476190478)+1.2)
91 t1,t2 = re.sub("[()]","",tmp).split("-")
92 sur=tmp = re.sub("[\(]","",re.sub("[\)]","", 93 re.sub(t1+"\-"+t2,str(subd(float(t1),float(t2))),sur),1),1)
94 #1-2*(-1388337.0476190478+1.2)
95 tmp = re.findall(‘\((.*)\+(.*)\)‘,sur)
96 sur = re.sub(tmp[0][0]+"\+"+tmp[0][1],str(add(tmp[0][0],tmp[0][1])),sur)
97 t1,t2,t3 = re.findall("[^\-\*\()]+",sur)
98 t3 = re.sub(‘-‘,‘‘,t3)
99 t1,t2 = filterfuter(re.sub(t2+"\*\(-"+t3+"\)","-"+str(mult(float(t3),float(t2))),sur)).split(‘+‘)
100 print("这个表达式 %s \n的计算结果:%s"%(rs,add(float(t1),float(t2))))
101 #2776672.6952380957
102 # print(eval(" 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ) ) - (-4*3)/ (16-3*2) ) "))
以上是关于python 一个可耻的计算器,百行以内的主要内容,如果未能解决你的问题,请参考以下文章