【实例简介】

【实例截图】

【核心代码】

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author : QiuMeng

print("欢迎使用计算机")

import re

def replace_func(FormulaChunk):

'''

#去除一些会影响运算的符号

:param FormulaChunk: #传入一个去除括号后的字符串

:return: 去除这些东西后标准的返回

'''

FormulaChunk = FormulaChunk.replace(" ","")

FormulaChunk = FormulaChunk.replace(" -","-")

FormulaChunk = FormulaChunk.replace("--"," ")

return FormulaChunk

def mul_and_div(CombineHandlerList):

'''

计算乘法,除法

:return:返回一个没有任何"*/"字符的列表

'''

ChunkPartitionList = CombineHandlerList[1]

for index,item in enumerate(ChunkPartitionList):

if "*" in item or "/" in item: # 判断这个元素中是否有"*/"

OperatorList = re.findall("[*/]",item) # 分割出乘除号

PartitionList = re.split("[*/]",item) # 根据乘除分割

SumCount = None

for i,j in enumerate(PartitionList):

if SumCount: #第一个元素不计算

if OperatorList[i-1] == "*": # 算乘法

SumCount *= float(j)

elif OperatorList[i-1] == "/": # 算除法

SumCount /= float(j)

else:

SumCount = float(j)

ChunkPartitionList[index] = SumCount

return CombineHandlerList

def add_and_subtract(CombineHandlerListMuledAndDived):

'''

[['-', '-', '-', '-', '-', '-', ' '], ['-9', '2', '5', -6.0, 1.6666666666666667, 80.0, 0.6, 18.0]]

计算加减法

:param CombineHandlerListMuledAndDived: 经过正负聚合且完成乘除的list

:return: 返回一个只有一个float字符串的list

'''

# ['-', '-', '-', '-', '-', '-', ' ']

ChunkOperationList = CombineHandlerListMuledAndDived[0] # 加减操作符列表

# ['-9', '2', '5', -6.0, 1.6666666666666667, 80.0, 0.6, 18.0] # 注意其中的字符串和float

ChunkPartitionList = CombineHandlerListMuledAndDived[1] # 按加减分割后元素列表

SumCount = None

for index,item in enumerate(ChunkPartitionList):

if SumCount:

if ChunkOperationList[index-1] == "-": # 算减法

SumCount -= float(item)

elif ChunkOperationList[index-1] == " ": # 算加法

SumCount = float(item)

else:

SumCount = float(item)

return SumCount

def combine(HandlerList):

'''

将ChunkPartitionList中类似于"2*"和"-" "3"放在一起

:param HandlerList: 已经处理过的两个列表

:return:

'''

ChunkOperationList = HandlerList[0] # 操作符列表

ChunkPartitionList = HandlerList[1] # 按加减分割后元素列表

for index,item in enumerate(ChunkPartitionList):

if item.endswith("/") or item.endswith("*"):

ChunkPartitionList[index] = item ChunkOperationList[index] ChunkPartitionList[index 1] # 合并

del ChunkOperationList[index] # 合并完成后删除对应的元素

del ChunkPartitionList[index 1]

CombineHandlerList = []

CombineHandlerList.insert(0,ChunkOperationList)

CombineHandlerList.insert(1,ChunkPartitionList)

return CombineHandlerList

def handler_list(FormulaChunk):

'''

'(-9-2-5-2*-3-5/3-40*4/2-3/5 6*3)'

将这样的一个括号内的快转化为固定的列表返回

:param UserInput:

:return:

'''

FormulaChunk = re.sub("[()]", "", FormulaChunk) # 去掉字符串前后的括号

FormulaChunk = replace_func(FormulaChunk)

# '-9-2-5-2*-3-5/3-40*4/2-3/5 6*3'

ChunkOperationList = re.findall("[ -]",FormulaChunk) # 获取这个块的所有加减法运算符号来获取一个list

# ['-', '-', '-', '-', '-', '-', '-', '-', ' ']

ChunkPartitionList = re.split("[ -]",FormulaChunk) # 通过加减符号将Chunk分隔开来获取一个list

# ['', '9', '2', '5', '2*', '3', '5/3', '40*4/2', '3/5', '6*3']

if not ChunkPartitionList[0]: #ChunkPartitionList列表第一个是否为空,该块则以 -开始

if ChunkOperationList[0] == "-": # 是负号要加到第二个里面,删除两个列表的第一个元素

ChunkPartitionList[1] = ChunkOperationList[0] ChunkPartitionList[1]

del ChunkPartitionList[0]

del ChunkOperationList[0]

elif ChunkOperationList[0] == " ": # 是正号去除两个列表的第一个元素

del ChunkPartitionList[0]

del ChunkOperationList[0]

# ['-', '-', '-', '-', '-', '-', '-', ' ']

# ['-9', '2', '5', '2*', '3', '5/3', '40*4/2', '3/5', '6*3']

# 这样的列表元素要进行再次处理将"2*"和"-" "3"放在一起

HandlerList = []

HandlerList.insert(0,ChunkOperationList)

HandlerList.insert(1,ChunkPartitionList)

return HandlerList

def calculate(UserInput):

'''

主函数

:param formula:

:return:

'''

# TODO 这块可以使用递归来写

while UserInput: #输入不为空

UserInput = UserInput.replace(" ", "") # 去掉空格

if re.findall("[()]",UserInput): # 是否有括号

FormulaChunk = re.search("\([^()] \)",UserInput).group() # 获取一个括号

# 要把括号中的一些东西给去除掉

HandlerList = handler_list(FormulaChunk) # 按照加减符号进行第一个分割

CombineHandlerList = combine(HandlerList) # 将ChunkPartitionList中类似于"2*"和"-" "3"放在一起后list

CombineHandlerListMuledAndDived = mul_and_div(CombineHandlerList) # 计算完成后乘除法的list

SumCount = add_and_subtract(CombineHandlerListMuledAndDived) # 计算完成加减法后的结果

UserInput = UserInput.replace(FormulaChunk,str(SumCount))

else: # 如果没有的话将其转化成对应的列表格式进行计算

HandlerList = handler_list(UserInput)

CombineHandlerList = combine(HandlerList)

CombineHandlerListMuledAndDived = mul_and_div(CombineHandlerList)

SumCount = add_and_subtract(CombineHandlerListMuledAndDived)

print(SumCount)

break

if __name__ == '__main__':

#formula = "1 - 2 * (30 (60-30 (-9-2- 5-2*-3-5/3-40*4/2-3/5 6*3) * (-9-2-5-2*5/3 7 /3*99/4*2998 10 * 568/14 )) -(-4*3)/ (16-3*2) )"

formula = input(">")

calculate(formula)

python计算器_python 计算器示例(入门级,实现了加减乘除等功能)相关推荐

  1. python做的简单gui计算器_Python计算器–使用Tkinter创建一个简单的GUI计算器

    在Python计算器教程中,您将学习创建简单的GUI计算器.在这篇文章中,我将向您展示如何使用python中的tkinter模块开发一个简单的计算器.因此,让我们进一步创建一个简单的python计算器 ...

  2. python计算器总结_Python 计算器的简单示例

    这篇文章主要为大家详细介绍了Python 计算器的简单示例,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! 简介 在这篇 ...

  3. python写科学计算器代码_Python编程使用tkinter模块实现计算器软件完整代码示例...

    Python编程使用tkinter模块实现计算器软件完整代码示例 来源:中文源码网    浏览: 次    日期:2018年9月2日 Python编程使用tkinter模块实现计算器软件完整代码示例 ...

  4. python整数加法计算器_Python应用实例赏析2.1简单计算

    在日常应用中,我们会经常使用计算器进行计算,有些时候普通计算器的功能不能满足需要,例如使用计算机系统自带的计算器,计算不能超过32位数,手机自带计算器不能超过15位等(我的电脑和手机),也可能计算需要 ...

  5. python数字计算器_Python作为计算器使用(一)——数字

    [摘要]Python作为一种面向对象的动态类型语言,其实用性多种多样,python作为计算器使用就是其中的一种,在很多编程系统中,作为计算器使用都是基础,那么在其中,数字的使用就显得尤为重要,那么今天 ...

  6. python编程计算器_Python设计实现的计算器功能完整实例

    本文实例讲述了Python设计实现的计算器功能.分享给大家供大家参考,具体如下: 通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/ ...

  7. python课程设计计算器_Python设计实现的计算器功能完整实例

    本文实例讲述了Python设计实现的计算器功能.分享给大家供大家参考,具体如下: 通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/ ...

  8. python分数计算器_python算法——方程计算器小工具

    python算法--方程计算器小工具 工具介绍 方程计算器小工具使用python开发,可实现三元以内一次.二次等方程的计算,包含基本计算器的功能.可用于老师.学生.家长等快速验证方程的求解,检查学生作 ...

  9. chatgpt赋能python:Python圆柱体积计算器:简单、高效、快速解决计算难题

    Python圆柱体积计算器:简单.高效.快速解决计算难题 圆柱体积是一个在日常生活.工程学.数学等领域都十分普遍的概念,可以用来计算许多实际问题中的体积,比如容器的容量.建筑材料的用量等等.在本文中, ...

最新文章

  1. 写文章 TEE技术分析【转】
  2. 【深度学习】深入浅出神经网络框架的模型元件(常用层和卷积层)
  3. JavaScript事件循环探索
  4. Windows 目录结构,服务以及端口, DOS常用命令学习
  5. java中udi_Java读取.properties配置文件的方法
  6. 计组之总线:3、总线操作和定时(同步定时、异步定时、版同步通信、分离式通信)
  7. 【C012】Python - 基础教程学习(三)
  8. Flume-ng HDFS sink原理解析
  9. w3c html.css,W3C教程(6):W3C CSS 活动
  10. this version of the Java Runtime only recognizes class file versions up to 52.0
  11. 10.3. TUI (Text User Interface)
  12. 计算机管理无法输入密码,光大网银控件已安装但无法输入密码
  13. 基于深度学习的行人检测技术
  14. 7-19 统计人数(2008慈溪) (100分)
  15. OSG KML文件解析
  16. html字体模糊怎么变清晰,电脑字体模糊怎么办 将字体调节清晰方法【详解】
  17. b3dm ~ ( Batched 3D Model )
  18. 如何将任意辣鸡话题写成一篇优秀的毕业论文——以本文为例
  19. yarn : 无法加载文件 C:\Users\EDY\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本。
  20. [原创内容] 秒变老司机--系统更新安装和集成批处理解决方案[Win7SP1x64简体中文官方镜像专用]1.0...

热门文章

  1. 使用Keras训练历史可视化(含踩雷)
  2. HTTPS加密协议详解
  3. windows下go安装及govendor设置
  4. 透明度测试-AlphaTest
  5. Htc Vive Sdk(OpenVR),Hello World
  6. MSN不能登錄的解決方法
  7. window套接字编程
  8. ASP.NET 数据绑定常用代码
  9. 用C++ RAII思想写windows驱动
  10. 5G的网络架构:Option3 Option3A Option3X