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

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

 #!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. git中的基本命令

    工作区:      当前的编辑位置 缓存区:      add 之后的区域 版本库:      commit之后的区域就是版本库 git init .         初始化 git add .    ...

  2. [React Native] Up & Running with React Native & TypeScript

    Create a new application with Typescript: react-native init RNTypeScript --template typescript Then: ...

  3. P2168 [NOI2015]荷马史诗 k叉哈夫曼树

    思路:哈夫曼编码 提交:1次(参考题解) 题解:类似合并果子$QwQ$ 取出前$k$小(注意如果叶子结点不满的话要补全),合并起来再扔回堆里去. #include<cstdio> #inc ...

  4. 001_FreeRTOS中断配置

    (一)优先级看:022_STM32中断优先级分组解析 (二)FreeRTOS的中断配置中没有处理亚优先级(响应优先级)的情况,所以只能配置成组4,16个都为抢占优先级 (三)此宏用来设置 MCU 使用 ...

  5. 牛客练习赛53 (C 富豪凯匹配串) bitset

    没想到直接拿 bitset 能过 $10^8$~ code: #include <bits/stdc++.h> #define N 1004 #define setIO(s) freope ...

  6. 【csp模拟赛6】树上统计-启发式合并,线段树合并

    30%:暴力 40%:枚举L,R从L~n枚举,R每增大一个,更新需要的边(bfs实现)60%:枚举每条边, 计算每条边的贡献另外20%的数据:枚举每条边,计算每条边的贡献100%:对于每一条边统计 有 ...

  7. postgresql 一些操作

    postgresql 对sql语句敏感的. 所以尽量标准化输入 #############查看版本信息 ############ 1.查看客户端版本 psql --version 1 2.查看服务器端 ...

  8. c标签forEach

    <c:forEach items="${requestScope.getUserMenuModelList}" var="item" varStatus= ...

  9. vgg16 感受野计算

    code: vgg_16 = [ [3, 1], [3, 1], [2, 2], [3, 1], [3, 1], [2, 2], [3, 1], [3, 1], [3, 1], [2, 2], [3, ...

  10. vue实现购物清单列表添加删除

    vue实现购物清单列表添加删除 一.总结 一句话总结: 基础的v-model操作,以及数组的添加(push)删除(splice)操作 1.checkbox可以绑定数组,也可以直接绑定值? 绑定数组就是 ...