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

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

 #!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. idea 包.路径切换为目录结构

    取消勾选

  2. 【题解】[Usaco2007 Open]Catch That Cow 抓住那只牛-C++

    题目DescriptionFarmer John has been informed of the location of a fugitive cow and wants to catch her ...

  3. 009_linuxC++之_友元函数

    (一)定义:友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数.类授予它的友元特别的访问权.通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你的类时,还要征得其 ...

  4. Oracle 后台进程(五)SMON进程

    转载自:刘相兵 Maclean Liu 文章 你所不知道的后台进程 SMON 功能   SMON(system monitor process)系统监控后台进程,有时候也被叫做 system clea ...

  5. 【线性代数】5-3:克莱姆法则,逆和体积(Cramer's Rule,Inverses,and Volumes)

    title: [线性代数]5-3:克莱姆法则,逆和体积(Cramer's Rule,Inverses,and Volumes) categories: Mathematic Linear Algebr ...

  6. JavaWeb_(Spring框架)xml配置文件

    系列博文 JavaWeb_(Spring框架)xml配置文件  传送门 JavaWeb_(Spring框架)注解配置 传送门 Xml配置 a)Bean元素:交由Spring管理的对象都要配置在bean ...

  7. root of context hierarchy

    Spring Boot项目,运行不明中断.日志如下: 2018-11-03 11:03:43.358 INFO [Thread-2][AbstractApplicationContext.java:9 ...

  8. richtextbox Ctrl+V只粘贴纯文本格式

    只能粘贴剪切板中的TXT内容 并且 不能改变 剪切板的内容1 当用户按下Ctrl+V屏蔽系统的粘贴功能,然后添加自己的功能2019年12月19日 19:34:38 private void richT ...

  9. sql注入笔记-sqlite

    1. SQLite 1. 常用语句及基本结构 (1)sqlite因为其比较简易每个db文件就是一个数据库,所以不存在information_schema数据库,但存在类似作用的表sqlite_mast ...

  10. Sublime Markdown预览插件安装流程

    使用方法 在sublime中已编辑好的markdown使用快捷键 Alt+M 即可在浏览器预览效果. 需要安装的插件 Markdown Editting:主要用来做 Markdown 编辑时的语法高亮 ...