Python编写计算器范例

#!/usr/bin/env python 
# _*_ coding:utf-8 _*_ 
#作者:想做土匪的书生
#来源地址:http://www.cnblogs.com/mosson/p/5828433.html
#项目名称:python计算器

import re,time 
def compte_add_sub(inp): 
    arg = inp[0]  # 传入的只剩下 加减的字符串为列表 获取第一个值 
    if arg.find("+-") != -1 or arg.find("-+") != -1: 
        arg = arg.replace("+-","-",len(arg)) 
        arg = arg.replace("-+","-",len(arg)) 
    elif arg.find("++") != -1  or arg.find("--") != -1: 
        arg = arg.replace("++","+",len(arg)) 
        arg = arg.replace("--","+",len(arg)) 
    arg = re.findall("[\+\-]|\d+\.?\d*",arg) # 
    if arg[0] == "-": 
        arg[0]=arg[0]+arg[1] 
        del arg[1] <!--more-->
    add_sum = float(arg[0]) 
    for i in range(1,len(arg),2): 
        if arg[i] == "+": 
            add_sum +=float(arg[i+1]) 
        elif arg[i] == "-": 
            add_sum -= float(arg[i+1]) 
    return add_sum 
#处理乘除 
def compute_mul_div(arg): 
    arg = arg[0] 
    end =len(arg) 
    ###########计算平方########## 
    arg = re.sub("(//)","/",arg) 
    #print(arg) 
    while True: 
        x = re.search("\d+\.*\d*(\*\*)\d+\.*\d*",arg) 
        if x: 
            expre = x.group() 
            print(expre) 
            x = x.group().split("**") 
            ret = float(x[0]) ** float(x[-1]) 
            if "e" in str(ret): 
                ret=int(ret) 
            #print(ret) 
            arg = arg.replace(expre,str(ret),1) 
            print(arg) 
        else: 
            #print("exit") 
            break
    ###########计算平方########## 
    li = re.findall(r"[*/]",arg) 
    num = len(li) 
    for i in range(num): 
        new = arg.split(li[i],1) 
        la=re.findall("[\+\-\/]?(\d+\.?\d*)",new[0])[-1] 
        sta = re.findall("([\+\-\/]?\d+\.?\d*)",new[1])[0] 
        if li[i] == "*": 
            new_che = float(la) * float(sta) 
        else: 
            new_che = float(la) / float(sta) 
        arg = new[0][0:-len(la)] + str(new_che) +new[1][len(sta):] 
    arg=[arg,] 
    return compte_add_sub(arg) 
def comput(expression): 
    inp = [expression,] 
    n= compute_mul_div(inp) 
    return n 
def excut(expression): 
    kuohao=[] 
    if expression.find("(") == -1: 
        ret = comput(expression) 
        return ret 
    for i in range(len(expression)): 
        if "(" == expression[i]: 
            kuohao.append(i) 
        elif ")" == expression[i]: 
            ans_kuo = expression[kuohao[-1]+1:i] 
            ret_num = comput(ans_kuo) 
            expression = expression[0:kuohao[-1]]+str(ret_num)+expression[i+1:] 
            return excut(expression) 
def main(): 
    a = '1 - 2 * ((60-30 +(1-40/5*5+3-2*5/3) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))'
    default = '1-40/5*5+3-2*5/3'
    a = default if len(a) == 0 else re.sub("\s*", '', a)  # 三元运算给a默认值,否则 给表达式去除空格 
    ret = excut(a) # 调用excut 方法 并将返回值给ret 
    print("\033[33;1m公式为:\033[0m",a) 
    print("\033[35;1m结果为:\033[0m",ret) 
if __name__ == '__main__': 
    start = time.clock() 
    main() 
    end = time.clock() 
    print("read: %f s" % (end - start))