额。。。学到几个常用模块了,也要其中考试了,每天晚上敲一点,敲得脑壳疼,不过又想到好一点的办法了,有时间再改吧。

此非吾所欲也,实属无奈也。。。。复习之路漫漫,吾将到书上求索,在此不多逗留,我挥一挥衣袖,留下一段惨不忍睹的代码,且不带走一片云彩。

 #!usr/bin/env/ python
# -*- coding:utf-8 -*-
# Author: XiaoFeng
import re a = '1 - 2 * ( ( 6 0 -3 0 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
a_new = re.sub(" ", "", a)
print(a_new)
ret = re.findall(r"\([^()]*\)", a_new)
while ret:
ret = re.findall(r"\([^()]*\)", a_new)
print(ret)
for element in ret:
formula = element
formula = formula.replace("(", "")
formula = formula.replace(")", "")
# print(formula)
for i in formula:
if "--" in formula:
formula = formula.replace("--", "+")
elif "*" == i:
handle = re.search(r"[-+]?\d+\.?\d*\*[-+]?\d+\.?\d*", formula)
if handle:
handle_list = handle.group()
else:
continue
handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list)
if "." in handle_a or "." in handle_b:
result = float(handle_a) * float(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
else:
result = int(handle_a) * int(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
formula = formula.replace(handle_list, result)
# print(formula)
elif "/" == i:
handle = re.search(r"[-+]?\d+\.?\d*/[-+]?\d+\.?\d*", formula)
if handle:
handle_list = handle.group()
else:
continue
handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list)
if "." in handle_a or "." in handle_b:
result = float(handle_a) / float(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
else:
result = int(handle_a) / int(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
formula = formula.replace(handle_list, result)
# print(formula)
for i in formula:
if "--" in formula:
formula = formula.replace("--", "+")
elif "+" == i:
handle = re.search(r"[-+]?\d+\.?\d*\+[-+]?\d+\.?\d*", formula)
if handle:
handle_list = handle.group()
else:
continue
handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list)
if "." in handle_a or "." in handle_b:
result = float(handle_a) + float(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
else:
result = int(handle_a) + int(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
formula = formula.replace(handle_list, result)
# print(formula)
elif "-" == i:
handle = re.search(r"[-+]?\d+\.?\d*-[-+]?\d+\.?\d*", formula)
if handle:
handle_list = handle.group()
else:
continue
handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list)
if "." in handle_a or "." in handle_b:
result = float(handle_a) + float(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
else:
result = int(handle_a) + int(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
formula = formula.replace(handle_list, result)
# print(formula)
# print(formula)
# print(element)
a_new = a_new.replace(element, formula)
# print(a_new)
print("------------")
print(a_new)
formula = a_new
for i in formula:
if "--" in formula:
formula = formula.replace("--", "+")
elif "*" == i:
handle = re.search(r"[-+]?\d+\.?\d*\*[-+]?\d+\.?\d*", formula)
if handle:
handle_list = handle.group()
else:
continue
handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list)
if "." in handle_a or "." in handle_b:
result = float(handle_a) * float(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
else:
result = int(handle_a) * int(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
formula = formula.replace(handle_list, result)
print(formula)
elif "/" == i:
handle = re.search(r"[-+]?\d+\.?\d*/[-+]?\d+\.?\d*", formula)
if handle:
handle_list = handle.group()
else:
continue
handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list)
if "." in handle_a or "." in handle_b:
result = float(handle_a) / float(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
else:
result = int(handle_a) / int(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
formula = formula.replace(handle_list, result)
print(formula)
print("=========")
for i in formula:
if "--" in formula:
formula = formula.replace("--", "+")
elif "+" == i:
handle = re.search(r"[-+]?\d+\.?\d*\+[-+]?\d+\.?\d*", formula)
if handle:
handle_list = handle.group()
else:
continue
handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list)
if "." in handle_a or "." in handle_b:
result = float(handle_a) + float(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
else:
result = int(handle_a) + int(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
formula = formula.replace(handle_list, result)
print(formula)
elif "-" == i:
handle = re.search(r"[-+]?\d+\.?\d*-[-+]?\d+\.?\d*", formula)
if handle:
handle_list = handle.group()
else:
continue
handle_a, handle_b = re.findall(r"[-+]?\d+\.?\d*", handle_list)
if "." in handle_a or "." in handle_b:
result = float(handle_a) + float(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
else:
result = int(handle_a) + int(handle_b)
if result >= 0:
result = "+" + str(result)
result = str(result)
formula = formula.replace(handle_list, result)
print(formula)

python3 正则表达式 re模块之辣眼睛 计算器的更多相关文章

  1. python3 正则表达式re模块

    正则表达式的功能:字符串的模糊匹配查询import re元字符 . ---->匹配除换行符意外的任意字符 ^ ---->匹配行首位置 $ ---->匹配行尾位置 关于重复的元字符 * ...

  2. Python3 正则表达式 re 模块的使用 - 学习笔记

    re 模块的引入 re 模块的使用 re.compile() re.match()与re.search() re.match re.search() 区别 re.findall()与re.findit ...

  3. Python3之turtle模块的使用

    Python3之turtle模块的使用     直接扣代码就行: import turtle as t t.pensize(4) t.hideturtle() t.colormode(255) t.c ...

  4. python基础系列教程——Python3.x标准模块库目录

    python基础系列教程——Python3.x标准模块库目录 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata ...

  5. Python3中正则模块re.compile、re.match及re.search函数用法详解

    Python3中正则模块re.compile.re.match及re.search函数用法 re模块 re.compile.re.match. re.search 正则匹配的时候,第一个字符是 r,表 ...

  6. 详解 Python3 正则表达式(三)

    上一篇:详解 Python3 正则表达式(二) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 模块级别的函数 ...

  7. 详解 Python3 正则表达式(二)

    上一篇:详解 Python3 正则表达式(一) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 使用正则表达式 ...

  8. 详解 Python3 正则表达式(一)

    本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 正则表达式介绍 正则表达式(Regular expressio ...

  9. python025 Python3 正则表达式

    Python3 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. ...

随机推荐

  1. 打包完的rcp产品svn不储存密码问题

    原因是缺少org.eclipse.core.runtime.compatibility.auth 这个依赖,需要添加到依赖中去 因为使用SVNKit的时候会去调eclipse这个api 详情见: ht ...

  2. Java8-Executors-No.03

    import java.util.Arrays; import java.util.List; import java.util.concurrent.Callable; import java.ut ...

  3. PHP实现页面跳转功能

    PHP跳转到指定页面的问题通常都会建设网站需求上看到,比如我们需要从一个页面跳转到另一个页面来实现某个功能或者效果.其实在PHP中进行页面跳转是有多种方法的,那么这篇文章就给大家介绍下,有哪些方法可以 ...

  4. 路由器配置——基于链路的OSPF的MD5口令认证

    一.实验目的:掌握基于链路的OSPFMD5口令认证 二.拓扑图: 三.具体步骤配置: (1)R1路由器的配置 Router>enable Router#configure terminal En ...

  5. CF1153F Serval and Bonus Problem 【期望】

    题目链接:洛谷 作为一只沉迷数学多年的蒟蒻OIer,在推柿子和dp之间肯定要选推柿子的! 首先假设线段长度为1,最后答案乘上$l$即可. 对于$x$这个位置,被区间覆盖的概率是$2x(1-x)$(线段 ...

  6. nginx下载 并将vue项目部署上去

    这是一个比较全的nginx配置说明:新手可以先忽略,直接往下看:https://www.cnblogs.com/findbetterme/p/11289273.html nginx官网下载地址:htt ...

  7. vue3.x 错误记录

    1:css报错 This dependency was not found: * !!vue-style-loader!css-loader?{"minimize":false,& ...

  8. 1-4CMYK色彩模式

    http://www.missyuan.com/thread-350717-1-1.html CMYK也称作印刷色彩模式,顾名思义就是用来印刷的. 只要是在印刷品上看到的图像,就是CMYK模式表现的 ...

  9. 三十、CentOS 7之systemd

    一.系统启动流程 POST --> bootloader  --> MBR工作 --> kernel(initramfs/initrd) --> ro rootfs --> ...

  10. kubernetes案例 tomcat+mysql

    该文章参考<kubernetes 权威指南> 环境: [root@master tomcat-mysql]# kubectl get nodesNAME      STATUS    AG ...