python作业-计算器
程序练习---实现简单的加、减、乘、除计算器
一、流程图

二、源代码
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 #@author: 时尚梦齐 4 5 import re 6 7 bracket = re.compile(r'\([^()]+\)') # 寻找最内层括号规则 8 add = re.compile(r'-?\d+\.?\d*\+\d+\.?\d*') # 寻找加法运算规则 9 sub = re.compile(r'-?\d+\.?\d*-\d+\.?\d*') # 寻找减法运算规则 10 mul = re.compile(r'\d+\.?\d*\*-?\d+\.?\d*') # 寻找乘法运算规则 11 div = re.compile(r'\d+\.?\d*/-?\d+\.?\d*') # 寻找除法运算规则 12 c_f = re.compile(r'\(?\+?-?\d+\)?') # 检查括号内是否运算完毕规则 13 strip = re.compile(r'[^(].*[^)]') # 脱括号规则 14 has_char = re.compile('[^0-9\-*/+(). ]') # 检查表达式字符规则 15 oper_error=re.compile('\*/|/\*|\*\*|//') # 检查多余乘除运算符规则 16 17 error_code = { 18 'illegal_character':'表达式里面有非法字符', 19 'lack_bracket':'缺少括号或括号没有闭合', 20 'ZeroDivisionError':'除数不能为0', 21 'oper_error':'操作符错误' 22 } 23 24 25 def Add(s): 26 ''' 27 计算加法 28 :param s: 最里面括号内的表达式 29 :return: 计算结果 30 ''' 31 exp = re.split(r'\+' ,add.search(s).group() ) # 1+2 --> [1,2] 32 return s.replace(add.search(s).group(), str(float(exp[0]) + float(exp[1])) ) 33 34 def Sub(s): 35 ''' 36 计算减法 37 :param s: 最里面括号内的表达式 38 :return: 计算结果 39 ''' 40 exp = re.split(r'-', sub.search(s).group() ) # 1-2 --> [1,2] 41 print(exp) 42 if len(exp) == 3: # 里面若是负数减负数,分割后是三个元素 43 return s.replace(sub.search(s).group(), str("-") + str(float(exp[1]) + float(exp[2]))) 44 else: 45 return s.replace(sub.search(s).group(), str(float(exp[0]) - float(exp[1])) ) 46 47 def Mul(s): 48 ''' 49 计算乘法 50 :param s: 最里面括号内的表达式 51 :return: 计算结果 52 ''' 53 exp = re.split(r'\*', mul.search(s).group() ) # 1*2 --> [1,2] 54 return s.replace(mul.search(s).group(), str(float(exp[0]) * float(exp[1])) ) 55 56 def Div(s): 57 ''' 58 计算除法并检测除数为0 59 :param s: 最里面括号内的表达式 60 :return: 计算结果 61 ''' 62 exp = re.split(r'/', div.search(s).group() ) # 1/2 --> [1,2] 63 if float(exp[1]) == 0.0: 64 return 'ZeroDivisionError' 65 else: 66 return s.replace(div.search(s).group(), str(float(exp[0]) / float(exp[1])) ) 67 68 def calc_format(s): 69 ''' 70 格式化运算符 71 :param s: 用户输入的表达式 72 :return: 格式化后的表达式 73 ''' 74 s = s.replace('++', '+') 75 s = s.replace('+-', '-') 76 s = s.replace('--', '+') 77 s = s.replace('-+', '-') 78 s = s.replace('*+', '*') 79 s = s.replace('/+', '/') 80 return s 81 82 def calc_check(s): 83 ''' 84 检查表达式有没有非法字符 85 :param s:用户输入的表达式 86 :return:错误信息 87 ''' 88 if has_char.findall(s): 89 return 'illegal_character' 90 elif len(re.findall(r'\(',s) ) != len(re.findall(r'\)', s) ): 91 return 'lack_bracket' 92 elif oper_error.findall(s): 93 return 'oper_error' 94 else: 95 return False 96 97 98 def run(s): 99 ''' 100 程序计算开始 101 :param s: 用户输入的表达式 102 :return: 错误信息或者计算结果 103 ''' 104 calc_check_res = calc_check(s) # 检查表达式是否正确 105 if calc_check_res: # 若有返回值 106 return calc_check_res # 返回错误信息 107 else: 108 s = s.replace(" ","") # 去除表达式空格 109 print('计算表达式: %s' % s) 110 # s = ''.join([x for x in re.split('\s+', s)]) # 将表达式按空格分割并重组 111 s = str('(%s)' % s) # 给表达式外层加括号 112 while bracket.search(s): # 循环判断表达式是否还有括号 113 s = calc_format(s) # 获取格式化后的表达式 114 s_search = bracket.search(s).group() # 查找表达式最里面的括号 115 if div.search(s_search): # 若除法运算存在(必须放在乘法之前) 116 div_check = Div(s_search) # 检查除数为0 117 if has_char.findall(div_check): # 若有返回值 118 return div_check # 返回错误信息 119 else: 120 s = s.replace(s_search, Div(s_search)) # 执行除法运算并将结果替换原表达式 121 elif mul.search(s_search): # 若乘法运算存在 122 s = s.replace(s_search, Mul(s_search)) # 执行乘法运算并将结果替换原表达式 123 elif sub.search(s_search): # 若减法运算存在(必须放在加法之前) 124 s = s.replace(s_search, Sub(s_search)) # 执行减法运算并将结果替换原表达式 125 elif add.search(s_search): # 若加法运算存在 126 s = s.replace(s_search, Add(s_search)) # 执行加法运算并将结果替换原表达式 127 elif c_f.search(s_search): # 若括号内无任何运算(类似(-2.32)除外) 128 s = s.replace(s_search,strip.search(s_search).group()) # 将括号脱掉,例:(-2.32)---> -2.32 129 130 return s # 返回计算结果 131 132 133 if __name__ == '__main__': 134 ''' 135 主程序入口 136 ''' 137 while True: 138 inp = input('请输入要计算的表达式或"q"退出程序:') 139 if inp == 'q': # 用户输入q退出程序 140 break 141 elif inp == '' or inp.isspace(): # 判断用户输入空格 142 continue 143 else: 144 res = run(inp) # 获取run函数返回结果 145 if res in error_code: # 若返回值在error_code里面 146 print(error_code[res]) # 打印error_code的value 147 else: 148 print('表达式计算结果为: %s' % res) 149 150 print('欢迎下次使用...')
浙公网安备 33010602011771号