猫史档案馆


【Python作品分享】一个极其普通的计算器1.7【作品秀】

用户:沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ查看:2 回复:2 评论:2 创建时间:2020-03-28T20:25:12


【作品展示】

center_image

 

【作品介绍】

这是这一个计算器最后一次更新了。我的心情是沉重的。

这次修复了一些已知的bug。

大家拿去用吧!尽情的享受这最后一次更新吧!

下次计算器我会用tkinter重写一遍。大家可以等好久好久(滑稽)

 

【作品源代码】

import easygui
import turtle
import math
p=turtle.Pen()
pen=turtle.Pen()
s=turtle.Screen()
s.setup(1000,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)
    #print(num)
    for i in range(len(num)):
        if ('!' in num [i]):#阶乘运算
            num[i] = str(math.factorial(int(num[i].split ('!')[0])))
            #print(num)
        if ('√' in num [i]):#平方根运算
            num[i] = str(math.sqrt(float(num[i].split('√')[-1])))
            #print(num)
        if num[i]=='圆周率'or num[i]=='pi':#转化
            num[i]=str(math.pi)
        if num[i]=='e'or num[i]=='E':#转化
            num[i]=str(math.e)
        if ('sin' in num[i] and (not 'a' in num[i])): #三角函数运算           
            if '°'in num[i]:#角度
                num[i] = num[i].split('sin')[-1]
                # easygui.msgbox(str(num[i]))
                num[i] = num[i].split('°')[0]
                # easygui.msgbox(str(num[i]))
                num[i] = float(num[i])
                num[i] = str(math.sin(math.radians(num[i])))
            else:#弧度
                num[i] = num[i].split('sin')[-1]
                num[i]=float(num[i])
                num[i] = str(math.sin(num[i]))
            #break
        if ('cos' in num[i] and (not 'a' in num[i])):  
            if '°'in num[i]:
                num[i] = num[i].split('cos')[-1]
                # easygui.msgbox(str(num[i]))
                num[i] = num[i].split('°')[0]
                # easygui.msgbox(str(num[i]))
                num[i] = float(num[i])
                num[i] = str(math.cos(math.radians(num[i])))
            else:
                num[i] = num[i].split('cos')[-1]
                num[i] = float(num[i])
                num[i] = str(math.cos(num[i]))
            #break
        if ('tan' in num[i] and (not ('a' in num[i]))): 
            easygui.msgbox(num[i])
            if '°'in num[i]:
                easygui.msgbox('du')
                num[i] = num[i].split('tan')[-1]
                easygui.msgbox(str(num[i]))
                num[i] = num[i].split('°')[0]
                easygui.msgbox(str(num[i]))
                num[i] = float(num[i])
                num[i] = str(math.tan(math.radians(num[i])))
            else:
                easygui.msgbox('h')
                num[i] = num[i].split('tan')[-1]
                easygui.msgbox(str(num[i]))
                num[i] = float(num[i])
                num[i] = str(math.tan(num[i]))
            #break
        if 'asin' in num[i]:                        #反三角函数运算  
            num[i] = num[i].split('asin')[-1]
            num[i] = float(num[i])
            num[i] = str(math.asin(num[i]))
            #break
        if 'acos' in num[i]:
            num[i] = num[i].split('acos')[-1]
            num[i] = float(num[i])
            num[i] = str(math.acos(num[i]))
            #break
        if 'atan' in num[i]:
            num[i] = num[i].split('atan')[-1]
            num[i] = float(num[i])
            num[i] = str(math.atan(num[i])) 
            #break
    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(-450, 260)
pen.write('输入“数字加符号加数字”以此类推进行运算(符号为+,-,*,/,^,!,√等)', font=('Arial', 20, 'normal'))
pen.goto(-130, 220)
pen.write('第一个数不能是负数', font=('Arial', 20, 'normal'))
pen.goto(-145, 180)
pen.write('鼠标模式:戳“exit”退出', font=('Arial', 20, 'normal'))  # 提示
pen.goto(-125,140)
pen.write('键盘模式:按cansel', font=('Arial', 20, 'normal'))
pen.hideturtle()
def main():#打包主函数
    a = ''
    b = ''
    p.goto(-450,0)
    p.clear()
    while True:
        #try:
            if easygui.ynbox('要用鼠标输入还是键盘输入(默认键盘)','超级喵计算器',('鼠标','键盘')) :#输入模式选择       
                while b!= '=':
                    b=easygui.choicebox('输入算式','超级喵计算器',('e','pi','sin','cos','tan','asin','acos','atan','exit','.','°','0','1','2','3','4','5','6','7','8','9','+','-','*','/','^','!','√','=','<-|x|','all_clear'))
                    if 'exit' in b:  # 退出
                        quit()
                        #return 'exit'
                    if b != '<-|x|' and b != 'all_clear':  # ↑输入内容
                        a += b  # ←保存输入
                        p.write(b, font=('Arial', 90, 'normal'))
                        if b=='sin'or b=='cos'or b=='tan':
                            p.forward(180)
                        elif b=='asin'or b=='acos'or b=='atan':#对不同长度的输入做不同的处理
                            p.forward(250)    
                        else:
                            p.forward(100)
                    elif b == 'all_clear':
                        a = ''#清空
                        b = ''
                        p.goto(-450, 0)
                        p.clear()
                        continue
                        #输入算式
                        
                    else:
                        p.undo()
                        p.undo()#回删一个
                        a=''.join(list(a)[0:len(a)-1])
                    
                    if p.xcor() >= 450:
                        p.goto(-450,p.ycor()-100)
                    
                    # easygui.msgbox(a)
                a = a.split('=')[0]
            else:
                a = turtle.textinput('超级喵计算器','输入算式')
                if a is None:
                    quit()
                    #return 'exit'
            p.write(calculator(chaifen(a)), font=('Arial', 100, 'normal'))
            easygui.msgbox('喵进行下一次计算','超级喵计算器','快喵')
            p.clear()#重置
            p.goto(-450, 0)
            a = ''
            b = ''
        # except ValueError:
        #     easygui.msgbox('看提示!不要乱加减符号和数字!不要输入字母等无关内容!','超级喵计算器')#输入有误
        #     main()         #输入错误后重新递归一次,进行下一次计算
                
        # except ZeroDivisionError:
        #     easygui.msgbox('不能除以0','超级喵计算器') 
        #     main()      
        # except IndexError: 
        #     easygui.msgbox('不能空打', '超级喵计算器')
        #     main()

        # except TypeError:
        #     easygui.msgbox('谁tm让你点×的?!', '超级喵计算器')
        #     main()
                
        # except OverflowError:
        #     easygui.msgbox('太大了!!!', '超级喵计算器')
        #     main()

main()  # 调用

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
盒子的box盒子的box

emotion_编程猫_点赞

点赞2


评论


ACTACT

我想跟着你学

 

点赞1


评论