简易计算器
2018-05-23 10:47 钱先生 阅读(225) 评论(0) 编辑 收藏 举报作业要求:
本节作业
开发一个简单的python计算器
- 实现加减乘除及拓号优先级解析
- 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式(不能调用eval等类似功能偷懒实现),运算后得出结果,结果必须与真实的计算器所得出的结果一致
hint:
re.search(r'\([^()]+\)',s).group()
'(-40/5)'
代码:
#!/usr/bin/python # -*- coding: utf-8 -*- import re # 加减运算 def add_subtract(expression): expression = expression.replace("+-", "-") expression = expression.replace("++", "+") expression = expression.replace("-+", "-") expression = expression.replace("--", "+") # 匹配加减号 temp = re.compile('[\-]?\d+\.*\d*[\+\-]{1}\d+\.*\d*') flag = temp.search(expression) # 如果加减算式不存在, i.e. 加减计算已完成,返回计算式 if not flag: return expression data = flag.group() if len(data.split("+")) > 1: part1, part2 = data.split("+") value = float(part1)+float(part2) elif data.startswith("-"): part1, part2, part3 = data.split("-") value = -float(part2)-float(part3) else: part1, part2 = data.split("-") value = float(part1)-float(part2) s1, s2 = temp.split(expression, 1) new_expression = "%s%s%s" % (s1, value, s2) return add_subtract(new_expression) # 乘除运算 def mul_divide(expression): expression = expression.replace(" ", "") # 匹配乘除号 temp = re.compile('\d+\.*\d*[\*\/]+[\+\-]?\d+\.*\d*') flag = temp.search(expression) # 如果乘除算式不存在, i.e. 乘除计算已完成,返回计算式 if not flag: return expression data = flag.group() if len(data.split("/")) > 1: part1, part2 = data.split("/") if float(part2) == 0: exit("denominator is 0") value = float(part1)/float(part2) else: part1, part2 = data.split("*") value = float(part1)*float(part2) s1, s2 = temp.split(expression, 1) new_expression = "%s%s%s" % (s1, value, s2) return mul_divide(new_expression) # 去掉括号 def rem_brackets(expression): temp = re.compile(r'\([^()]+\)') flag = temp.search(expression) if not flag: r1 = mul_divide(expression) r2 = add_subtract(r1) return r2 sub = flag.group().strip('[()]') temp_result1 = mul_divide(sub) temp_result2 = add_subtract(temp_result1) s1, s2 = temp.split(expression, 1) new_expression = '%s%s%s' % (s1, temp_result2, s2) return rem_brackets(new_expression) def main(): exit_flag = False while not exit_flag: print("系统支持的计算式格式".center(20, "*")) print("\r") msg = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )' print(msg) print("\r") print("The end".center(27, "*")) user_exp = input("\r\n\r\n请输入计算式(限加减乘除), 输入quit退出: ") expression = user_exp.strip() if expression == "quit": exit_flag = True else: compute1 = rem_brackets(expression) compute2 = eval(expression) print("Result of expression:\033[31;1m%s\033[0m" % compute1) if float(compute1) == float(compute2): print("计算结果正确") print("\r") else: print("\r\n计算结果错误".center(20, "*")) print("\r")
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步