python3 正则表达式 re模块之辣眼睛 计算器
额。。。学到几个常用模块了,也要其中考试了,每天晚上敲一点,敲得脑壳疼,不过又想到好一点的办法了,有时间再改吧。
此非吾所欲也,实属无奈也。。。。复习之路漫漫,吾将到书上求索,在此不多逗留,我挥一挥衣袖,留下一段惨不忍睹的代码,且不带走一片云彩。
#!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模块之辣眼睛 计算器的更多相关文章
- python3 正则表达式re模块
正则表达式的功能:字符串的模糊匹配查询import re元字符 . ---->匹配除换行符意外的任意字符 ^ ---->匹配行首位置 $ ---->匹配行尾位置 关于重复的元字符 * ...
- Python3 正则表达式 re 模块的使用 - 学习笔记
re 模块的引入 re 模块的使用 re.compile() re.match()与re.search() re.match re.search() 区别 re.findall()与re.findit ...
- Python3之turtle模块的使用
Python3之turtle模块的使用 直接扣代码就行: import turtle as t t.pensize(4) t.hideturtle() t.colormode(255) t.c ...
- python基础系列教程——Python3.x标准模块库目录
python基础系列教程——Python3.x标准模块库目录 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata ...
- Python3中正则模块re.compile、re.match及re.search函数用法详解
Python3中正则模块re.compile.re.match及re.search函数用法 re模块 re.compile.re.match. re.search 正则匹配的时候,第一个字符是 r,表 ...
- 详解 Python3 正则表达式(三)
上一篇:详解 Python3 正则表达式(二) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 模块级别的函数 ...
- 详解 Python3 正则表达式(二)
上一篇:详解 Python3 正则表达式(一) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 使用正则表达式 ...
- 详解 Python3 正则表达式(一)
本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 正则表达式介绍 正则表达式(Regular expressio ...
- python025 Python3 正则表达式
Python3 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. ...
随机推荐
- idea 查看类继承关系的快捷键
类似eclipse ctrl+t的快捷键,idea中是ctrl+H
- 边学边体验django
django菜鸟入门连接:http://www.runoob.com/django/django-template.html 安装以及Demo里面都有. 我边学边在电脑上演示,然后发现一些和实际不一样 ...
- jQuery.extend([deep], target, object1, [objectN])
jQuery.extend([deep], target, object1, [objectN]) 概述 用一个或多个其他对象来扩展一个对象,返回被扩展的对象.直线电机 如果不指定target,则给j ...
- Win 7 x64 + Visual Studio 2015为WinXP编译可执行程序
造冰箱的大熊猫@cnblogs 2019/9/5 本文承接<Win7下使用Visual Studio为WinXP编译可执行文件>一文. - 在64位Win7(开发机)上,编写基于C的Win ...
- [Luogu] 【模板】点分治1
// 模板题#include <bits/stdc++.h> ; , head[N], dis[N]; ]; int size[N], maxson[N], Root; bool vis[ ...
- luoguP3366 【模板】最小生成树 x
P3366 [模板]最小生成树 2.4K通过 6.3K提交 题目提供者HansBug 标签 云端↑ 生成树 难度 普及- 时空限制 1s / 128MB 题目描述 如题,给出一个无向图,求出最小生成树 ...
- 【线性代数】4-4:正交基和Gram算法(Orthogonal Bases and Gram-Schmidt)
title: [线性代数]4-4:正交基和Gram算法(Orthogonal Bases and Gram-Schmidt) categories: Mathematic Linear Algebra ...
- php安装扩展的地址
1 查看扩展 phpinfo or extention_loads or php -m 下载扩展地址 http://pecl.php.net or http://windows.php.n ...
- 【LeetCode-86】分隔链表
[题目描述] 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-& ...
- 最简单的babel+webpack配置
首先先介绍一下2个重要的库:core-js 和 regenerator core-js core-js 是用于 JavaScript 的组合式标准化库,它包含 es5 (e.g: object.fre ...