利用正则实现计算器

利用正则来实现简单计算器的功能,能够设计计算带括号的加减乘除运算。当然不使用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. Codefoces 723A The New Year: Meeting Friends

    A. The New Year: Meeting Friends time limit per test:1 second memory limit per test:256 megabytes in ...

  2. COGS 1299. bplusa【听说比a+b还要水的大水题???】

    1299. bplusa ☆   输入文件:bplusa.in   输出文件:bplusa.out   评测插件 时间限制:1 s   内存限制:128 MB [题目描述] 输入一个整数n,将其拆为两 ...

  3. HDU--2114

    Calculate S(n) Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. TypeScript笔记 5--变量声明(解构和展开)

    解构是什么 解构(destructuring assignment)是一种表达式,将数组或者对象中的数据赋给另一变量. 在开发过程中,我们经常遇到这样问题,需要将对象某个属性的值赋给其它两个变量.代码 ...

  5. PHP安全之webshell和后门检测

    基于PHP的应用面临着各种各样的攻击: XSS:对PHP的Web应用而言,跨站脚本是一个易受攻击的点.攻击者可以利用它盗取用户信息.你可以配置Apache,或是写更安全的PHP代码(验证所有用户输入) ...

  6. logback的使用和logback.xml详解

    一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分为下面下个模块: logback-core:其它两 ...

  7. 短时间内多个请求状态更新,导致react 不能及时响应问题总结

    个人总结 这段时间项目中遇到这样一个问题,旧项目中增加了一个聊天对话的模块,这是其他同学负责的部分,因为要有消息提醒,所以做了个轮询.消息提示因为是页头部分,所以每个模块都会引用到.这是背景. 现象 ...

  8. jsp页面固定页面为绝对路径

    1 <!-- 固定到绝对路径 --> 2 <base href="<%=request.getContextPath()%>/"/>

  9. Web前端性能优化——如何提高页面加载速度

    前言:  在同样的网络环境下,两个同样能满足你的需求的网站,一个"Duang"的一下就加载出来了,一个纠结了半天才出来,你会选择哪个?研究表明:用户最满意的打开网页时间是2-5秒, ...

  10. SQL作业及调度创建

    转自:http://www.cnblogs.com/accumulater/p/6223909.html --定义创建作业 转自http://hi.baidu.com/procedure/blog/i ...