python 利用正则实现简易计算器
来源:互联网 发布:淘宝金牌店铺联盟 编辑:程序博客网 时间:2024/05/20 11:49
#-*-coding:utf-8-*-#__author:martin#date:2017/10/11import redef f_string(s): s = s.replace(' ','') s = s.replace('++', '+') s = s.replace('--', '+') s = s.replace('+-', '-') return sdef cal_mul_div(s): #(33+4*5.5+10/2) while re.search('\d+\.?\d*[*/]\d+\.?\d*',s) : ret = re.search('\d+\.?\d*[*/]\d+\.?\d*',s).group() if ret.count('*'): x, y = re.split('[*]', ret) mul = float(x) * float(y) s =s.replace(ret,str(mul)) if ret.count('/'): x, y = re.split('[/]', ret) mul = float(x) / float(y) s =s.replace(ret,str(mul)) return sdef cal_plus_sub(s): #(33+22.0-5.0+8-5+9) while re.search('[\-]?\d+\.?\d*[+][\-]?\d+\.?\d*', s): ret = re.search('[\-]?\d+\.?\d*[+][\-]?\d+\.?\d*', s).group() x, y = re.split('[+]', ret) add = str(float(x)+float(y)) s = s.replace(ret,'+'+add) s =f_string(s) while re.search('[\-]?\d+\.?\d*[-][\-]?\d+\.?\d*', s): ret = re.search('[\-]?\d+\.?\d*[-][\-]?\d+\.?\d*', s).group() numbers = re.split('[-]', ret) if len(numbers) == 3: result =0 for i in numbers: if i: result -= float(i) else: x,y = numbers result = float(x)-float(y) s = s.replace(ret,'+'+str(result)) s = f_string(s) return sexp = '(1+(3*4)+2+(4*5)-100)'while exp.count('(') > 0: ret = re.search('\([^()]+\)', exp).group() replace_str = cal_mul_div(ret) replace_str = cal_plus_sub(replace_str) exp = exp.replace(ret,replace_str[1:-1])print(exp.replace('+',''))
阅读全文
0 0
- python 利用正则实现简易计算器
- python----------------正则实现计算器功能
- Python版简易计算器的实现
- 用python实现的简易计算器
- Python Tkinter简易计算器
- Python简易计算器
- python简易计算器
- [python]简易计算器
- python-tkinter 简易计算器
- 初学python:简易计算器
- 利用socket网络编程,实现的简易CS计算器
- 利用cgi 实现简易网页加法计算器功能
- python:利用tkinter实现的计算器源代码
- 计算器简易实现
- javascript实现简易计算器
- js实现简易计算器
- 实现简易计算器
- 简易计算器的实现
- 微信小程序 —— 自定义长圆形view
- sizeof PK strlen
- 正则表达式语法入门
- 二叉树的序遍历
- [转]矩阵奇异值分解(SVD)及其应用
- python 利用正则实现简易计算器
- Oracle笔记十二:集合、序列
- 冒泡排序
- VueJs探索之watch用法详解
- CodeForces 867E Buy Low Sell High
- Struts2之HelloWolrd
- 统计单词(包括中文)出现的次数
- 使用Jsp输出Person类的成员信息(无数据库连接)
- display内联属性