python 实现计算器功能 输入字符串,输出相应结果
import re
formul='1 - 2 *( (6 0- 30+(0-40/5) * (9-2* 5/3 +7 /3*99/4*2998 +10 *568/14)) - (-4*3) / (16-3*2))'
def zhengli(func):
func = func.replace("+-","-").replace("--","+")
return func
def addjian(func):
func = func.strip("()")
ret = re.findall("-?\d*\.?\d+",func)
sum=
for i in ret:
sum +=float(i)
return str(sum)
def chengchu(func):
func = func.strip("()")
while("/"in func or "*" in func):
ret = re.search('\d*\.?\d+[\*/]-?\d*\.?\d',func).group()
digital = re.split('([\*/])',ret) #加个括号是为了优先级的原因,保留非数字
result = str(float(digital[])*float(digital[])) if digital[] == '*' else str(float(digital[])/float(digital[]))
func = func.replace(ret,result,)
func = zhengli(func)
return func def comput(formal):
formal = re.sub('\s','',formal)
while(re.search('\([^()]*\)',formal)):
print(formal)
ret = re.findall('\([^()]*\)',formal)
for i in ret:
print("wu kuohao:"+i)
result_chengchu = chengchu(i) if '*'in i or '/' in i else i#先算乘除
result_addjian = addjian(result_chengchu) if '+'in result_chengchu or '-' in result_chengchu else result_chengchu#再算加减
formal = formal.replace(i,result_addjian,)
formal = zhengli(formal)
else:
result_chengchu = chengchu(formal) if '*' in formal or '/' in formal else formal # 先算乘除
result_addjian = addjian(result_chengchu) if '+' in result_chengchu or '-' in result_chengchu else result_chengchu # 再算加减
return result_addjian print(comput(formul))
python 实现计算器功能 输入字符串,输出相应结果的更多相关文章
- 2. Python中的基本输入、输出、格式化
本文利用的是Python 3.x版本,建议学习3.x版本 Python中的基本输入.输出.格式化 1. 输入 使用input([prompt])读取一行,将其转换为string类型并返回,input的 ...
- Python基础二(输入与输出)
通常,一个程序都会有输入/输出,这样可以与用户进行交互.用户输入一些信息,你会对他输入的内容进行一些适当的操作,然后再输出给用户想要的结果.Python的输入/输出,我们可以用input进行输入,pr ...
- Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化
一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...
- 小白学Python(3)——输入和输出,显示你的名字
任何计算机程序都是为了执行一个特定的任务,有了输入,用户才能告诉计算机程序所需的信息,有了输出,程序运行后才能告诉用户任务的结果. 输入是Input,输出是Output,因此,我们把输入输出统称为In ...
- 利用PYTHON设计计算器功能
通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ))- (-4*3 ...
- Python系列教程(三):输入和输出
1.1 raw_input() 在Python中,获取键盘输入的数据的方法是采用 raw_input 函数(至于什么是函数,咱们以后的章节中讲解),那么这个 raw_input 怎么用呢? 看如下示例 ...
- python语言学习5——输入和输出
输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字. 注意点: 字符串用的是单引号 碰到逗号输出时就会输出一个空格 输入 python提供了一个input(),可以让用户输入一个字 ...
- Python+OpenCV4:读写输入和输出的简单实践(图片、视频、摄像头)
典型的文件处理流程如下: 利用命令行参数 sys.argv 命令行参数是读取文件时常用的方式. 命令行参数保存在 sys.argv 的列表中,列表的第一个元素是脚本名称,后面的元素是命令行参数: 通过 ...
- 【python】文件的输入和输出
1.os模块 2.os.path 模块 3.实例 1. os模块 对文件系统的访问大多通过python的os模块实现,其中os 模块负责大部分的文件系统操作,包括删除/重命名文件,遍历目录树,管理文件 ...
随机推荐
- ctypes to load library in c/c++
cdll.LoadLibrary(...) restype (default is c_int) argtypes (what's the default? c_int?) customized da ...
- oracle tps
http://blog.csdn.net/nilxin/article/details/5812480 sample 1: 定义 TPS:Transactions Per Second(每秒传输的事物 ...
- 2个rman自动恢复的脚本
### scripts 1--the scirpt is used for restore db from vcs to a point to time recovery--and the targe ...
- python学习之j进程和线程:
每个进程至少有一个线程,python因为每个线程都共用一个GIL全局锁(同时只能运行一个线程),所以不能用多线程(除非重新写C解释器),但是多进程的GIL锁各自独立可多进程. 进程与线程的区别在于一个 ...
- gdb手册
摘自:https://github.com/hellogcc/100-gdb-tips/blob/master/src/quit-gdb-silently.md. 我只是摘抄我平时没注意到的,或者我认 ...
- .Net魔兽登录页面
一,页面部分展示 二.代码展示 1.登录页面: public partial class FrmLogin : Form { public FrmLogin() { InitializeCompone ...
- go实现生产者消费者
package main import ( "fmt" "math/rand" ) func main() { ch := make(chan int) don ...
- postgresql 存储过程动态更新数据
-- 目标:动态更新表中数据 -- 老规矩上代码-----------------------------tablename 表名--feildname 字段名数组--feildvalue 字段值数组 ...
- SQL Server 2008添加字段成功,但提示列名无效
在sql后查询,给现有表添加一个字段,即执行sql语句: alter table [sxrq_1108].[dbo].[公司周报表详情] add 总计 int default 0 然后在上述sql查语 ...
- uvm_pkg——老板,打包带走
Thus spake the master programmer: “After three day without programming, life becomes meaningless.” 编 ...