2014-03-19 99 views
0

我道歉评估中缀表示法,如果我似乎愚蠢这里,但我难倒...因为我说需要有这样的程序,使用堆栈计算中间符号表情,但我不能为我的生命得到这件事情适当地解决。使用Python堆栈问题

如果有人可以帮助我解决我的代码,并可能解释,我出了错那么我将非常感激。此外,比较遗憾的是靠不住的格式,这是我的第一篇文章,我不完全理解的代码输入格式。

import operator 

def Main(): 
    #In main, necessary stacks are generated and 
    #all calculations and methods are called and preformed. 

    opStack = ArrayStack() 
    numStack = ArrayStack() 
    opList = ['*', '/', '+', '-'] 
    nums = '1234567890' 
    parn = ['(', ')'] 
    toEval = input('Enter the Expression: ') 

    toEval = toEval.split() 
    #print(toEval) 

    for each in toEval: 

     if each in nums: 
      numStack.push(each) 

     if each == parn[0]: 
      opStack.push(each) 

     if each in opList:    
      if each == opList[2] or opList[3]: 
       opStack.push(each) 

      if each == opList[0] or opList[1]: 
        while opStack.top() == (opList[2] or opList[3]) and len(opStack) > 0 and len(numStack) >= 2: 
         ans = Eval(numStack.pop(),numStack.pop(),opStack.pop()) 
         numStack.push(ans) 
         opStack.push(each) 
     if each == parn[1]: 
      while opStack.top() != "(": 
       ans = Eval(numStack.pop(),numStack.pop(),opStack.pop()) # this line is poping the empty stack 
       numStack.push(ans) 
      opStack.pop() 

    while opStack.is_empty() != True: 
     ans = Eval(numStack.pop(),numStack.pop(),opStack.pop()) 
     numStack.push(ans) 
    print(ans) 


def Eval(num1, num2, op): 
    #two numbers and an op are pulled from stacks, op checked against dict 
    #dict should supply necessary 

    ops2 = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv} 

    op_char = op 

    op_func = ops2[op_char] 

    res = op_func(float(num1), float(num2)) 

    return res 



class ArrayStack: 
# LIFO Stack implementation using a Python list as underlying storage. 

    def __init__(self): 
     # Create an empty stack. 
     self._data = [] # nonpublic list instance 

    def __len__(self): 

     # Return the number of elements in the stack. 

     return len(self._data) 

    def is_empty(self): 

     # Return True if the stack is empty. 
     return len(self._data) == 0 

    def push(self, e): 
     # Add element e to the top of the stack. 

     self._data.append(e) # new item stored at end of list 

    def top(self): 
     # Return (but do not remove) the element at the top of the stack. 
     # Raise Empty exception if the stack is empty. 

     if self.is_empty(): 
      raise Empty('Stack is empty') 
     return self._data[-1] # the last item in the list 

    def pop(self): 

     #Remove and return the element from the top of the stack (i.e., LIFO). 
     #Raise Empty exception if the stack is empty. 

     if self.is_empty(): 
      raise Empty('Stack is empty') 
     return self._data.pop() # remove last item from list 



Main() 

回答

0

这两个if语句总是为真:

if each == opList[2] or opList[3]: 

if each == opList[0] or opList[1]: 

你想要更多的东西是这样的:

if each == opList[2] or each == opList[3]: 

等。

可能有其他的问题,但一个肯定会保持你的程序不能正常工作。