我可以用Python2.7编写一个执行符号计算的函数吗?

2024-06-15 17:39:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在从Java过渡到Python,并承担了创建一个计算器的任务,该计算器可以对中缀符号表示的数学表达式执行符号运算(,而不使用Sympy等自定义模块)。目前,它被构建为接受以空格分隔的字符串,并且只能执行(,),+,-,*,和/运算符。不幸的是,我无法找到简化符号表达式的基本算法。在

例如,给定字符串“2*((9/6)+6*x)”,我的程序应该执行以下步骤:

  1. 2*(1.5+6*x)
  2. 3+12*x

但我不能让程序在分发2时忽略x。此外,如何处理“x*6/x”,以便在简化后返回“6”?

编辑:为了澄清,我所说的“符号化”是指在执行剩余的计算时,它会在输出中留下“A”和“f”这样的字母。在

编辑2:我(大部分)完成了代码。如果将来有人无意中发现这篇文章,或者你们中有人好奇,我会把它贴在这里。在

    def reduceExpr(useArray):

        # Use Python's native eval() to compute if no letters are detected.
        if (not hasLetters(useArray)):
            return [calculate(useArray)] # Different from eval() because it returns string version of result

        # Base case. Returns useArray if the list size is 1 (i.e., it contains one string). 
        if (len(useArray) == 1):
            return useArray

        # Base case. Returns the space-joined elements of useArray as a list with one string.
        if (len(useArray) == 3):
            return [' '.join(useArray)]

        # Checks to see if parentheses are present in the expression & sets.
        # Counts number of parentheses & keeps track of first ( found. 
        parentheses = 0
        leftIdx = -1

        # This try/except block is essentially an if/else block. Since useArray.index('(') triggers a KeyError
        # if it can't find '(' in useArray, the next line is not carried out, and parentheses is not incremented.
        try:
            leftIdx = useArray.index('(')
            parentheses += 1
        except Exception:
            pass

        # If a KeyError was returned, leftIdx = -1 and rightIdx = parentheses = 0.
        rightIdx = leftIdx + 1

        while (parentheses > 0):
            if (useArray[rightIdx] == '('):
                parentheses += 1
            elif (useArray[rightIdx] == ')'):
                parentheses -= 1
            rightIdx += 1

        # Provided parentheses pair isn't empty, runs contents through again; else, removes the parentheses
        if (leftIdx > -1 and rightIdx - leftIdx > 2):
            return reduceExpr(useArray[:leftIdx] + [' '.join(['(',reduceExpr(useArray[leftIdx+1:rightIdx-1])[0],')'])] + useArray[rightIdx:])
        elif (leftIdx > -1):
            return reduceExpr(useArray[:leftIdx] + useArray[rightIdx:])

        # If operator is + or -, hold the first two elements and process the rest of the list first
        if isAddSub(useArray[1]):
            return reduceExpr(useArray[:2] + reduceExpr(useArray[2:]))
        # Else, if operator is * or /, process the first 3 elements first, then the rest of the list
        elif isMultDiv(useArray[1]):
            return reduceExpr(reduceExpr(useArray[:3]) + useArray[3:])
        # Just placed this so the compiler wouldn't complain that the function had no return (since this was called by yet another function).
        return None

Tags: andofthereturnifis符号not
2条回答

在对符号进行操作之前,需要更多的处理。您想要得到的表单是一个在叶节点中有值的操作树。首先,您需要在字符串上运行lexer来获取元素-尽管如果总是使用空格分隔的元素,那么只需拆分字符串就足够了。然后,您需要使用所需的语法来解析该标记数组。在

如果您需要有关语法和解析文本的理论信息,请从这里开始:http://en.wikipedia.org/wiki/Parsing如果您需要更实际的东西,请转到https://github.com/pyparsing/pyparsing(您不必使用pyparsing模块本身,但是它们的文档有很多有趣的信息)或http://www.nltk.org/book

2 * ( ( 9 / 6 ) + 6 * x )开始,您需要到达如下树:

      *
2           +
         /     *
        9 6   6 x

然后您可以访问每个节点并决定是否要简化它。常量操作将是最简单的消除方法-只需计算结果并用1.5交换“/”节点,因为所有子节点都是常量。在

有很多策略需要继续,但本质上你需要找到一种方法来遍历树并对其进行修改,直到没有什么可以改变的。在

如果要打印结果,只需再次遍历树并生成一个表达式来描述它。在

如果您正在用Python解析表达式,那么可以考虑使用Python语法来解析表达式,并使用^{} module(AST=abstract syntax tree)来解析它们。在

使用Python语法的好处是:不必为此而创建单独的语言,解析器和计算器都是内置的。缺点:解析树中有很多您不需要的额外复杂性(您可以通过使用内置的NodeVisitorNodeTransformer类来避免其中的一些复杂性)。在

>>> import ast
>>> a = ast.parse('x**2 + x', mode='eval')
>>> ast.dump(a)
"Expression(body=BinOp(left=BinOp(left=Name(id='x', ctx=Load()), op=Pow(),
right=Num(n=2)), op=Add(), right=Name(id='x', ctx=Load())))"

下面是一个示例类,它遍历Python解析树并执行递归常量折叠(对于二进制操作),以向您展示您可以相当容易地完成的事情。在

^{pr2}$

相关问题 更多 >