一个计算器小程序
需求
代码实现
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author : QiuMeng
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)