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

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

 #!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. 发现sqlite

    1.前言 本文使用"发现"二字,是表示我作为一个用过mysql oracle hive以及各种nosql数据库的男人,竟然发现有一个如此常识的数据库我竟然不知道. 在配置airfl ...

  2. 2019牛客暑期多校训练营(第七场)D Number——实系数多项式因式分解定理

    前置知识 代数基本定理 定理:每个次数 ≥ 1 复系数多项式在复数域中至少有一个跟. 由此推出,n次复系数多项式方程在复数域内有且只有n个根(重根按重数计算).(只要不断把多项式除以(x-xa),即可 ...

  3. JavaScript有趣的知识点

    JavaScript中总有一些有趣的小知识,而且又是很容易犯错的.我把我遇到的慢慢罗列一下,方便大家避坑 typeof(null)返回的结果是 object " "变成布尔类型为t ...

  4. 浏览器事件环(EventLoop)

    1. 基础知识 1. js语言特点 1. js语言是单线程语言,主线程是单线程.如UI渲染,脚本加载是主线程任务. 2. js语言采用事件循环(EventLoop)机制. 2. 同步任务: 不被引擎挂 ...

  5. 020_统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个

    统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个 #!/bin/bash#awk 使用-F 选项指定文件内容的分隔符是/或者:#条件判断$7:$8 大于等于 13:30, ...

  6. alarm函数

    alarm函数 设置定时器(闹钟).在指定seconds后,内核会给当前进程发送14)SIGALRM信号.进程收到该信号,默认动作终止. 每个进程都有且只有唯一个定时器. unsigned int a ...

  7. Ubuntu 14.04 更换为阿里云源

    #备份sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak#编辑sudo vim /etc/apt/sources.list,清空后,加入以下 ...

  8. 【Robot Framework 项目实战 03】使用脚本自动生成统一格式的RF自动化用例

    背景 虽然大家都已经使用了统一的关键字,但是在检查了一些测试用例之后,还是发现因为大家对RF的熟悉程度不一导致的测试用例颗粒度差异很大的情况:而且在手动方式转化测试用例过程中,有不少工作是完全重复的且 ...

  9. 运维自动化之ansible

    Ansible简介 Ansible是一个简单的自动化运维管理工具,基于Python语言实现,由Paramiko和PyYAML两个关键模块构建,可用于自动化部署应用.配置.编排task(持续交付.无宕机 ...

  10. vmware安装Linux