zoukankan      html  css  js  c++  java
  • python3简单实现支持括号的加减乘除运算

    1.首先表达式的空格。
    2.循环计算最内层带括号的表达式(提取运算符出现的顺序,然后计算)
    3.计算最外层的表达式输出。
    刚接触python,代码不够严谨,仅实现功能。不知道如何在函数中使用运算符变量做运算(使用的时候是字符串形式),希望知道的朋友告诉我,互相学习一下。
    import re,math
    def qcysf(s):
        while re.findall('+-|++|--|-+',s):
            s = s.replace('+-','-')
            s = s.replace('++','+')
            s = s.replace('--','+')
            s = s.replace('-+','-')
        return s
    def yunsuan(a1):
        temp1 = re.sub('d|.|+|-| ','',a1)
        # print(temp1)
        # print(a1)
        for i in temp1:
            if i == "*":
                b = re.search('(-?d+.?d**-?d+.?d*)', a1).group()
                # print(b)
                temp2 = round(float(b.split('*')[0]) * float(b.split('*')[1]),10)
                temp2 = '+' + str(temp2)
                a1 = a1.replace(b, temp2,1)
                # print(a1)
            else:
                b = re.search('(-?d+.?d*/-?d+.?d*)', a1).group()
                # print(b)
                temp2 = round(float(b.split('/')[0]) / float(b.split('/')[1]),10)
                temp2 = '+' + str(temp2)
                a1 = a1.replace(b, temp2,1)
                # print(a1)
        a1 = qcysf(a1)
        # print(a1)
        a2 = a1.lstrip('-')
        temp3 = re.sub('d|.| ', '', a2)
        for i in temp3:
            if i == "+":
                    b = re.search('(-?d+.?d*+d+.?d*)', a1).group()
                    temp2 = round(float(b.split('+')[0]) + float(b.split('+')[1]),10)
                    a1 = a1.replace(b, str(temp2),1)
                    # print(a1)
            else:
                b = re.search('(d+.?d*-d+.?d*)', a1).group()
                temp2 = round(float(b.split('-')[0]) - float(b.split('-')[1]),10)
                a1 = a1.replace(b, str(temp2),1)
                # print(a1)
        return a1
    
    if __name__ == "__main__":
        a = input('请输入你要计算的内容:')
        a = a.replace(' ','')
        # print(a)
        if re.findall('[a-zA-Z]]',a):
            print('你输入的内容不合法')
        else:
            while re.search("([^()]+)", a):
                b = re.search("([^()]+)", a).group()
                # b = qcysf(b)
                # print(a)
                # print(b)
                b1 = re.sub('(|)','',b)
                # print(a)
                temp = yunsuan(b1)
                a = a.replace(b, str(temp))
            # print('这是倒数第二个',a)
            a = qcysf(a)
            a = yunsuan(a)
            print(a)
    View Code

    请输入你要计算的内容:1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )
    2776672.6951997215

  • 相关阅读:
    .NET Framework 3.0 和 Windows SDK
    用 C# 开发 SQL Server 2005 的自定义聚合函数
    IronPython 源码剖析系列(2):IronPython 引擎的运作流程
    IronPython 个人网站样例宝藏挖掘
    SetRenderMethodDelegate 方法
    使用 Castle ActiveRecord 开发发现的一些问题
    IronPython for ASP.NET CTP WhitePaper 摘要翻译
    关于 IE 模态对话框的两个问题
    逐步改用 IronPython 开发你的 ASP.NET 应用程序
    使用 Flash 和 C# WinForm 配合打造界面漂亮的应用程序(摘要)
  • 原文地址:https://www.cnblogs.com/guojintao/p/9078825.html
Copyright © 2011-2022 走看看