利用正则实现计算器

利用正则来实现简单计算器的功能,能够设计计算带括号的加减乘除运算。当然不使用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正则实现简单计算器的更多相关文章

  1. Python之实现简单计算器功能

    一,需求分析 要求计算一串包含数字+-*/()的类似于3*( 4+ 50 )-(( 100 + 40 )*5/2- 3*2* 2/4+9)*((( 3 + 4)-4)-4)表达式的数值 二,知识点 正 ...

  2. Python正则简单实例分析

    Python正则简单实例分析 本文实例讲述了Python正则简单用法.分享给大家供大家参考,具体如下: 悄悄打入公司内部UED的一个Python爱好者小众群,前两天一位牛人发了条消息: 小的测试题:  ...

  3. 用Python写一个简单的Web框架

    一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...

  4. Python正则式的基本用法

    Python正则式的基本用法 1.1基本规则 1.2重复 1.2.1最小匹配与精确匹配 1.3前向界定与后向界定 1.4组的基本知识 2.re模块的基本函数 2.1使用compile加速 2.2 ma ...

  5. hiho #1332 : 简单计算器 栈+递归

    #1332 : 简单计算器 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 编写一个程序可以完成基本的带括号的四则运算.其中除法(/)是整除,并且在负数除法时向0取整.( ...

  6. 认识python正则模块re

    python正则模块re python中re中内置匹配.搜索.替换方法见博客---python附录-re.py模块源码(含re官方文档链接) 正则的应用是处理一些字符串,phthon的博文python ...

  7. 1.C#WinForm基础制作简单计算器

    利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...

  8. 菜鸟学Android编程——简单计算器《一》

    菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...

  9. python 正则,常用正则表达式大全

    Nginx访问日志匹配 re.compile #re.compile 规则解释,改规则必须从前面开始匹配一个一个写到后面,前面一个修改后面全部错误.特殊标准结束为符号为空或者双引号:  改符号开始 从 ...

随机推荐

  1. 我们编写 React 组件的最佳实践

    刚接触 React 的时候,在一个又一个的教程上面看到很多种编写组件的方法,尽管那时候 React 框架已经相当成熟,但是并没有一个固定的规则去规范我们去写代码. 在过去的一年里,我们在不断的完善我们 ...

  2. HDU1159-Common Subsequence-LCS

    上次写题解写到一半,写的比较具体,没写完,忘记存草稿了...导致现在没心情了. Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    ...

  3. 2017 ECJTU ACM程序设计竞赛 矩阵快速幂+二分

    矩阵 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submission ...

  4. Victor and World(spfa+状态压缩dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 Victor and World Time Limit: 4000/2000 MS (Java/ ...

  5. Effective Java 第三版——24. 优先考虑静态成员类

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  6. 微信小程序监听input输入并取值

    小程序的事件分为两种,冒泡和非冒泡事件,像<form/>的submit事件,<input/>的input事件,<scroll-view/>的scroll事件等非冒泡 ...

  7. solrcloud(solr集群版)安装与配置

    1 Solr集群 1.1 什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的 ...

  8. SSH会话连接超时问题

    目前大多数ssh服务是运行在Linux系统上的sshd服务.当访问终端在windows上时,各终端软件,如,putty,SecureCRT等,大多支持设置向服务器端自动发送消息,来防止终端定期超时.其 ...

  9. javascript中window.location.search的用法和作用。

    用该属性获取页面 URL 地址: window.location 对象所包含的属性 属性 描述 hash 从井号 (#) 开始的 URL(锚) host 主机名和当前 URL 的端口号 hostnam ...

  10. [SinGuLaRiTy] 复习模板-高精度模板

    [SinGuLaRiTy-1042] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 结构体封装 //高精度运算 注意%I64d与%lld # ...