Python3.6基于正则实现的计算器示例【无优化简单注释版】
作者:Tyran_U 时间:2023-07-19 05:29:25
本文实例讲述了Python3.6基于正则实现的计算器。分享给大家供大家参考,具体如下:
# -*- coding:utf-8 -*-
#!python3
import re
import copy
def my_calc(inside):
"""
计算括号内的算术式
:param inside:算术式
:return:结果
"""
while True:
# 1、首先需要把含有优先级最高的*和/找出来
# 这里有几种情况,(1*1) (1*-1) (-1*1)除法类似(暂时不考虑分母为0的情况)
# 但是有了正则就方便多了
search_ret = re.search('(\(-)?\d+(\.\d+)?[*/]-?\d+(\.\d+)?', inside)
if search_ret is None:
break
ret_str = search_ret.group()
if '(' in ret_str:
ret_str = ret_str[1:]
num_list = re.split('[*/]', ret_str)
operator = re.search('[*/]', ret_str).group()
calc_ret = 0
if operator == '*':
calc_ret = float(num_list[0]) * float(num_list[1])
elif operator == '/':
calc_ret = float(num_list[0]) / float(num_list[1])
inside = inside.replace(ret_str, str(calc_ret))
# */都运算完以后就可以做+-了
while True:
# 2、把含有+-的算术式找出来
search_ret = re.search('(\(-)?\d+(\.\d+)?[+\-]-?\d+(\.\d+)?', inside)
if search_ret is None:
break
ret_str = search_ret.group()
if '(' in ret_str:
ret_str = ret_str[1:]
tmp_str = copy.copy(ret_str)
num_1 = re.match('-?\d+(\.\d+)?', tmp_str).group()
tmp_str = tmp_str.replace(num_1, '')
operator = tmp_str[0]
num_2 = tmp_str[1:]
calc_ret = 0
if operator == '+':
calc_ret = float(num_1) + float(num_2)
elif operator == '-':
calc_ret = float(num_1) - float(num_2)
inside = inside.replace(ret_str, str(calc_ret))
return re.sub('[()]', '', inside)
def format_str(s):
s = s.replace('--', '+')
s = s.replace('-+', '-')
s = s.replace('++', '+')
s = s.replace('+-', '-')
if s[0] == '+':
s = s[1:]
s = '('+s+')'
return s
def un_bracket_calc(final_str): # -1*2+3-4/-5
final_str = format_str(final_str)
final_str = my_calc(final_str)
return final_str
def my_math(s): # "((-1-2*-3)/(3-2)+(9*5-89)*(2*3*(3-0)))"
while True:
inside_bracket = re.search('[()]+[()]+', s)
if inside_bracket is None:
# 括号都算完了,如果还有算术式继续运算
s = un_bracket_calc(s)
break
src_str = inside_bracket.group()
ret = my_calc(src_str)
s = s.replace(src_str, ret)
return s
s_src = "((-1 - 2 * -3) / (3 - 2) + (9 * 5 - 9) * (2 * 3 * (3 - 0))) * -100 + 99-100 * -1-1"
s_src = s_src.replace(' ', '')
print(my_math(s_src))
s_ret = ((-1 - 2 * -3) / (3 - 2) + (9 * 5 - 9) * (2 * 3 * (3 - 0))) * -100 + 99 - 100 * -1 - 1
print(s_ret)
运行结果:
PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:
在线一元函数(方程)求解计算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科学计算器在线使用_高级计算器在线计算:
http://tools.jb51.net/jisuanqi/jsqkexue
在线计算器_标准计算器:
http://tools.jb51.net/jisuanqi/jsq
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/tyrantu1989/article/details/78473637
标签:Python3.6,正则,计算器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
innerHTML在Mozilla Firefox和Opera下执行的一个特例情况。
2023-09-16 11:23:46
django 连接数据库出现1045错误的解决方式
2024-01-17 19:37:47
“Unable to read local eventlog (reason:事件日志文件已在读取时间更改)”解决办法
2009-08-27 13:12:00
Python实现爬取亚马逊数据并打印出Excel文件操作示例
2021-09-10 05:10:36
![](https://img.aspxhome.com/file/2023/3/105203_0s.png)
三分钟时间教你用Python绘制春联
2023-11-06 00:26:08
![](https://img.aspxhome.com/file/2023/9/115759_0s.jpg)
AspJpeg组件:介绍、注册、高级使用方法
2010-01-25 12:42:00
IE地址栏显示网站图标制作方法
2007-10-13 11:08:00
详解js前端代码异常监控
2024-04-22 13:06:26
![](https://img.aspxhome.com/file/2023/0/135800_0s.png)
TMDPHP 模板引擎使用教程
2023-11-15 03:21:56
![](https://img.aspxhome.com/file/2023/9/94489_0s.gif)
keras使用Sequence类调用大规模数据集进行训练的实现
2021-01-03 20:24:35
巧用MySQL InnoDB引擎锁机制解决死锁问题
2008-12-19 17:24:00
Navicat 连接SQLServer数据库(图文步骤)
2024-01-22 03:42:31
![](https://img.aspxhome.com/file/2023/8/113578_0s.jpg)
解析python的局部变量和全局变量
2023-08-23 18:17:01
MySQL高级查询语法分析
2024-01-13 14:11:09
![](https://img.aspxhome.com/file/2023/3/76363_0s.jpg)
Python设计模式之工厂方法模式实例详解
2021-02-26 05:40:59
![](https://img.aspxhome.com/file/2023/9/76129_0s.png)
利用OBJECT_DEFINITION函数来代码存档
2009-01-20 15:34:00
详解appium+python 启动一个app步骤
2021-12-19 02:44:55
![](https://img.aspxhome.com/file/2023/3/70203_0s.png)
解决新版Pycharm中Matplotlib图像不在弹出独立的显示窗口问题
2023-06-28 02:44:14
![](https://img.aspxhome.com/file/2023/5/61455_0s.jpg)
在PyCharm环境中使用Jupyter Notebook的两种方法总结
2021-10-07 08:06:32
![](https://img.aspxhome.com/file/2023/7/112027_0s.jpg)
pip更新问题的解决:'python -m pip install --upgrade pip' 报错问题(最新推荐)
2023-04-23 04:32:13
![](https://img.aspxhome.com/file/2023/8/105028_0s.png)