7_python之路之python计算器
7_python之路之python计算器
1.程序说明:Readme.cmd
1.程序文件及说明:
calculator.py 2.python版本:python-3.5.3 3.程序使用:python calculator.py "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )"
【使用双引号引住需要运算的内容】 4.程序功能解析:
计算器 5.程序执行结果:请亲自动手执行或者查看我的博客 6.程序博客地址:http://www.cnblogs.com/chenjw-note/p/8523839.html
2.程序代码:calculator.py
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# author:chenjianwen
# email:1071179133@qq.com
import re
import sys ##计算乘除法
def compute_mutiply_and_dividend(formula):
operators = re.findall(r'[*/]',formula)
calc_list = re.split('[*/]',formula)
calc_res = None
for index,i in enumerate(calc_list):
if calc_res:
if operators[index - 1] == "*":
calc_res *= float(i)
elif operators[index - 1] == "/":
calc_res /= float(i)
else:
calc_res = float(i)
return calc_res ##计算加减
def compute_add_and_subt(formula):
operators = re.findall(r'[+-]',formula)
calc_list = re.split(r'[+-]',formula)
calc_res = None
for index,i in enumerate(calc_list):
if calc_res:
if operators[index - 1] == "+":
calc_res += float(i)
elif operators[index - 1] == "-":
calc_res -= float(i)
else:
calc_res = float(i)
return calc_res ##消除两个符号
def remove(formula):
formula = formula.replace("+-","-")
formula = formula.replace("-+", "-")
formula = formula.replace("++", "+")
formula = formula.replace("--", "-")
formula = formula.replace("- -", "+")
return formula ##处理特殊情况1 - 2 * -14969036.7968254 ['-', '-'] ['1 ', ' 2 * ', '14969036.7968254'] ' 2 * '
def special_done(a,b):
for index,i in enumerate(b):
i = i.strip()
if i.endswith("*") or i.endswith("/"):
b[index] = b[index] + a[index] + b[index + 1]
del b[index + 1]
del a[index]
return a,b #具体运算
def compute(formula):
#print(formula)
formula = formula.strip('()')
a = re.findall(r'[+-]',formula)
b = re.split(r'[+-]',formula)
if len(b[0].strip()) == 0: ##判断如果第一个是减号,值为空了无法运算,将其减号赋下一个值
b[1] = a[0] + b[1]
del a[0]
del b[0]
#print(a)
#print(b)
a,b = special_done(a,b)
for index,i in enumerate(b):
if re.search(r'[*/]',i):
res = compute_mutiply_and_dividend(i)
b[index] = res
#print(b)
##开始运算+ -
total_res = None
for index,i in enumerate(b):
if total_res:
if a[index - 1] == "+":
total_res += float(i)
elif a[index - 1] == "-":
total_res -= float(i)
else:
total_res = float(i)
#print("\033[32;1m[%s]运算结果:\033[0m" % formula, total_res)
return total_res #主逻辑
def calc(formula):
calc_status = True
calc_res = None
while calc_status:
m = re.search(r'\([^()]*\)',formula)
if m:
sub_res = compute(m.group())
formula = formula.replace(m.group(),str(sub_res))
formula = remove(formula)
#print(formula)
else:
#print('\033[41;1m----没拓号了...---\033[0m') print('\n\n\033[42;1m最终结果:\033[0m', compute(formula))
calc_status = False
print("计算结束") calc(sys.argv[1])
print("eval的计算结果:",eval(sys.argv[1]))
3.程序执行输出

7_python之路之python计算器的更多相关文章
- Python之路,Day6 - Python基础6
		
本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...
 - python 计算器的(正则匹配+递归)
		
经过2天的长时间的战斗,python计算器终于完成了. import re val="1-2*((60-30*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3 ...
 - Python之路,Day4 - Python基础4 (new版)
		
Python之路,Day4 - Python基础4 (new版) 本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...
 - python开发之路:python数据类型(老王版)
		
python开发之路:python数据类型 你辞职当了某类似微博的社交网站的底层python开发主管,官还算高. 一次老板让你编写一个登陆的程序.咔嚓,编出来了.执行一看,我的妈,报错? 这次你又让媳 ...
 - Python计算器实操
		
要求: 开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * ...
 - 作业1开发一个简单的python计算器
		
开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...
 - 老男孩python作业5-开发一个简单的python计算器
		
开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...
 - (路-莫)-Python基础一
		
一,Python介绍 1,python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打 ...
 - Python菜鸟之路:Python基础-线程、进程、协程
		
上节内容,简单的介绍了线程和进程,并且介绍了Python中的GIL机制.本节详细介绍线程.进程以及协程的概念及实现. 线程 基本使用 方法1: 创建一个threading.Thread对象,在它的初始 ...
 
随机推荐
- 还是 js 替代 vw vh 了
			
有个需求是要层叠两张图,就像你现在看到的:整个浏览器和html页面,内层图片要水平居中,等比例与源UI图适配不同设备 本来很简单的使用 vw vh了( 核心代码 top: 13 ...
 - 解决 android.support.v7.widget.GridLayout 使用 xmlns:app 出现 error 的问题
			
GridLayout 是在 Android API Level 14 加进来的 它可用来取代 TableLayout 也提供了自由度较大且实用的排版功能 为了兼容 4.0 以下的较低版本 Androi ...
 - Java Spring-传统AOP开发
			
2017-11-10 17:25:48 Spring中通知Advice类型(增强代码): 前置通知,org.springframework.aop.MethodBeforeAdvice:方法前 后置通 ...
 - How To Use Amazon MWS To Download Unshipped Order Reports
			
文章来源:http://www.samswiches.com/2011/02/how-to-use-amazon-mws-to-download-unshipped-order-reports/ ac ...
 - 自行申请德国的VAT号码?
			
我在香港/大陆地区,是否可以自行申请德国的VAT号码? 德国联邦税务局按照不同国家申请人划分成不同申请办公室,以下为德国联邦税务局负责中国境内申请人的办公室地址及联络方式: FINANZAMT BER ...
 - 快速使用CSS 弹性盒子
			
布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现:2009年,W3C提出了一种新的方案 ...
 - poj1087最大流拆点
			
刚开始看这题太长了就放着,后来做了之后才发现并不难,就是构造图有点麻烦 一开始写了180行@.@结果tle了,后来想到用map直接访问的话可能会快点,就不用每次循环了 #include<map& ...
 - Assert 类
			
Assert 类 使用 true/false 命题验证单元测试中的条件. 继承层次结构 System.Object Microsoft.VisualStudio.TestTools.Uni ...
 - HTML <select> 标签
			
定义和用法 select 元素可创建单选或多选菜单. <select&> 元素中的 <option> 标签用于定义列表中的可用选项. HTML 4.01 与 HTML ...
 - EPANET头文件解读系列4——EPANET2.H
			
该头文件的功能与系列3中的TOOLKIT.H类似,而且内容也几乎一致,所以也就不再详细介绍.