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 规则解释,改规则必须从前面开始匹配一个一个写到后面,前面一个修改后面全部错误.特殊标准结束为符号为空或者双引号: 改符号开始 从 ...
随机推荐
- Phabricator API Go 创建task/提交文件到Phabricator
Go Phabricator API 代码/程序创建task/提交文件到Phabricator Creat Task or upload file to phabricator with code i ...
- js面向对象学习笔记(三):原型
//原型:改写对象下面公用的方法或者属性,让公用的方法或者属性在内存中只存在一份(提高性能)//原型:prototype :要写在构造函数的下面var arr =[1,2,3,4,5];var arr ...
- BZOJ 1800: [Ahoi2009]fly 飞行棋【思维题,n^4大暴力】
1800: [Ahoi2009]fly 飞行棋 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1689 Solved: 1335[Submit][St ...
- B. OR in Matrix
B. OR in Matrix time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- ionic2 安装与cordova打包
1.安装: cnpm install -g cordova ionic ionic start name cd name cnpm install 2.环境配置: http://www.cnblo ...
- post请求测试
也可以用下图来指定测试参数
- Android Studio解决导入项目非常慢的问题
http://www.androidchina.net/5527.html Android Studio比Eclipse ADT有巨大的优势. Android Studio原生支持使用Gradle来构 ...
- JEECG 3.7.2版本发布,企业级JAVA快速开发平台
JEECG 3.7.2版本发布 - 微云快速开发平台 JEECG是一款基于代码生成器的J2EE快速开发平台,开源界"小普元"超越传统商业企业级开发平台.引领新的开发模式(Onli ...
- MLlib--GBDT算法
转载请标明出处http://www.cnblogs.com/haozhengfei/p/8b9cb1875288d9f6cfc2f5a9b2f10eac.html GBDT算法 江湖传言:GBDT算法 ...
- Spark算子--flatMapValues
转载请标明出处http://www.cnblogs.com/haozhengfei/p/e7a46cecc65720997392516d553d9891.html flatMapValues--Tra ...