您好,登錄后才能下訂單哦!
class PrintQueue:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def enqueue(self, item):
self.items.insert(0, item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
class Printer:
def __init__(self, ppm):
self.ppm = ppm
self.current_task = None
self.time_remaining = 0
def tick(self):
if self.current_task is not None:
self.time_remaining -= 1
if self.time_remaining <= 0:
self.current_task = None
def busy(self):
return self.current_task is not None
def start_next(self, new_task):
self.current_task = new_task
self.time_remaining = new_task.get_pages() * 60 / self.ppm
class Task:
def __init__(self, time):
self.time = time
def get_pages(self):
return self.time
def simulation(num_seconds, pages_per_minute):
printer = Printer(pages_per_minute)
print_queue = PrintQueue()
waiting_times = []
for current_second in range(num_seconds):
if new_print_task():
task = Task(random.randint(1, 20))
print_queue.enqueue(task)
if not printer.busy() and not print_queue.is_empty():
next_task = print_queue.dequeue()
waiting_times.append(next_task.time)
printer.start_next(next_task)
printer.tick()
average_wait = sum(waiting_times) / len(waiting_times)
print("Average Wait Time: {:.2f} seconds, {} tasks remaining.".format(average_wait, print_queue.size()))
def new_print_task():
num = random.randint(1, 180)
return num == 180
if __name__ == '__main__':
import random
for i in range(10):
simulation(3600, 5) # Simulate for 1 hour at 5 pages per minute
這段代碼實現了一個簡單的打印任務監控系統。它模擬了一個打印隊列和打印機,根據隨機生成的打印任務來模擬打印過程。在主程序中,我們可以調用simulation
函數來運行模擬,輸出平均等待時間和剩余任務數。在這里,我們模擬了10次1小時內以每分鐘5頁的速度打印的情況。您可以根據實際需求調整模擬的時間和打印速度。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。