1 # -*- coding:utf-8 -*-
2 import re
3
4 # expression='1-2*((60+2*(-3-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))'
5 # expression='1 -2*10*(-9-2*5/3+7/3*99/4*2998+10*568/14)-(-4*3)/(16-3*2)'
6 # content=re.search('(([-+*/]*d+.?d*)+)',expression).group() #(-3-40.0/5)
7
8 def add_sub(bracket_content_after_md): #加减法
9 print("处理加法运算: %s" %bracket_content_after_md) #-3-8.0
10
11 if re.search('(d+.?d*)((+-)|(--))(d+.?d*)',bracket_content_after_md): #提取+- -- 表达式
12 first_add_sub = re.search('(d+.?d*)((+-)|(--))(d+.?d*)', bracket_content_after_md).group() # 提取加减法表达式
13 # print(first_add_sub) #60+-3818009.3809523815
14
15 #加减为减,减减为加
16
17 if re.search('+',first_add_sub): #如果有+ 那么就是+-,就可以转换为-
18 first_add_sub = first_add_sub.replace('+','')
19 # print(first_add_sub)
20
21 elif re.search('--',first_add_sub): #如果为--,则转换为+
22 first_add_sub = first_add_sub.replace('--', '+')
23 # print(first_add_sub)
24
25 first_add_sub_dig = re.split('+|-', first_add_sub) # 将数字和+ - 号分离
26 # print(first_add_sub_dig) #['3', '8.0']
27
28 if bracket_content_after_md[0] == '-': # 加个负号判断
29 first_dig = -float(first_add_sub_dig[0]) # 如果第一位是负号,这将第一个数字变为负数
30 bracket_content_after_md = bracket_content_after_md[1:] # 将这个负号去掉,因为已经变成负数了
31 # print(bracket_content_after_md)
32 else:
33 first_dig = float(first_add_sub_dig[0])
34 # print(first_dig) #3.0
35 second_dig = float(first_add_sub_dig[1])
36 # print(second_dig) #8.0
37 first_add_sub_sym = re.findall('[+-]', first_add_sub) # 提取运算符号
38 # print(first_add_sub_sym) #['-']
39
40 if '-' in first_add_sub_sym: # 判断运算符并进行真实运算
41 result_add_sub = first_dig - second_dig
42 # print(result_add_sub,print(type(result_add_sub)))
43 result_add_sub_str = str(result_add_sub) # 将结果转化为字符串
44 # print(result_add_sub_str,type(result_add_sub_str)) #-5.0 <class 'str'>
45 else: # 加法进行下面的操作和上面除法操作一样
46 result_add_sub = first_dig + second_dig
47 # print(result_add_sub,print(type(result_add_sub)))
48 result_add_sub_str = str(result_add_sub) # 将结果转化为字符串
49 # print(result_add_sub_str, type(result_add_sub_str)) # -5.0 <class 'str'>
50
51 # print(bracket_content_after_md,type(bracket_content_after_md),first_add_sub,type(first_add_sub),type(result_add_sub_str))
52 #60+-3818009.3809523815 <class 'str'> 60-3818009.3809523815 <class 'str'> <class 'str'>
53 #下面要进行替换操作
54
55 bracket_content_after_md = result_add_sub_str
56 # print(bracket_content_after_md,result_add_sub_str)
57 return add_sub(bracket_content_after_md)
58
59 elif re.search('(d+.?d*)[+-](d+.?d*)', bracket_content_after_md):
60 first_add_sub = re.search('(d+.?d*)[+-](d+.?d*)', bracket_content_after_md).group() #提取加减法表达式
61 # print(first_add_sub,type(first_add_sub)) #3-8.0 <class 'str'>
62 first_add_sub_dig = re.split('+|-', first_add_sub) #将数字和+ - 号分离
63 # print(first_add_sub_dig) #['3', '8.0']
64
65 if bracket_content_after_md[0] == '-': #加个负号判断
66 first_dig = -float(first_add_sub_dig[0]) #如果第一位是负号,这将第一个数字变为负数
67 bracket_content_after_md = bracket_content_after_md[1:] #将这个负号去掉,因为已经变成负数了
68 # print(bracket_content_after_md)
69 else:
70 first_dig = float(first_add_sub_dig[0])
71 # print(first_dig) #3.0
72 second_dig = float(first_add_sub_dig[1])
73 # print(second_dig) #8.0
74 first_add_sub_sym = re.findall('[+-]',first_add_sub) #提取运算符号
75 # print(first_add_sub_sym) #['-']
76
77 if '-' in first_add_sub_sym: #判断运算符并进行真实运算
78 result_add_sub = first_dig - second_dig
79 # print(result_add_sub,print(type(result_add_sub)))
80 result_add_sub_str = str(result_add_sub) # 将结果转化为字符串
81 # print(result_add_sub_str,type(result_add_sub_str)) #-5.0 <class 'str'>
82 else: #加法进行下面的操作和上面除法操作一样
83 result_add_sub = first_dig + second_dig
84 # print(result_add_sub,print(type(result_add_sub)))
85 result_add_sub_str = str(result_add_sub) # 将结果转化为字符串
86 # print(result_add_sub_str, type(result_add_sub_str)) # -5.0 <class 'str'>
87
88 bracket_content_after_as = bracket_content_after_md.replace(first_add_sub, result_add_sub_str) #替换运算结果
89
90 bracket_content_after_md = bracket_content_after_as
91
92 return add_sub(bracket_content_after_md) ##迭代处理加减
93
94 else:
95 return bracket_content_after_md ##迭代处理加减
96
97 def mul_div(bracket_content): #乘除法--处理括号内所有乘除法
98 print("处理乘法运算: %s" %bracket_content) #-3-40.0/5
99
100 if re.search('(d+.?d*)((*-)|(/-))(d+.?d*)',bracket_content): #提取并处理*-,/-,
101 first_mul_div = re.search('(d+.?d*)((*-)|(/-))(d+.?d*)', bracket_content).group()
102 first_mul_div_dig = re.split('((*-)|(/-))', first_mul_div)
103 # print(first_mul_div) #2*-11.0
104 first_mul_div_dig = re.split('*|/', first_mul_div) #提取乘法运算的两个数字
105 # print(first_mul_div_dig) #['2', '-11.0']
106 first_dig = float(first_mul_div_dig[0]) # 提取第一个数字并转化格式
107 # print(first_dig) #2.0
108 second_dig = float(first_mul_div_dig[1]) # 提取第二个数字并转化格式
109 # print(second_dig)
110 first_mul_div_sym = re.findall('[*/]', first_mul_div) # 提取运算符号
111 # print(first_mul_div_sym)
112 if '/' in first_mul_div_sym: #判断运算符并进行真实运算
113 result_mul_dig = first_dig / second_dig
114 # print(result_mul_dig,print(type(result_mul_dig)))
115 result_mul_dig_str = str(result_mul_dig) #将结果转化为字符串
116 # print(result_mul_dig_str,type(result_mul_dig_str))
117 else: #乘法进行下面的操作和上面除法操作一样
118 result_mul_dig = first_dig * second_dig
119 # print(result_mul_dig)
120 result_mul_dig_str = str(result_mul_dig)
121 # print(result_mul_dig_str, type(result_mul_dig_str))
122
123 bracket_content_after_md = bracket_content.replace(first_mul_div,result_mul_dig_str) #替换运算结果
124 # print(bracket_content_after_md) #-3-8.0
125
126 bracket_content = bracket_content_after_md
127 # print(bracket_content)
128
129 return mul_div(bracket_content) #迭代处理乘除
130
131 elif re.search('(d+.?d*)[*/](d+.?d*)',bracket_content):
132 first_mul_div = re.search('(d+.?d*)[*/](d+.?d*)',bracket_content).group() #提取第一个带有次乘除符号表达式 40.0/5,group将对象转为字符串
133 # print(first_mul_div,print(type(first_mul_div))) #40.0/5
134
135 first_mul_div_dig = re.split('*|/',first_mul_div) #提取乘法运算的两个数字
136 # print(first_mul_div_dig) #['40.0', '5']
137 first_dig = float(first_mul_div_dig[0]) #提取第一个数字并转化格式
138 # print(first_dig,type(first_dig)) #40.0 <class 'float'>
139 second_dig = float(first_mul_div_dig[1])
140 # print(second_dig, type(second_dig)) #5.0 <class 'float'>
141
142 first_mul_div_sym = re.findall('[*/]',first_mul_div) #提取运算符号
143 # print(first_mul_div_sym,type(first_mul_div_sym[0])) #['/']
144
145 if '/' in first_mul_div_sym: #判断运算符并进行真实运算
146 result_mul_dig = first_dig / second_dig
147 # print(result_mul_dig,print(type(result_mul_dig)))
148 result_mul_dig_str = str(result_mul_dig) #将结果转化为字符串
149 # print(result_mul_dig_str,type(result_mul_dig_str))
150 else: #乘法进行下面的操作和上面除法操作一样
151 result_mul_dig = first_dig * second_dig
152 # print(result_mul_dig)
153 result_mul_dig_str = str(result_mul_dig)
154 # print(result_mul_dig_str, type(result_mul_dig_str))
155
156 bracket_content_after_md = bracket_content.replace(first_mul_div,result_mul_dig_str) #替换运算结果
157 # print(bracket_content_after_md) #-3-8.0
158
159 bracket_content = bracket_content_after_md
160 # print(bracket_content)
161
162 return mul_div(bracket_content) #迭代处理乘除
163
164 else: #返回处理乘除后结果
165 return bracket_content
166
167 # add_sub(bracket_content_after_md) #调用加减法
168
169 def deep_bracket(expression): #处理小括号
170 if re.search('(([-+*/]*d+.?d*)+)',expression): #有小括号则提取并处理
171 bracket = re.search('(([-+*/]*d+.?d*)+)',expression).group() #提取第一个小括号
172 # print(bracket) #(-3-40.0/5)
173 bracket_content= bracket.split("(")[1].split(")")[0] #去除括号
174 # print(bracket_content) #-3-40.0/5
175 bracket_content_after_md = mul_div(bracket_content) #调用乘除法函数,先算乘除
176 # print(bracket_content_after_md,type(bracket_content_after_md)) #9-3.3333333333333335+173134.50000000003+405.7142857142857 <class 'str'>
177
178 print('=======================')
179 bracket_content_after_as = add_sub(bracket_content_after_md) #此为第一个小括号的计算结果,调用加减法函数,算加减法
180 # print(bracket_content_after_as) #173527.88095238098
181
182 expression = expression.replace(bracket,bracket_content_after_as)
183 print("表达式为: %s" %expression)
184
185 deep_bracket(expression)
186
187 else: #没有小括号交给乘法和加法处理
188 # mul_div(expression)
189 bracket_content_after_md = mul_div(expression) #先调用乘除函数
190 bracket_content_after_as = add_sub(bracket_content_after_md) #再掉用加减函数
191
192 if __name__ == "__main__":
193 expression = input("请输入表达式>>:")
194 deep_bracket(expression)