kuake 发表于 2022-3-22 17:38:24

Python数据结构与算法的改写

麻烦帮忙改正错误,使程序能够正常运行

ba21 发表于 2022-3-22 17:38:25

这里的错误给你指出。
你的代码你的思路很难理解

ba21 发表于 2022-3-22 17:41:40

代码文字形式上传。
挣你点币还得巧半天代码。难挣不?

kuake 发表于 2022-3-22 17:43:16

class Queue:
    def __init__(self):
      self.items = []
      
    def isEmpty(self):
      return self.items==[]
   
    def enqueue(self,item):
      self.items.insert(0,item)
      
    def dequeue(self):
      return self.items.pop()
   
    def size(self):
      return len(self.items)
   
import random   
class Washer:
    def __init__(self):
      self.currentTask=None
      self.timeRemaining=0

    def tick(self):
      if self.currentTask != None:
            self.timeRemaining = self.timeRemaining-1
            if self.timeRemaining <= 0:
                self.currentTask = None

    def busy(self):
      if self.currentTask != None:
            return True
      else:
            return False

    def startNext(self,newtask):
      self.currentTask = newtask
      self.timeRemaining = random.randrange(10,31)

import random
class Task:
    def __init__(self,time):
      self.timestamp = time
      
    def getStamp(self):
      return self.timestamp

    def waitTime(self,currenttime):
      return currenttime-self.timestamp


import random

def simulation(numMinutes):

    labwasher = random.randrange(10,31)
    printQueue = Queue()
    waitingtimes = []

    for currentMinute in range(numMinutes):

      if newPrintTask():
         task = Task(currentMinute)
         printQueue.enqueue(task)

      if (not labwasher.busy()) and \
                  (not printQueue.isEmpty()):
            nexttask = printQueue.dequeue()
            waitingtimes.append( \
                  nexttask.waitTime(currentMinute))
            labwasher.startNext(nexttask)

      labwasher.tick()

    averageWait=sum(waitingtimes)/len(waitingtimes)
    print("Average Wait %6.2f secs %3d tasks remaining."\
                  %(averageWait,printQueue.size()))

def newPrintTask():
      num = random.randrange(1,16)
      if num == 15:
            return True
      else:
            return False

kuake 发表于 2022-3-22 17:44:43

ba21 发表于 2022-3-22 17:41
代码文字形式上传。
挣你点币还得巧半天代码。难挣不?

class Queue:
    def __init__(self):
      self.items = []
      
    def isEmpty(self):
      return self.items==[]
   
    def enqueue(self,item):
      self.items.insert(0,item)
      
    def dequeue(self):
      return self.items.pop()
   
    def size(self):
      return len(self.items)
   
import random   
class Washer:
    def __init__(self):
      self.currentTask=None
      self.timeRemaining=0

    def tick(self):
      if self.currentTask != None:
            self.timeRemaining = self.timeRemaining-1
            if self.timeRemaining <= 0:
                self.currentTask = None

    def busy(self):
      if self.currentTask != None:
            return True
      else:
            return False

    def startNext(self,newtask):
      self.currentTask = newtask
      self.timeRemaining = random.randrange(10,31)

import random
class Task:
    def __init__(self,time):
      self.timestamp = time
      
    def getStamp(self):
      return self.timestamp

    def waitTime(self,currenttime):
      return currenttime-self.timestamp


import random

def simulation(numMinutes):

    labwasher = random.randrange(10,31)
    printQueue = Queue()
    waitingtimes = []

    for currentMinute in range(numMinutes):

      if newPrintTask():
         task = Task(currentMinute)
         printQueue.enqueue(task)

      if (not labwasher.busy()) and \
                  (not printQueue.isEmpty()):
            nexttask = printQueue.dequeue()
            waitingtimes.append( \
                  nexttask.waitTime(currentMinute))
            labwasher.startNext(nexttask)

      labwasher.tick()

    averageWait=sum(waitingtimes)/len(waitingtimes)
    print("Average Wait %6.2f secs %3d tasks remaining."\
                  %(averageWait,printQueue.size()))

def newPrintTask():
      num = random.randrange(1,16)
      if num == 15:
            return True
      else:
            return False

C丁洞杀O 发表于 2022-3-23 07:40:47

这Python,也太难了,不过如果是C的话简单一点哇
页: [1]
查看完整版本: Python数据结构与算法的改写