[ Python - 6 ] 正则表达式实现计算器功能
2023-09-27 14:20:24 时间
要求:禁止使用eval函数。参考网上代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!_*_coding:utf-8_*_ """用户输入计算表达式,显示计算结果""" import re a = '2+(6/3-2+(1*4))' # 取最内层的括号 bracket = re.compile('\([^()]+\)') # 加法 add = re.compile('(-?\d+\.?\d*\+\d+\.?\d*)|(-?\d+\.?\d*\+-\d+\.?\d*)') # 减法 sub = re.compile('(\d+\.?\d*-\d+\.?\d*)|(\d+\.?\d*--\d+\.?\d*)') # 乘法 mul = re.compile('(\d+\.?\d*\*\d+\.?\d*)|(\d+\.?\d*\*-\d+\.?\d*)') # 除法 div = re.compile('(\d+\.?\d*/-\d+\.?\d*)|(\d+\.?\d*/\d+\.?\d*)') # 检查括号内是否运算完毕 c_f = re.compile('\(?\+?-?\d+\)?') # 去掉括号 strip = re.compile('[^(].*[^)]') def Add(s): '''加法运算''' exp = re.split('\+', add.search(s).group()) return s.replace(add.search(s).group(), str(float(exp[0]) + float(exp[1]))) def Sub(s): '''减法运算''' exp = re.split('-', sub.search(s).group()) return s.replace(sub.search(s).group(), str(float(exp[0]) - float(exp[1]))) def Mul(s): '''乘法运算''' exp = re.split('\*', mul.search(s).group()) return s.replace(mul.search(s).group(), str(float(exp[0]) * float(exp[1]))) def Div(s): '''除法运算''' exp = re.split('/', div.search(s).group()) return s.replace(div.search(s).group(), str(float(exp[0]) / float(exp[1]))) def calc(): while True: s = input('Please input the expression(q for quit):') if s == 'q': break else: s = ''.join([x for x in re.split('\s+',s)]) # 将表达式按空格分割并重组 if not s.startswith('('): # 若用户输入的表达式首尾无括号,则统一格式化为:(表达式) s = str('(%s)' % s) while bracket.search(s): # 若表达式s存在括号 # print('---') s = s.replace('--', '+') # 检查表达式,并将--运算替换为+运算 # 获取最内层的表达式 s_search = bracket.search(s).group() # 将最内层括号及其内容赋给变量s_search # 括号里除法运算 if div.search(s_search): # 若除法运算存在(必须放在乘法之前) s = s.replace(s_search, Div(s_search)) # 执行除法运算并将结果替换原表达式 # 括号里乘法运算 elif mul.search(s_search): # 若乘法运算存在 s = s.replace(s_search, Mul(s_search)) # 执行乘法运算并将结果替换原表达式 # 括号里减法运算 elif sub.search(s_search): # 若减法运算存在(必须放在加法之前) s = s.replace(s_search, Sub(s_search)) # 执行减法运算并将结果替换原表达式 # 括号里加法运算 elif add.search(s_search): # 若加法运算存在 s = s.replace(s_search, Add(s_search)) # 执行加法运算并将结果替换原表达式 elif c_f.search(s_search): # 若括号内无任何运算(类似(-2.32)除外) s = s.replace(s_search, strip.search(s_search).group()) # 将括号脱掉,例:(-2.32)---> -2.32 print(s) # print('The answer is: %.2f' %(float(s))) if __name__ == '__main__': print(eval('1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )')) calc()
相关文章
- 推荐 10 款最好的 Python IDE
- python正则表达式
- [ python ] 正则表达式及re模块
- 【Python开发】Python之re模块 —— 正则表达式操作
- 【Python开发】查看数据类型
- python进程通信-方式01:管道与信号量
- Python 正则表达式(分组)
- 自学Python可以找到合适的工作吗?
- Python面试题之Python正则表达式指南
- Python常见报错 - 运维笔记
- Python物理学高效计算
- Python 正则表达式
- Python 有点意思
- 21世纪编程语言死亡名单,Java、Python、Go等均在劫难逃
- python读写文件字符集
- Python笔记:正则表达式方法
- python的@是什么(函数装饰器)
- python给字段名和值都加上引号
- python核心编程学习记录之正则表达式
- 成功解决:Can‘t find Python executable “python“, you can set the PYTHON env variable.
- 第11.26节 Python正则表达式运算符优先级
- 第11.25节 Python正则表达式编译re.compile及正则对象使用
- 第11.21节 Python 中正则表达式的其他扩展功能
- 第11.20节 Python 中正则表达式的扩展功能:后视断言、后视取反
- 第11.17节 Python 正则表达式扩展功能:命名组功能及组的反向引用
- 第11.13节 Python正则表达式的转义符””功能介绍
- 第11.12节 Python元字符“|”支持的正则表达式多选一匹配模式
- 在windows下MySQLdb/MySQL-python的安装
- python 移动文件和移动目录 统一处理 Python利用递归实现文件的复制
- python 正则表达式 re