猫史档案馆


【Python作品分享】一个极其普通的计算器求助【求助帖】

用户:沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ查看:0 回复:0 评论:0 创建时间:2020-03-18T15:16:37


【作品展示】

center_image

 

【作品介绍】

完整代码看这里,求助内容看“计算器求助消息”

 

【作品源代码】

import easygui
import turtle
import math
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)):
    #     if ('!' in num [i]):
    #         num[i] = math.factorial(float(num[i].split ('!')[-1])
    #     elif ('√' in num [i]):
    #         num[i] = math.sqrt(float(num[i].split('√'))[-1])
    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(-340, 260)
pen.write('输入“数字加符号加数字”以此类推进行运算(符号为+,-,*,/,^,!,√)', font=('Arial', 20, 'normal'))
pen.goto(-130, 220)
pen.write('第一个数不能是负数', font=('Arial', 20, 'normal'))
pen.goto(-290, 180)
pen.write('鼠标模式:戳“exit”退出(报错过几次就得戳几次)', font=('Arial', 20, 'normal'))  # 提示
pen.goto(-340,140)
pen.write('键盘模式:按cansel(报错次数-1 2次以上,以下的话次数与报错次数相同)', font=('Arial', 20, 'normal'))
pen.goto(-200,100)
pen.write('总之,狂按退出就对了(滑稽)', font=('Arial', 20, 'normal'))
pen.hideturtle()
def main():#打包主函数
    a = ''
    b = ''
    p.goto(-340,-50)
    p.clear()
    while True:
        try:
            if easygui.ynbox('要用鼠标输入还是键盘输入','超级喵计算器',('鼠标','键盘')):       
                while b!= '=':
                    b=easygui.buttonbox('输入算式','超级喵计算器',('exit','.','0','1','2','3','4','5','6','7','8','9','+','-','*','/','^','!','√','=','<-|x|','all_clear'))
                    if 'exit' in b:  # 退出
                        return 'exit'
                    if b != '<-|x|' and b != 'all_clear':  # ↑输入内容
                        a += b  # ←保存输入
                        p.write(b, font=('Arial', 100, 'normal'))
                        p.forward(100)#正常输入
                    elif b == 'all_clear':
                        a = ''#清空
                        b = ''
                        p.goto(-340, -50)
                        p.clear()
                        continue
                        #输入算式
                        
                    else:
                        p.undo()
                        p.undo()#回删一个
                        a=''.join(list(a)[0:len(a)-1])
                    
                    if p.xcor() >= 300:
                        p.goto(-340,p.ycor()-100)
                    
                    # easygui.msgbox(a)
                a = a.split('=')[0]
            else:
                a = turtle.textinput('超级喵计算器','输入算式')
                if a is None:
                    return 'exit'
            p.write(calculator(chaifen(a)), font=('Arial', 100, 'normal'))
            easygui.msgbox('喵进行下一次计算','超级喵计算器','快喵')
            p.clear()#重置
            p.goto(-340, -50)
            a = ''
            b = ''
        except ValueError:
            easygui.msgbox('看提示!不要乱加减符号和数字!不要输入字母等无关内容!','超级喵计算器')#输入有误
            if main()=='exit':         #输入错误后重新递归一次,进行下一次计算
                break
        except ZeroDivisionError:
            easygui.msgbox('不能除以0','超级喵计算器') 
            if main() == 'exit':
                break
        except IndexError:
            easygui.msgbox('不能空打', '超级喵计算器')
            if main() == 'exit':
                break
        except TypeError:
            easygui.msgbox('谁tm让你点×的?!', '超级喵计算器')
            if main() == 'exit':
                break
        except OverflowError:
            easygui.msgbox('太大了!!!', '超级喵计算器')
            if main() == 'exit':
                break


main()  # 调用

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页