Python中的ifix评估



我正在尝试在此处转换代码http://www.geeksforgeeks.org/expression-evaluation/to Python。但是,我遇到了一些麻烦,无法找到。

class evaluateString:
def evalString(self,expression):
    valueStack = []
    opStack = []
    i=0
    while(i<len(expression)):
        if(expression[i] == ' '):
            continue
        if(expression[i]>='0' and expression[i] <= '9'):
            charNumber = [] #for storing number
            while(i<len(expression) and expression[i]>='0' and expression[i] <= '9'):
                charNumber.append(expression[i])
                i+=1
            valueStack.append(int(''.join(charNumber)))
        elif (expression[i]=='('):
            opStack.append(expression[i])
        elif (expression[i]==')'):
            while(opStack[-1]!='('):
                valueStack.append(self.applyOperation(opStack.pop(),valueStack.pop(),valueStack.pop()))
                opStack.pop()
        elif(expression[i]=='+'or expression[i]=='-'or expression[i]=='*'or expression[i]=='/'):
            while( (len(opStack)!=0) and ( self.opPrecedence(expression[i],opStack[-1]) ) ):
                valueStack.append(self.applyOperation(opStack.pop(),valueStack.pop(),valueStack.pop()))
                opStack.append(expression[i])
        i = i + 1
    while(len(opStack)!=0):
        valueStack.append(self.applyOperation(opStack.pop(),valueStack.pop(),valueStack.pop()))
    return valueStack.pop()

def applyOperation(self,op,a,b):
    if op=='+':
        return a+b
    elif op=='-':
        return a-b
    elif op=='*':
        return a*b
    elif op=='/':
        return a/b
    else:
        return 0
def opPrecedence(self,op1,op2):
    if (op2 == '(' or op2 == ')'):
        return False
    if ((op1 == '*' or op1 == '/') and (op2 == '+' or op2 == '-')):
        return False
    else:
        return True
a = evaluateString()
print(a.evalString("(5+7)"))

我能够在Valuestack中获得正确的数字。但是,在最后两个Elseif中似乎存在问题。有人可以将我指向正确的方向吗?

我已经完成了一些修复程序,并且适用于某些操作。但是我尚未在所有情况下对其进行测试。另外,操作仅是整数,没有浮子(例如,检查下面的最后一个输出(。

class evaluateString:
  def evalString(self,expression):
    valueStack = []
    opStack = []
    i=0
    while(i<len(expression)):
        if(expression[i] == ' '):
            continue
        if(expression[i]>='0' and expression[i] <= '9'):
            charNumber = [] #for storing number
            j = i
            while(j<len(expression) and expression[j]>='0' and expression[j] <= '9'):
                charNumber.append(expression[j])
                j += 1
            i = (j-1)
            valueStack.append(int(''.join(charNumber)))
        elif (expression[i]=='('):
            opStack.append(expression[i])
        elif (expression[i]==')'):
            while(opStack[-1]!='('):
                valueStack.append(self.applyOperation(opStack.pop(),valueStack.pop(),valueStack.pop()))
            opStack.pop()
        elif(expression[i]=='+'or expression[i]=='-'or expression[i]=='*'or expression[i]=='/'):
            while( (len(opStack)!=0) and ( self.opPrecedence(expression[i],opStack[-1]) ) ):
                valueStack.append(self.applyOperation(opStack.pop(),valueStack.pop(),valueStack.pop()))
            opStack.append(expression[i])
        i = i + 1
    while(len(opStack)!=0):
        valueStack.append(self.applyOperation(opStack.pop(),valueStack.pop(),valueStack.pop()))
    return valueStack.pop()

  def applyOperation(self,op,a,b):
    if op=='+':
        return a+b
    elif op=='-':
        return b-a
    elif op=='*':
        return a*b
    elif op=='/':
        return b/a
    else:
        return 0
  def opPrecedence(self,op1,op2):
    if (op2 == '(' or op2 == ')'):
        return False
    if ((op1 == '*' or op1 == '/') and (op2 == '+' or op2 == '-')):
        return False
    else:
        return True
a = evaluateString()
print(a.evalString("8*12"))        #prints 96
print(a.evalString("(122-434)"))   #prints -312
print(a.evalString("(232+12)/2"))  #print 122
print(a.evalString("232/12+2"))    #prints 21

python eval((将评估infix表达式

print(eval("(5+7)/2"))

它将将评估的infix表达值打印为6。

相关内容

  • 没有找到相关文章

最新更新