python 实现一个计算器功能
#s = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
#第一步 分步实现 ("2-1*-22+3-10/-5")
# 1. 实现一个乘除法 两两相乘/相除
# 2. 实现一个加减法 两两相加/相减
# 3. 把计算结果 替换原来的表达式
# 4. 替换完成后 处理整体表达式的符号
# 5. 五个函数: 计算atom_cal() format() mul_div() add_sub() cal()
#第二步 去括号 计算括号内的 import re def format(exp):
exp = exp.replace("-+","-")
exp = exp.replace("+-","-")
exp = exp.replace("--","+")
exp = exp.replace("++","+")
return exp def atom_cal(exp):
if "*" in exp:
a,b = exp.split("*")
return str(float(a) * float(b)) elif "/" in exp:
a, b = exp.split("/")
return str(float(a) / float(b)) def mul_div(exp):
while True:
ret = re.search("\d+(\.\d+)?[*/]-?\d+(\.\d+)?",exp) #拿到乘除表达式 #从左到右拿结果,拿不到返回None
if ret:
atom_exp = ret.group()
res = atom_cal(atom_exp)
# print(atom_exp,res)
exp = exp.replace(atom_exp,res)
else:
return exp def add_sub(exp):
ret = re.findall("[+-]?\d+(?:\.\d+)?", exp) # 取出数字和数字前面的符号 findall返回的是一个列表 查找所有项
exp_sum = 0
for i in ret:
exp_sum = exp_sum + float(i) # 取出来的是字符串
return exp_sum def cal(exp):
exp = mul_div(exp)
exp = format(exp)
exp_sum = add_sub(exp)
return exp_sum def main(exp):
exp = exp.replace(" ","")
while True:
ret = re.search("\([^()]+\)",exp)
if ret:
inner_bracket = ret.group()
res = str(cal(inner_bracket))
exp = exp.replace(inner_bracket,res)
exp = format(exp)
else:break
return cal(exp)
s = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
ret = main(s)
print(ret)
python 实现一个计算器功能的更多相关文章
- 如何用Python写一个计算器软件 附带效果图
该计算器使用Python tkinter模块开发 效果如下图 import tkinter #导入tkinter模块 root = tkinter.Tk() root.minsize(280,500 ...
- Python+Tkinter 实现计算器功能
#=================================================================================== import tkinter ...
- 用python编写一个计算器
# 1 - 2 * ((60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2)))# 通过Pyt ...
- C# 一个计算器功能实现引发的思考
一.需求 计算器功能需求,这个众所周知,很明确了. 二.步骤分析 1)初级实现计算器 static int Calculator(int a,int b,string str) { switch(st ...
- python造一个计算器
正则表达式之简易计算器 关注公众号"轻松学编程"了解更多. 需求:使用正则表达式完成一个简易计算器. 功能:能够计算简单的表达式. 如:12((1+2)/(2+3)+1)*5.1- ...
- Python定做一个计算器,小而美哒~
使用qt designer ,按装anaconda后,在如下路径找到: conda3.05\Library\bin designer.exe文件,双击启动: 创建窗体,命名为XiaoDing,整个 ...
- 用python实现一个计算器
import re def atom_cal(exp): # 计算乘除法 if '*' in exp: a,b = exp.split('*') return str(float(a) * float ...
- 完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能
#!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能'''try: a=int(raw_input(" ...
- 利用PYTHON设计计算器功能
通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ))- (-4*3 ...
随机推荐
- 【剑指Offer学习】【面试题4 : 替换空格】
题目: 请实现一个函数,把字符串中的每个空格替换成"%20",例如“We are happy.”,则输出“We%20are%20happy.”. 以下代码都是通过PHP代码实现. ...
- WPF GridSplitter最好设置HorizontalAlignment和VerticalAlignment,否则不可以左右移动
<Window x:Class="XamlTest.Window5" xmlns="http://schemas.microsoft.com/winf ...
- C#并口热敏小票打印机打印位图
原文:C#并口热敏小票打印机打印位图 最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: class ...
- BGP的一网双平面规划
网络拓扑: XRV1 ===================================================================== # sysname XRV1# boa ...
- Kinect 开发驱动配置
有几种配置方案 1.openNI+SensorKinect+PCL 的开发环境(pcl 标配) http://blog.csdn.net/chenxin_130/article/details/669 ...
- C# 生成txt日志文件
/// <summary> /// 创建日志文件,每天一个 /// </summary> /// <param name="logContent"&g ...
- 二叉树基本操作C代码
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct ChainTree) struct ChainTr ...
- 修复VirtualBox "This kernel requires the following features not present on the CPU: pae Unable to boot – please use a kernel appropriate for your CPU"(安装深度Linux的时候就需要)
异常处理汇总-开发工具 http://www.cnblogs.com/dunitian/p/4522988.html 修复VirtualBox "This kernel requires ...
- C# 获取当前月份天数的三种方法总结
方法一: //最有含量的一种 int days = System.Threading.Thread.CurrentThread.CurrentUICulture.Calendar.GetDaysInM ...
- Redis系统管理
EXISTS/DEL exists <key>判断某个key是否存在 del <key>删除某个key *** TYPE/KEYS type <key>获取key的 ...