任性计算器

简单的四则混合运算,加上小括号。

import re
user_input=r'1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
def reslut(in_str):
    #判断字符串内是否有括号
    for i in in_str:
        if i=='(':
            return False
    return True
def replace_str(in_str):
    #去空格
    out_str = in_str.replace(" ", '')
    return out_str
def is_operation(oper):
    #判断是否为计算符号
    if oper == '+' or oper == '-' or oper == '*' or oper == '/':
        return True
    else:
        return False
def my_list(in_str):
    #把数据存入列表中
    list1=[]
    t='' #向list1添加数据
    n=''
    for i in in_str:
        if in_str[0]=='-' and len(list1)==0 and t=='':
            t=t+i
        elif is_operation(i):
            if is_operation(n):
                t=t+i
            else:
                list1.append(t)
                list1.append(i)
                t=''
        else:
            t=t+i
        n=i
    list1.append(t)
    return list1
def calculator_one(in_list):
    if ('*'in in_list) or ('/'in in_list):
        i=0
        out_list=in_list
        for item in in_list:
            if item=='*':
                a=float(in_list[i-1])
                b=float(in_list[i+1])
                out_list[i-1]=str(a*b)
                out_list[i]=''
                out_list[i+1] = ''
                break
            elif item=='/':
                a=float(in_list[i-1])
                b=float(in_list[i+1])
                if b==float(0):
                    print("被除数不能为0")
                    exit()
                out_list[i-1]=str(a/b)
                out_list[i]=''
                out_list[i+1] = ''
                break
            i+=1
        new_list = [x for x in out_list if x != '']
        return calculator_one(new_list)
    else:
        return in_list
def calculator_two(in_list):
    if '+'in in_list or '-' in in_list:
        i=0
        out_list=in_list
        for item in in_list:
            if item=='+':
                a=float(in_list[i-1])
                b=float(in_list[i+1])
                out_list[i-1]=str(a+b)
                out_list[i]=''
                out_list[i+1] = ''
                break
            elif item=='-':
                a=float(in_list[i-1])
                b=float(in_list[i+1])
                out_list[i-1]=str(a-b)
                out_list[i]=''
                out_list[i+1] = ''
                break
            i+=1
        new_list = [x for x in out_list if x != '']
        return  calculator_two(new_list)
    else:
        return in_list
def replace_brackets(in_str):
    #去括号
    if reslut(in_str):
        n=my_list(in_str)
        n=calculator_one(n)
        n=calculator_two(n)
        return n[0]
    else:
        out_str=re.search(r'\([^\(,^\)]*\)',in_str)
        t=out_str.group()
        t = t.replace('(', '')
        t = t.replace(')', '')
        t=my_list(t)
        t=calculator_one(t)
        t=calculator_two(t)
        in_str=re.sub(r'\([^\(,^\)]*\)',t[0],in_str,1)
        return replace_brackets(in_str)
        # in_str.replace(out_str,t)

b=replace_str(user_input)
b=replace_brackets(b)
print(b)
print(eval(user_input))

 

posted @ 2017-05-02 16:20  天行健风行云  阅读(254)  评论(0编辑  收藏  举报