用户:
沉著的飞叶龙uZWJ查看:0 回复:0 评论:0 创建时间:2020-03-12T22:52:13
【作品展示】

【作品介绍】
没错,就是一个极其普通的计算器。功能可能还没你系统的好。
但!这是作者在顶着空中课堂和作业(换个作者可能会有钉钉,我是腾讯课堂)的情况下完成的!
能用就可以!
♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂♂
【作品源代码】
def chaifen(suanshi):
#拆分程序
canshu = suanshi
number = []
fuhao = []
for i in canshu:#对五种符号进行不同的处理
if (i == '+'):
canshu = canshu.split('+', 1)
fuhao.append('+')
number.append(canshu[0])
break
if (i == '-'):
canshu = canshu.split('-', 1)
fuhao.append('-')
number.append(canshu[0])
break
if (i == '*'):
canshu = canshu.split('*', 1)
fuhao.append('*')
number.append(canshu[0])
break
if (i == '/'):
canshu = canshu.split('/', 1)
fuhao.append('/')
number.append(canshu[0])
break
if (i == '^'):
canshu = canshu.split('^', 1)
fuhao.append('^')
number.append(canshu[0])
break
number.append(canshu[-1])
return number, fuhao#返回结果
#这是只能两个数的符号运算,任意个数的有些难,作者办不到(作者试过递归,但失败了)
def calculator(chaifen_ed_suanshi):#运算部分
num,f=chaifen_ed_suanshi
for i in range(len(num)):
num[i] = float(num[i])#转化成数字
result = num[0]
if f==['+']:
result+=num[1]
if f == ['-']:
result -= num[1]
if f==['*']:
result*=num[1]
if f == ['/']:
result /= num[1]
if f == ['^']:
result **= num[1]
#对五种符号分类运算↑
if '.' == list(str(result))[-2] and '0' == list(str(result))[-1]:#把类似1.0,15.0的结果的末尾去掉,再返回结果
return str(result).split('.0')[0]
else:
return result#正常返回结果
print('欢迎使用“超级喵”计算器,使用提示如下:↓')
print('输入“数字加符号加数字”进行运算(符号为+,-,*,/,^)')
print('第一个数不能是负数')
print('空打直接退出')#提示
while True:
try:
a = input('输入算式')
print(calculator(chaifen(a)))#输入算式
except ValueError:
print('看提示!不要乱加减符号和数字!不要输入字母等无关内容!')#输入有误
except IndexError:#退出
break
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn