在python中解析数学表达式并求解以找到答案

问题描述 投票:0回答:3

我对编程很陌生。这与python有关。因此,我们的想法是采用一个表达式,例如3/5或最多3/5 * 2(最多两个运算符,注意运算符可以是+, - ,/,*中的任何一个)并解决它。空格可以存在于表达式中的任何位置。

用户输入表达式,比如3/5,程序需要解析表达式并显示答案。我试过的是下面的内容。注意,我只尝试了第一部分,一旦我可以正确地分割用户输入的原始表达式(这将是一个字符串),创建函数将是容易的部分:

expres= str(input("something:"))

ssplit= hit.partition("/")
onec= int((ssplit[0].lstrip()).rstrip())
twoc= (ssplit[1].lstrip()).rstrip()
threec= int((huns[2].lstrip()).rstrip())


print(onec,"...",twoc,"...",threec) #just a debug test print

所以上面,我可以采用类似3/5的表达式并将其拆分为三个单独的字符串:3,/和5.我还可以删除运算符/操作数之前和之后的所有空格。我有分裂像4/5 + 6这样的表达式的问题,因为我无法将代码放入ssplit [3]或ssplit [4],然后输入类似3/5的表达式,因为它不会被定义。基本上我需要你帮助找出如何分割像3 / 4-6等表达式。我还需要“ssplit= hit.partition("/")”这一行的帮助,这样它就会查看输入的表达式并使用+, - 和*。任何和所有的帮助表示赞赏。如果我上面的代码看起来讨厌和低效,请给我批评。谢谢!

注意我不能,也不想使用eval。操作顺序是必需的。我不能使用复杂的命令。我需要保持简单,我可以使用的最多是字符串库,在字符串/整数/浮点数等之间进行转换以及if和等等。声明。我也可以使用功能。

python string parsing split expression
3个回答
5
投票

如果我不打算依赖外部库,我会这样做:

def parse(x):
    operators = set('+-*/')
    op_out = []    #This holds the operators that are found in the string (left to right)
    num_out = []   #this holds the non-operators that are found in the string (left to right)
    buff = []
    for c in x:  #examine 1 character at a time
        if c in operators:  
            #found an operator.  Everything we've accumulated in `buff` is 
            #a single "number". Join it together and put it in `num_out`.
            num_out.append(''.join(buff))
            buff = []
            op_out.append(c)
        else:
            #not an operator.  Just accumulate this character in buff.
            buff.append(c)
    num_out.append(''.join(buff))
    return num_out,op_out

print parse('3/2*15')

它不是最优雅的,但它可以让你获得合理的数据结构(至于我关注的话)

现在代码实际解析和评估数字 - 这将以浮点方式完成所有事情,但很容易改变...

import operator
def my_eval(nums,ops):

    nums = list(nums)
    ops = list(ops)
    operator_order = ('*/','+-')  #precedence from left to right.  operators at same index have same precendece.
                                  #map operators to functions.
    op_dict = {'*':operator.mul,
               '/':operator.div,
               '+':operator.add,
               '-':operator.sub}
    Value = None
    for op in operator_order:                   #Loop over precedence levels
        while any(o in ops for o in op):        #Operator with this precedence level exists
            idx,oo = next((i,o) for i,o in enumerate(ops) if o in op) #Next operator with this precedence         
            ops.pop(idx)                        #remove this operator from the operator list
            values = map(float,nums[idx:idx+2]) #here I just assume float for everything
            value = op_dict[oo](*values)
            nums[idx:idx+2] = [value]           #clear out those indices

    return nums[0]

print my_eval(*parse('3/2*15'))

2
投票

这不是解析表达式的方法,你应该更多地考虑词法分析器和解析器,比如PLYpyparsing。但是,如果您只想评估表达式,可以使用eval(expr)。请注意,eval将执行您提供的任何代码,因此它并不安全。

编辑这里有一个使用pyparsing的例子,应该让你开始:

pyparsing example


1
投票

使用shlexStringIO Python模块。在Python 2.3+中:

>>> from StringIO import StringIO
>>> import shlex
>>> input = StringIO('3/4+5')
>>> list(shlex.shlex(input))
['3', '/', '4', '+', '5']
© www.soinside.com 2019 - 2024. All rights reserved.