zoukankan      html  css  js  c++  java
  • 一个简易计算器代码

    import re
    def basic_cal(cal_list):
    def del_ex(temp,ind):
    cal_list[ind-1]=temp
    del cal_list[ind],cal_list[ind]
    def mul_div(c_list):
    while True:
    for index,args in enumerate(c_list):
    if args=="*":
    args_temp=float(c_list[index-1])*float(c_list[index+1])
    del_ex(args_temp,index)
    break
    elif args=="/":
    args_temp=float(c_list[index-1])/float(c_list[index+1])
    del_ex(args_temp,index)
    break
    if "*" not in c_list and "/" not in c_list:
    return c_list
    def plu_min(c_list):
    while True:
    for index,args in enumerate(c_list):
    if args=="+":
    args_temp=float(c_list[index-1])+float(c_list[index+1])
    del_ex(args_temp,index)
    break
    elif args=="-":
    args_temp=float(c_list[index-1])-float(c_list[index+1])
    del_ex(args_temp,index)
    break
    if "+" not in c_list and "-" not in c_list:
    return c_list
    if cal_list[0]=="-":
    cal_list[0]+=cal_list[1]
    del cal_list[1]
    cal_list=mul_div(cal_list)
    cal_list=plu_min(cal_list)
    return cal_list


    y="((2+3*5)-4/25*(-25-36))/5*23+22551*(5/2+6.56*5.25)/5*25-(88+22)"
    y1=y
    while True:
    if "(" in y:
    y_bra=re.search(r"([d+-/*]+)",y).group()
    else:
    y_bra=y
    if re.search("[+*/-]-[d.]+",y_bra):
    s_min=re.search("[+*/-]-[d.]+",y_bra).group()
    y_bra=y_bra.replace(s_min,s_min[0]+"000")
    y=y.replace(s_min,s_min[0]+"000")
    y_list=re.findall("[d.]+|+|-|/|*",y_bra)
    y_list[y_list.index("000")]=s_min[1:len(s_min)]
    else:
    y_list=re.findall("[d.]+|+|-|/|*",y_bra)
    y_list=basic_cal(y_list)
    y=y.replace(y_bra,str(y_list[0]))
    if y==str(y_list[0]):
    break
    print("the answer is",y_list,eval(y1))
  • 相关阅读:
    例解 Linux 下 Make 命令
    linux使用bin文件安装jdk
    Linux查看及设置系统字符集
    FTP的两种主动模式和被动模式
    Mongodb之主从复制
    Nginx配置认证登录
    AWK
    Redis+Keepalived实现高可用
    Redis哨兵配置
    Keepalived指定文件接收日志
  • 原文地址:https://www.cnblogs.com/phoenix-mountain/p/13047552.html
Copyright © 2011-2022 走看看