实例介绍
【实例简介】
【实例截图】
【实例截图】

【核心代码】
#!/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)
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论