用户:
沉著的飞叶龙uZWJ查看:2 回复:1 评论:2 创建时间:2020-03-16T13:23:19
【作品展示】

【作品介绍】
(记住,以后每次更新都是这张哲学图)调整了排版格式,修复了一些bug,修改了提示的位置,增加了“不能除以零”的报错提示。
希望你们喜欢!作者还会继续努力的!
【作品源代码】
import easygui
import turtle
p=turtle.Pen()
pen=turtle.Pen()
s=turtle.Screen()
s.setup(700,700)
p.penup()
pen.penup()
turtle.screensize(10000,10000)#准备
def chaifen(suanshi):
# 拆分程序
count = 0
canshu = suanshi
number = []
fuhao = []
# 对五种符号进行不同的处理
for i in range(len(canshu)):
if (canshu[i] == '+'):
canshu = canshu.split('+', 1)
fuhao.append('+')
number.append(canshu[count])
count += 1
# print(fuhao)
break
if (canshu[i] == '-'):
canshu = canshu.split('-', 1)
fuhao.append('-')
number.append(canshu[count])
count += 1
# print(fuhao)
break
if (canshu[i] == '*'):
canshu = canshu.split('*', 1)
fuhao.append('*')
number.append(canshu[count])
count += 1
# print(fuhao)
break
if (canshu[i] == '/'):
canshu = canshu.split('/', 1)
fuhao.append('/')
number.append(canshu[count])
count += 1
# print(fuhao)
break
if (canshu[i] == '^'):
canshu = canshu.split('^', 1)
fuhao.append('^')
number.append(canshu[count])
count += 1
# print(fuhao)
break
if ((('+' in canshu[-1]) or ('-' in canshu[-1]) or ('*' in canshu[-1]) or ('/' in canshu[-1]) or ('^' in canshu[-1])) and (('0' in canshu[-1]) or ('1' in canshu[-1]) or ('2' in canshu[-1]) or ('3' in canshu[-1]) or ('4' in canshu[-1]) or ('5' in canshu[-1]) or ('6' in canshu[-1]) or ('7' in canshu[-1]) or ('8' in canshu[-1]) or ('9' in canshu[-1]))):
n1, f1 = chaifen(canshu[-1])#一次拆分还不够
return number + n1, fuhao + f1#递归拆分
else: #↑必须把之前的数据也加上!
number.append(canshu[-1])
return number, fuhao # 返回结果
# def fan(l):
# a = ''
# i1 = (len(l) - 1)
# for __count in range(len(l)):
# a = (a + l[i1])
# i1 -= 1
# return a
def calculator(chaifen_ed_suanshi): # 运算部分
num,f=chaifen_ed_suanshi
f=''.join(f)
# f=fan(f)
for i in range(len(num)):
num[i] = float(num[i])#转化成数字
result = num[0]
# print(num,result)
for i in range(len(f)):
if f[i]=='+':
result+=num[i+1]
if f[i] == '-':
result -= num[i+1]
if f[i]=='*':
result*=num[i+1]
if f[i] == '/':
result /= num[i+1]
if f[i] == '^':
result **= num[i+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#正常返回结果
pen.goto(-250, 300)
pen.write('欢迎使用“超级喵”计算器,使用提示如下:↓', font=('Arial', 20, 'normal'))
pen.goto(-330, 260)
pen.write('输入“数字加符号加数字”进行运算(符号为+,-,*,/,^)', font=('Arial', 20, 'normal'))
pen.goto(-130, 220)
pen.write('第一个数不能是负数', font=('Arial', 20, 'normal'))
pen.goto(-120, 180)
pen.write('只输入“=”直接退出', font=('Arial', 20, 'normal')) # 提示
pen.hideturtle()
def main():#打包主函数
a = ''
b=''
p.goto(-340,50)
p.clear()
while True:
try:
while b!= '=':
b=easygui.buttonbox('输入算式','“超级喵计算器',('.','0','1','2','3','4','5','6','7','8','9','+','-','*','/','^','='))
a+=b#←保存输入 ↑输入内容
#输入算式
p.write(b , font=('Arial', 100, 'normal'))
p.forward(100)
if p.xcor() >= 300:
p.goto(-340,p.ycor()-100)
# easygui.msgbox(a)
a=a.split('=')[0]
p.write(calculator(chaifen(a)), font=('Arial', 100, 'normal'))
easygui.msgbox('喵进行下一次计算','超级喵计算器','快喵')
p.clear()
p.goto(-340, 50)
a = ''
b = ''
except ValueError:
easygui.msgbox('看提示!不要乱加减符号和数字!不要输入字母等无关内容!','超级喵计算器')#输入有误
main()#输入错误后重新递归一次,进行下一次计算
except IndexError:#退出
break
except ZeroDivisionError:
easygui.msgbox('不能除以0','超级喵计算器')
main()#调用
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn