Fork me on GitHub

1.简单计算器

#计算一个表达式的时候,首先肯定是先算括号里面的,再算乘除法,后算加减法
import re
# 1.去括号
def remove_kuohao(expression):
    '''
    这是一个去除括号的函数
    :param expression:  传进来的表达式
    :return: 计算后的结果
    '''
    while True:
        ret = re.search(r'\([^(]*?\)',expression) #先利用正则匹配到式子里面的括号
        if ret: #如果有括号
            re_expression = ret.group()  #先把找到的括号group()一下得到
            # print(re_expression)
            res = jianhua(re_expression.strip('()'))  #括号里面有*/-+都不一定,就又定义了一个简化这个表达式的函数
            # 如果是乘法就执行乘法运算,如果是除法运算就执行除法等,这样的话就直接调用jianhua这个函数了,当然这个式子
            # 有括号,顺便把括号也去了,直接计算去掉括号后的式子
            expression = expression.replace(re_expression,str(res))  #把计算出来的结果放回去:就是把原来的括号里的表达式
                                                                    # 替换成现在计算的结果
            # print(expression)
            continue
        break
    res = jianhua(expression)
    return res
#简化算法
def jianhua(re_expression):
    while True:
        # 匹配乘除法
        ret = re.search(r'\d+\.*?\d*[*/]\-?\d+\.?\d*', re_expression) #利用正则匹配乘除法
        if ret:  # 如果匹配到有*或/就执行下面的代码
            chengchu_expression = ret.group() #就先group一下得到有乘除法的表达式
            res = chengchu(chengchu_expression)  #调用乘除法的函数计算
            re_expression = re_expression.replace(chengchu_expression, str(res))  #吧有乘除法的表达式和计算出来的结果替换
            re_expression =chulifuhao(re_expression)  #在调用处理符号的函数
            # print(re_expression)
            continue
        break
    # 匹配加减法
    num = re.findall(r'[+-]?\d+\.?\d*', re_expression)#在没有乘除法的时候,就让匹配加减法,
    if len(num) > 1:  #如果匹配到的表达式的长度大于1,不过是加法还是减法,都让计算加法。因为float啥结果就是啥
        mynum = 0
        for i in num:
            mynum += float(i)
        return mynum
    else:
        return num[0]
# 3.计算两个数的+-*/
def chengchu(expression):
    if '*' in expression:
        x,y = expression.split('*')
        return float(x)*float(y)  #返回乘法运算的结果 ,让res接收
    if '/' in expression:
        x, y = expression.split('/')
        return float(x)/float(y)   #返回乘法运算的结果 ,让res接收

def chulifuhao(expression):
    if '--' in expression:
       expression = expression.replace('--','+')
    if '++' in expression:
        expression = expression.replace('++','+')
    if '+-' in expression:
       expression = expression.replace('+-','-')
    if '-+' in expression:
        expression = expression.replace('-+','-')
    return expression  #返回结果

cmd = input('请输入你要计算的表达式:>>')
# s = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
new_s = cmd.replace(' ', '') #先将所有的空格去了
# print(new_s)
ret = remove_kuohao(new_s)  #调用去除括号的函数,进行下一步操作
print(ret)

计算器

高级的计算器

import re
from functools import reduce
from tkinter import *
'''处理特殊-号运算'''


def minus_operation(expresstion):
    minus_operators = re.split("-", expresstion)
    calc_list = re.findall("[0-9]", expresstion)
    if minus_operators[0] == "":
        calc_list[0] = '-%s' % calc_list[0]
    res = reduce(lambda x, y: float(x) - float(y), calc_list)
    print(">>>>>>>>>>>>>>减号[%s]运算结果:" % expresstion, res)
    return res

'''reduce()对sequence连续使用function, 如果不给出initial, 则第一次调用传递sequence的两个元素,
以后把前一次调用的结果和sequence的下一个元素传递给function'''

'''处理双运算符号'''


def del_duplicates(expresstion):
    expresstion = expresstion.replace("++", "+")
    expresstion = expresstion.replace("--", "-")
    expresstion = expresstion.replace("+-", "-")
    expresstion = expresstion.replace("--", "+")
    expresstion = expresstion.replace('- -', "+")
    return expresstion
'''*/运算函数'''
def mutiply_dividend(expresstion):
    calc_list = re.split("[*/]", expresstion)  # 用* or /分割公式
    operators = re.findall("[*/]", expresstion)  # 找出所有*和/号
    res = None
    for index, i in enumerate(calc_list):
        if res:
            if operators[index - 1] == '*':
                res *= float(i)
            elif operators[index - 1] == '/':
                res /= float(i)
        else:
            res = float(i)
    procession0 = "[%s]运算结果=" % expresstion, res
    # final_result.insert(END, procession0)  # 插入窗体
    print(procession0)
    return res

'''处理运算符号顺序混乱情况'''
def special_features(plus_and_minus_operators, multiply_and_dividend):
    for index, i in enumerate(multiply_and_dividend):
        i = i.strip()
        if i.endswith("*") or i.endswith("/"):
            multiply_and_dividend[index] = multiply_and_dividend[index] + plus_and_minus_operators[index] + multiply_and_dividend[index + 1]
            del multiply_and_dividend[index + 1]
            del plus_and_minus_operators[index]
        return plus_and_minus_operators, multiply_and_dividend

def minus_special(operator_list, calc_list):
    for index, i in enumerate(calc_list):
        if i == '':
            calc_list[index + 1] = i + calc_list[index + 1].strip()
'''运算除了()的公式+-*/'''
def figure_up(expresstion):
    expresstion = expresstion.strip("()")  # 去掉外面括号
    expresstion = del_duplicates(expresstion)  # 去掉重复+-号
    plus_and_minus_operators = re.findall("[+-]", expresstion)
    multiply_and_dividend = re.split("[+-]", expresstion)
    if len(multiply_and_dividend[0].strip()) == 0:
        multiply_and_dividend[1] = plus_and_minus_operators[0] + multiply_and_dividend[1]
        del multiply_and_dividend[0]
        del plus_and_minus_operators[0]
    plus_and_minus_operators, multiply_and_dividend = special_features(plus_and_minus_operators, multiply_and_dividend)
    for index, i in enumerate(multiply_and_dividend):
        if re.search("[*/]", i):
            sub_res = mutiply_dividend(i)
            multiply_and_dividend[index] = sub_res
    # print(multiply_and_dividend, plus_and_minus_operators)  # 计算
    final_res = None
    for index, item in enumerate(multiply_and_dividend):
        if final_res:
            if plus_and_minus_operators[index - 1] == '+':
                final_res += float(item)
            elif plus_and_minus_operators[index - 1] == '-':
                final_res -= float(item)
        else:
            final_res = float(item)
            procession = '[%s]计算结果:' % expresstion, final_res
        # final_result.insert(END, procession)  # 插入窗体
        # print(procession)
    return final_res
"""主函数:运算逻辑:先计算拓号里的值,算出来后再算乘除,再算加减"""
def calculate():
    expresstion = expresstions.get()  # 获取输入框值
    flage = True
    calculate_res = None  # 初始化计算结果为None
    while flage:
        m = re.search("\([^()]*\)", expresstion)  # 先找最里层的()
    # pattern = re.compile(r"\([^()]*\)")
    # m = pattern.match(expresstion)
        if m:
            sub_res = figure_up(m.group())  # 运算()里的公式
            expresstion = expresstion.replace(m.group(), str(sub_res))  # 运算完毕把结果替换掉公式
        else:
            # print('---------------括号已经计算完毕--------------')
            procession1 = "最终计算结果:%s\n"%figure_up(expresstion)
            print(procession1)
            final_result.insert(END, procession1)  # 插入窗体
            # print('\033[31m最终计算结果:', figure_up(expresstion))
            flage = False

if __name__ == "__main__":
# res = calculate("1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )")
    window = Tk()  ###创建窗体
    window.title('计算器')  ###命名窗体
    frame1 = Frame(window)  ###框架1
    frame1.pack()  ###放置
    frame2 = Frame(window)  ###框架2
    frame2.pack()  ###放置
    lable = Label(frame1, text="请输入公式:")  ###文字标签
    lable.pack()
    expresstions = StringVar()  ###输入框属性,字符串
    entryname = Entry(frame1, textvariable=expresstions)  ###文本输入框
    bt_get_expresstions = Button(frame1, text="提交", command=calculate)  ###按钮挂件
    bt_get_expresstions.pack()
    entryname.pack()
    lable.grid_slaves(row=1,column=1)
    entryname.grid_slaves(row=1,column=1)
    bt_get_expresstions.grid_slaves(row=1,column=3)
    final_result = Text(frame2)  ###计算结果显示框
    final_result.tag_config("here", background="yellow", foreground="blue")
    final_result.pack()
    window.mainloop()  ###事件循环

窗体计算器

  

  

posted on 2018-03-03 13:21  vmaze  阅读(259)  评论(0编辑  收藏  举报

导航