如何从Queue类移动到Stack类,然后再计算函数?

时间:2018-11-04 16:50:47

标签: python stack queue

我正在尝试制作一个程序,使用户可以通过键入逻辑门,输入和输出来制作逻辑门电路。该程序当前将输入添加到队列中,并且在写入输出时,应将输入从队列中删除并添加到堆栈中以进行计算。当堆栈中装满3个项目时,它将计算3个项目并将答案返回到堆栈。当我运行我的代码时,它总是说我是从一个空堆栈中弹出的,我不知道如何解决它。用户必须先输入输入,然后输入门,最后输入输出。示例:将1 AND 0 NOT OUTPUT输入为1 O AND NOT OUTPUT到程序中。我真的需要帮助,因为我已经尝试了好几个星期了。

class Queue():
   def __init__(self):
      self.queue = []
      self.__frontPtr = 0
      self.__rearPtr = 0
      self.__maxLength = 10
      self.__length =0

   def addToQ(self,newGate):
      self.queue.insert(0,newGate)
      self.__rearPtr+=1

   def empty(self):
      self.queue = []
      self.__frontPtr = 0
      self.__rearPtr = 0
      return(self.queue,self.__frontPtr,self.__rearPtr)

   def isEmpty(self):
      return self.queue==[]

   def removeFromQ(self):
      return self.queue.pop()

   def displayQ(self):
      print(self.queue)
      print("\n")

   def size(self):
      return (len(self.queue))


class Stack():
   def __init__(self):
      self.stack=[]

   def isEmpty(self):
      return self.stack==[]

   def push(self,newItem):
      self.stack.append(newItem)

   def pop(self):
      return self.stack.pop()

   def empty(self):
      self.stack=[]

   def size(self):
      return len(self.stack)

   def display(self):
      print(self.stack)
      print("\n")


class Calculation(Stack,Queue):
   def __init__(self):
      self.answer=0
      self.myStack=Stack()
      self.myQueue=Queue()


   def isFull(self):
      if self.myStack.size()!=3:
         self.moveToStack()
      else:
         self.calculate()

   def moveToStack(self):
      newItem=self.myQueue.removeFromQ()
      self.myStack.push(newItem)
      self.isFull()


   def calculate():
      item1=self.myStack.pop()
      print(item1)
      item2=self.myStack.pop()
      if item2=="notgate":
         if item3=="one":
            answer=0
            self.myStack.push(0)
         else:
            answer=1
            self.myStack.push(0)
      else:
         item3=self.myStack.pop()


      if item1=="andgate":
         if item2=="one" and item3=="one":
            answer=1
            self.myStack.push(1)
         else:
            answer=0
            self.myStack.push(0)


      elif item1=="nandgate":
         if item2=="one" and item3=="one":
            answer=0
            self.myStack.push(0)
         elif item2=="one" and item3=="zero":
            answer=1
            self.myStack.push(1)
         elif item2=="zero" and item3=="one":
            answer=1
            self.myStack.push(1)
         else:
            answer=1
            self.myStack.push(1)

      elif item1=="norgate":
         if item2=="zero" and item3=="zero":
            answer=1
            self.myStack.push(1)
         elif item2=="one" and item3=="zero":
            answer=0
            self.myStack.push(0)
         elif item2=="zero" and item3=="one":
            answer=0
            self.myStack.push(0)
         else:
            answer=0
            self.myStack.push(0)

      elif item1=="xorgate":
         if item2=="one" and item3=="zero":
            answer=0
            self.myStack.push(0)
         elif item2=="zero" and item3=="one":
            answer=0
            self.myStack.push(0)
         elif item2=="zero" and item3=="zero":
            answer=1
            self.myStack.push(1)
         else:
            answer=1
            self.myStack.push(1)

      elif item1=="orgate":
         if item2=="one":
            answer=1
            self.myStack.push(1)
         elif item3=="one":
            answer=1
            self.myStack.push(1)
         else:
            answer=0
            self.myStack.push(0)

    #  if self.myStack.size()<3:
     #    self.moveToStack()

      if self.myQueue.size()==0:
         print("output is", self.myStack.pop())

circuitInput=str(input("Enter circuit item: "))
gateQueue=Queue()
gateStack=Stack()
gateCal=Calculation()
while circuitInput!="output":
   circuitInput=str(input("Enter circuit item: "))
   gateQueue.addToQ(circuitInput)
gateQueue.addToQ("output")
gateCal.isFull()

0 个答案:

没有答案