Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从String-Python计算数学表达式_Python_String_Expression_Eval_Evaluate - Fatal编程技术网

从String-Python计算数学表达式

从String-Python计算数学表达式,python,string,expression,eval,evaluate,Python,String,Expression,Eval,Evaluate,我需要从给定的字符串(如“(19+((91-96)-13))”计算表达式,但我必须自己制作te算法,我不能使用eval()或类似的东西 我曾多次尝试使用此代码,但它给我带来了负数问题: while counter < 1: chunks = [] counter2 = 1 for character in operation: if character.isdigit(): if chunks[-1].isdigit():

我需要从给定的字符串(如“(19+((91-96)-13))”计算表达式,但我必须自己制作te算法,我不能使用eval()或类似的东西

我曾多次尝试使用此代码,但它给我带来了负数问题:

while counter < 1:
    chunks = []
    counter2 = 1

    for character in operation:
        if character.isdigit():
            if chunks[-1].isdigit():   # If the last chunk is already a number
                chunks[-1] += character  # Add onto that number
            else:
                chunks.append(character) # Start a new number chunk
        elif character in '+-/*()':
            chunks.append(character)  # This doesn't account for `1 ++ 2`.

    for e in reversed(chunks):
        if e == '(':
            counter2 = len(chunks) - counter2
            break
        else:
            counter2 = counter2 + 1

    if chunks[counter2+2] == '+':
        result2 = int (chunks[counter2+1]) + int (chunks[counter2+3])
    elif chunks[counter2+2] == '-':
        result2 = int (chunks[counter2+1]) - int (chunks[counter2+3])
    elif chunks[counter2+2] == '*':
        result2 = int (chunks[counter2+1]) * int (chunks[counter2+3])
    elif chunks[counter2+2] == '/':
        result2 = int (chunks[counter2+1]) / int (chunks[counter2+3])


    chunks[counter2] = ''
    chunks[counter2 + 1] = ''
    chunks[counter2 + 2] = str (result2)
    chunks[counter2 + 3] = ''
    chunks[counter2 + 4] = ''

    operation = ''.join(chunks)
计数器<1时:
块=[]
计数器2=1
对于操作中的字符:
如果字符.isdigit():
if chunk[-1].isdigit():#如果最后一个chunk已经是一个数字
块[-1]+=字符#添加到该数字上
其他:
chunks.append(character)#开始一个新的数字块
“+-/*()”中的elif字符:
chunks.append(character)#这不能解释'1++2'。
对于反向中的e(块):
如果e=='(':
计数器2=len(块)-计数器2
打破
其他:
计数器2=计数器2+1
如果块[counter2+2]=='+':
结果2=int(块[counter2+1])+int(块[counter2+3])
elif块[counter2+2]='-':
结果2=int(块[counter2+1])-int(块[counter2+3])
elif块[counter2+2]='*':
结果2=int(块[counter2+1])*int(块[counter2+3])
elif块[counter2+2]=='/':
结果2=int(块[counter2+1])/int(块[counter2+3])
块[counter2]=''
块[counter2+1]=''
块[counter2+2]=str(result2)
块[计数器2+3]=''
块[计数器2+4]=''
操作=''.join(块)

不要注意while条件,我正在更新它,因为我只需要检查一下

您应该尝试自己调试更多代码。您遇到的问题是int转换是在“-”而不是“-5”上进行的。您需要将序列中的下一个“-”和integer组合起来,并将其转换为int并使用。
temp=str(chunks[counter2+3])+str(chunks[counter2+4])

使用适当的词法分析工具,比如Lex/Yacc,会让你的生活更轻松……问题是我不能,我必须制定整个算法……你在使用这段代码时遇到了哪些负数的具体问题?例如(-5-6)它不会正确地执行,因为它将使用“-”而不是数字,我甚至得到了类似(9+-18)的东西,我不知怎么地修复了它,但现在它给出了奇怪的结果