加减乘除带括号的计算器代码

代码如下

 1 import re
 2 def form_at(string):
 3     string = string.replace('++','+')
 4     string = string.replace('-+','-')
 5     string = string.replace('+-','-')
 6     string = string.replace('--','+')
 7     string = string.replace('+/','/')
 8     string = string.replace('+*','*')
 9     string = string.replace('*+','*')
10     return  string
11 def chech_expression(string):
12     chech_result = True
13     if not string.count('(') == string.count(')'):
14         print('表达式错误,括号未闭合')
15         chech_result = False
16     if re.findall('[a-z]+',string.lower()):
17         print('表达式错误,含非法字符')
18         chech_result = False
19     return chech_result
20 def calc_mul_div(string):
21     regular = '\d+\.?\d*[*/]\d+\.?\d*'
22     while re.findall(regular,string):
23         expression = re.search(regular,string).group()
24         if expression.count('*')==1:
25             x,y = expression.split('*')
26             mul_result = str(float(x)*float(y))
27             string = string.replace(expression,mul_result)
28             string = format(string)
29         if expression.count('/')==1:
30             x,y=expression.split('/')
31             div_result =str(float(x)/float(y))
32             string = string.replace(expression,div_result)
33             string = format(string)
34     return string
35 def cal_add_sub(string):
36     add_regular = '[\-]?\d+\.?\d*\+[\-]?\d+\.?\d*'
37     sub_regular = '[\-]?\d+\.?\d*\-[\-]?\d+\.?\d*'
38     while re.findall(add_regular,string):
39         add_list = re.findall(add_regular,string)
40         for add_str in add_list:
41             x,y = add_str.split('+')
42             add_result = str(float(x)+float(y))
43             string = string.replace(add_str,add_result)
44             string = format(string)
45     while re.findall(sub_regular,string):
46         sub_list = re.findall(sub_regular,string)
47         for sub_str in sub_list:
48             numbers = sub_str.split('-')
49             if len(numbers)==3:
50                 result = 0
51                 for v in numbers:
52                     if v:
53                         result-=float(v)
54             else :
55                 x,y = numbers
56                 result = float(x)-float(y)
57                 string = string.replace(sub_str,str(result))
58                 string = format(string)
59     return string
60 if __name__ =='__main__':
61 
62     str_ing = input('the :')
63     if chech_expression(str_ing):
64         str_ing = form_at(str_ing)
65         while str_ing.count('(') > 0:
66             str_s = re.search('\([^()]*\)',str_ing).group()
67             replace_str = calc_mul_div(str_s)
68             replace_str = cal_add_sub(replace_str)
69             str_ing = format(str_ing.replace(str_s,replace_str[1:-1]))
70         else:
71             replace_str = calc_mul_div(str_ing)
72             replace_str = cal_add_sub(replace_str)
73             str_ing = str_ing.replace(str_ing,replace_str)
74             print('the result :'+str_ing.replace('+',' '))
View Code

猜你喜欢

转载自www.cnblogs.com/CIBud/p/12041382.html