python正则实现简单计算器
利用正则实现计算器
利用正则来实现简单计算器的功能,能够设计计算带括号的加减乘除运算。当然不使用eval等语句。
利用递归:
import re
from functools import reduce
def foo2(y):
if '(' in y:
t1 = re.search(r'\([^()]+\)',y)
temp1 = t1.group().strip('()')
res = foo1(temp1)
y = y.replace(t1.group(),str(res))
return foo2(y)
else:
return foo1(y)
def foo1(x):
x = x.replace(' ','')
if x.find('*') == -1 and x.find('/') == -1:
res = str(foo3(x))
return res
else:
t1 = re.search(r'(?P<x1>(-?\d+\.\d*)|(-?\d+))(?P<x2>\*|\/)(?P<x3>(-?\d+\.\d*)|(-?\d+))',x)
t2 = re.search(r'((-?\d+\.\d*)|(-?\d+))(\*|\/)((-?\d+\.\d*)|(-?\d+))',x)
y1 = float(t1.group('x1')) if '.' in t1.group('x1') else int(t1.group('x1'))
y2 = float(t1.group('x3')) if '.' in t1.group('x3') else int(t1.group('x3'))
temp = y1 * y2 if t1.group('x2') == '*' else y1 / y2
tt = '+' + str(temp) if y1 < 0 and temp > 0 else str(temp)
x = x.replace(t2.group(),tt)
return foo1(x)
def foo3(z):
z = z.replace(' ','')
z = re.sub('\+\-|\-\+','-',z)
z = re.sub('\-\-|\+\+','+',z)
z = re.findall('-?\d+\.\d*|-?\d+',z)
z = list(map(lambda x: float(x) if '.' in x else int(x), z))
res = reduce(lambda x,y:x+y,z)
return res
while True:
s = input('please input :> ')
print('the result is %s' %foo2(s))
利用循环:
import re
from functools import reduce
def foo2(y):
while '(' in y:
t1 = re.search(r'\([^()]+\)',y)
temp1 = t1.group().strip('()')
res = foo1(temp1)
y = y.replace(t1.group(),str(res))
return foo1(y)
def foo1(x):
x = x.replace(' ','')
while x.find('*') != -1 or x.find('/') != -1:
t1 = re.search(r'(?P<x1>(-?\d+\.\d*)|(-?\d+))(?P<x2>\*|\/)(?P<x3>(-?\d+\.\d*)|(-?\d+))',x)
t2 = re.search(r'((-?\d+\.\d*)|(-?\d+))(\*|\/)((-?\d+\.\d*)|(-?\d+))',x)
y1 = float(t1.group('x1')) if '.' in t1.group('x1') else int(t1.group('x1'))
y2 = float(t1.group('x3')) if '.' in t1.group('x3') else int(t1.group('x3'))
temp = y1 * y2 if t1.group('x2') == '*' else y1 / y2
tt = '+' + str(temp) if y1 < 0 and temp > 0 else str(temp)
x = x.replace(t2.group(),tt)
return str(foo3(x))
def foo3(z):
z = z.replace(' ','')
z = re.sub('\+\-|\-\+','-',z)
z = re.sub('\-\-|\+\+','+',z)
z = re.findall('-?\d+\.\d*|-?\d+',z)
z = list(map(lambda x: float(x) if '.' in x else int(x), z))
res = reduce(lambda x,y:x+y,z)
return res
while True:
s = input('please input :> ')
print('the result is %s' %foo2(s))
python正则实现简单计算器的更多相关文章
- Python之实现简单计算器功能
一,需求分析 要求计算一串包含数字+-*/()的类似于3*( 4+ 50 )-(( 100 + 40 )*5/2- 3*2* 2/4+9)*((( 3 + 4)-4)-4)表达式的数值 二,知识点 正 ...
- Python正则简单实例分析
Python正则简单实例分析 本文实例讲述了Python正则简单用法.分享给大家供大家参考,具体如下: 悄悄打入公司内部UED的一个Python爱好者小众群,前两天一位牛人发了条消息: 小的测试题: ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- Python正则式的基本用法
Python正则式的基本用法 1.1基本规则 1.2重复 1.2.1最小匹配与精确匹配 1.3前向界定与后向界定 1.4组的基本知识 2.re模块的基本函数 2.1使用compile加速 2.2 ma ...
- hiho #1332 : 简单计算器 栈+递归
#1332 : 简单计算器 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 编写一个程序可以完成基本的带括号的四则运算.其中除法(/)是整除,并且在负数除法时向0取整.( ...
- 认识python正则模块re
python正则模块re python中re中内置匹配.搜索.替换方法见博客---python附录-re.py模块源码(含re官方文档链接) 正则的应用是处理一些字符串,phthon的博文python ...
- 1.C#WinForm基础制作简单计算器
利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...
- 菜鸟学Android编程——简单计算器《一》
菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...
- python 正则,常用正则表达式大全
Nginx访问日志匹配 re.compile #re.compile 规则解释,改规则必须从前面开始匹配一个一个写到后面,前面一个修改后面全部错误.特殊标准结束为符号为空或者双引号: 改符号开始 从 ...
随机推荐
- visualMap组件横向展示
- Error 0xC00000FD: Stack overflow 之 更改堆栈保留大小
Stack overflow 顾名思义就是堆栈内存溢出. 一.无限递归 这个要自己仔细检查一下,程序中是否含有无限递归的情况,比如下面这就是无限递归: int function(int x, in ...
- Link-Cut-Trees
填坑,填坑,填坑…… 开篇镇人品……下文的比喻仅供娱乐…… 为了迎接JSZX校内互测,我临时填坑学了LCT…… 怎么说呢……我也是懵懵懂懂地看了N篇博客,对着标程敲上一发代码,然后才慢慢理解.这里推荐 ...
- [51nod1671]货物运输
公元2222年,l国发生了一场战争. 小Y负责领导工人运输物资. 其中有m种物资的运输方案,每种运输方案形如li,ri.表示存在一种货物从li运到ri. 这里有n个城市,第i个城市与第i+1个城市相连 ...
- UVA11636-Hello World!-水题
Hello World! Time limit: 1.000 seconds When you first made the computer to print the sentence "H ...
- Codeforces Round #449 (Div. 2)-897A.Scarborough Fair(字符替换水题) 897B.Chtholly's request(处理前一半) 897C.Nephren gives a riddle(递归)
A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Unity Object Pool完全体
using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public ...
- 使用notepad++作为keil的外部编辑器
之前一直不喜欢keil的编辑界面,但是又不想太浮夸.看到很多群里有人用vscode写stm32的序,但是直接用vscode编写的花,各种设置很麻烦.而且还不能调试.于是想到有没有一个轻便简约的外部编辑 ...
- 常用SQL笔记总结
DDL(data definition language)创建和管理表 1.创建表 1.直接创建 例如: create table emp( name varchar(20), salary int ...
- 苹果新贵 Swift 之前世今生
摘要 : 做为一个70后程序员,克里斯先后发明了 LLVM.Clang 和 Swift,请问你做了什么? 上 周出差劳顿,这篇文章几次动笔都未完成,常常躺倒床上就昏睡过去.南方的天气闷热潮湿,让我 ...