猫史档案馆


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

用户:沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ查看:3 回复:3 评论:3 创建时间:2020-03-13T11:04:27


【作品展示】

center_image

 

【作品介绍】

各位,这个计算器的第二版出来了。

没错,我又更新了。

这次我终于解决了算法的问题,现在可以输入任意长度的算式了,比如1+1+1+1+1+1+1+1+1和69*44/52(但还是只能从左往右依次算,不能区分运算的等级)

祝你们使用愉快!

PS:更新预告:1.2版预计:

1、!阶乘运算

2、gui界面

你们想看我更新哪个,还是一起更新?评论区见。

 

【作品源代码】

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])) 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, f1 + fuhao#递归拆分
    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#正常返回结果
print('欢迎使用“超级喵”计算器,使用提示如下:↓')
print('输入“数字加符号加数字”进行运算(符号为+,-,*,/,^)')
print('第一个数不能是负数')
print('空打直接退出')#提示
while True: 
    try:       
        a = input('输入算式')
        print(calculator(chaifen(a)))#输入算式
    except ValueError:
        print('看提示!不要乱加减符号和数字!不要输入字母等无关内容!')#输入有误
    except IndexError:#退出
        break




 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

***怎么还没有人啊😫>_<

点赞0


评论


wrmdcxywrmdcxy

eval()

点赞0


评论


wrmdcxywrmdcxy

eval()函数可以计算数学表达式

点赞0


评论