模拟学生打印任务队列,为学生、 打印任务和打印机分别构建对象,利用概 率知识实现打印任务调度策略。打印任务模拟程序加进不同设定,来进行更丰富的模拟: • 学生数量加倍; • 打印队列中任务超过两个(>2),打印速度加倍。打印任务不多余两个(<=2),打印速 度恢复; • 当前打印任务剩余时间大于 100 秒(>100),打印速度加倍。当前打印任务剩余时间 不多于 100 秒(<=100),打印速度恢复; • 打印队列最多容纳 7 个打印任务; • 连续打印时间不得超过 5 分钟,超过 5 分钟时休息半分钟。
时间: 2023-08-24 18:07:17 浏览: 46
以下是模拟学生打印任务队列的代码实现,其中包含所有设定:
```python
import random
import time
class Student:
def __init__(self, name):
self.name = name
def __str__(self):
return f"学生{self.name}"
class PrintJob:
def __init__(self, pages):
self.pages = pages
self.time = pages * 5
self.startTime = None
def startPrint(self, currentTime):
self.startTime = currentTime
def getWaitTime(self, currentTime):
return currentTime - self.startTime
def __str__(self):
return f"打印任务({self.pages}页)"
class Printer:
def __init__(self):
self.currentPrintJob = None
self.timeRemaining = 0
self.printSpeed = 1
def tick(self):
if self.currentPrintJob is not None:
self.timeRemaining -= 1
if self.timeRemaining <= 0:
self.currentPrintJob = None
def isBusy(self):
return self.currentPrintJob is not None
def startNext(self, newPrintJob):
self.currentPrintJob = newPrintJob
self.timeRemaining = newPrintJob.time / self.printSpeed
newPrintJob.startPrint(time.time())
def accelerate(self):
self.printSpeed = 2
def decelerate(self):
self.printSpeed = 1
def canAccommodate(self, jobQueue):
return len(jobQueue) < 7
class PrintQueue:
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)
def newPrintJob():
return PrintJob(random.randint(1, 20))
def simulate(numStudents):
printQueue = PrintQueue()
printer = Printer()
waitingTimes = []
for currentSecond in range(3600):
if random.randint(1, 180) == 180:
newJob = newPrintJob()
print(f"{currentSecond}s: 新的打印任务 {newJob}")
if printer.canAccommodate(printQueue):
printQueue.enqueue(newJob)
else:
print(f"{currentSecond}s: 打印队列已满,{newJob} 被丢弃")
if not printer.isBusy() and not printQueue.isEmpty():
nextJob = printQueue.dequeue()
printer.startNext(nextJob)
print(f"{currentSecond}s: {nextJob} 开始打印")
printer.tick()
if currentSecond % 30 == 0:
if printer.printSpeed == 2:
printer.decelerate()
print(f"{currentSecond}s: 打印速度恢复正常")
elif printer.printSpeed == 1 and printer.currentPrintJob is not None and printer.currentPrintJob.timeRemaining > 100:
printer.accelerate()
print(f"{currentSecond}s: 打印速度加倍")
if currentSecond % 300 == 0:
print(f"{currentSecond}s: 已经过去 {currentSecond // 60} 分钟")
if currentSecond % 1800 == 0:
print(f"{currentSecond}s: 休息半分钟")
time.sleep(30)
waitingTimes.append(printQueue.size())
averageWait = sum(waitingTimes) / len(waitingTimes)
print(f"平均等待时间:{averageWait:.2f} 秒,还剩 {printQueue.size()} 个打印任务在队列中。")
if __name__ == '__main__':
simulate(10)
```
在这个模拟中,我们创建了 `Student`、`PrintJob` 和 `Printer` 三个类来分别代表学生、打印任务和打印机。同时,我们还创建了一个 `PrintQueue` 类来表示打印任务队列。
在 `Printer` 类中,我们使用了一个 `printSpeed` 属性来表示打印机的打印速度。当打印队列中的任务数超过 2 个时,我们将打印速度加倍;当当前打印任务剩余时间大于 100 秒时,我们也将打印速度加倍。这些设定都在 `simulate` 函数中实现。
此外,我们还增加了打印队列最多容纳 7 个打印任务和连续打印时间不得超过 5 分钟的限制。当打印队列已满时,新的打印任务将被丢弃;当连续打印时间超过 5 分钟时,程序将休息半分钟以便打印机冷却。
在主函数中,我们调用了 `simulate` 函数来完成模拟。我们设定了 10 名学生来进行打印任务,也就是说打印队列中最多可能存在 10 个打印任务。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)