开发一个简单的python计算器
1、实现加减乘除及拓号优先级解析
2、用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-25/3 + 7 /399/42998 +10 * 568/14 )) - (-43)/ (16-32) )等类似公式后,必须自己解析里面的(),+,-,,/符号和公式(不能调用eval等类似功能偷懒实现),运算后得出结果,结果必须与真实的计算器所得出的结果一致
如果你对python感兴趣,我这有个学习Python基地,里面有很多学习资料,感兴趣的+Q群:688244617
1 #!usr/bin/evn python
2 #coding:cp936
3 #Author:Siwen
4
5
6 print ("Hello world!")
7
8
9 import re
10
11 def handle_special_occactions(plus_minus,multiply_and_dividend):
12 #有时会出现这种情况 , ['-', '-'] ['1 ', ' 2 * ', '14969036.7968254'],2*...后面这段实际是 2*-14969036.7968254,需要特别处理下,太恶心了'''
13 for index,i in enumerate(multiply_and_dividend):
14 i = i.strip()
15 if i.endswith("*") or i.endswith("/"):
16 multiply_and_dividend[index] = multiply_and_dividend[index] + plus_minus[index] + multiply_and_dividend[index+1]
17 del multiply_and_dividend[index+1]
18 del plus_minus[index]
19 return plus_minus,multiply_and_dividend
20
21 def remove_jia(formula):
22 formula = formula.replace("++","+")
23 formula = formula.replace("+-","-")
24 formula = formula.replace("-+","-")
25 formula = formula.replace("--","+")
26 formula = formula.replace("- -","+")
27 return formula
28
29
30 def compute_mutiply_and_dividend(formula):
31 operators = re.findall("[*/]", formula )
32 calc_list = re.split("[*/]", formula )
33 t_res = None
34 for index,item in enumerate(calc_list):
35 item=item.strip()
36 if t_res: #代表不是第一次循环
37 if operators[index-1] == '/':
38 t_res /=float(item)
39 if operators[index-1] == '*':
40 t_res *=float(item)
41 else:
42 t_res = float(item)
43 print("乘除结果为:",t_res)
44 return(t_res)
45
46 def compute(formula):
47 #计算不带括号的算式
48 formula=formula.strip('()')
49 formula=remove_jia(formula)
50 plus_minus=re.findall(r'[+-]',formula)
51 multiply_and_dividend = re.split("[+-]", formula)#去除乘除的公式
52 if len(multiply_and_dividend[0].strip()) == 0:#代表这肯定是个负号
53 multiply_and_dividend[1] = plus_minus[0] + multiply_and_dividend[1]
54 del multiply_and_dividend[0]
55 del plus_minus[0]
56
57 plus_minus,multiply_and_dividend=handle_special_occactions(plus_minus,multiply_and_dividend)
58
59 for index,i in enumerate(multiply_and_dividend):
60 if re.search("[*/]" ,i):
61 sub_res = compute_mutiply_and_dividend(i)
62 multiply_and_dividend[index] =sub_res
63
64 print(multiply_and_dividend, plus_minus)
65 total_res = None
66 for index,item in enumerate(multiply_and_dividend):
67
68 if total_res: #代表不是第一次循环
69
70 if plus_minus[index-1] == '+':
71 total_res +=float(item)
72 if plus_minus[index-1] == '-':
73 total_res -=float(item)
74
75 else:
76 total_res = float(item)
77 print('算式%s计算结果为:'%formula,total_res)
78 return(total_res)
79
80
81
82 def cale(formula):
83 #主要逻辑,先算括号里的值,再算乘除,再算加减
84 parenthesise_flag=True
85 sub=None
86 sub_res=0
87 while parenthesise_flag:
88 m=re.search('\([^()]*\)',formula)
89
90 if m:
91 m=m.group()
92 print (m)
93 #print('先算括号里',m)
94 sub_res=compute(m)
95 print (sub_res)
96 formula=formula.replace(m,str(sub_res))
97 else:
98 print('没有括号了')
99 #formula=formula.replace(m.group(),str(sub_res))
100 print(formula)
101 sub=compute(formula)
102 # print '计算结果为:',sub
103 parenthesise_flag=False#算式中的括号都已经被剥除了
104 res="1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))"
105 #res='1+2*(3+4)'
106 res=cale(res)
107
108
运行结果: