Python实现优先队列案例:急症室调度

前言

使用优先队列来模拟“急症室调度”。

  • 优先队列基于单链表实现。
  • 病人patient 对象有两个变量:name-名称、condition-病情等级(1-3值越小越优先)。
  • 使用类似MVC形式编写

完整代码GitHub:
https://github.com/GYT0313/Python-DataStructure/tree/master/6-queue/er

1. 运行示例

运行:python erapp.py

示例中,先输入一个对象patient1(tom, 2),输入对象patient2(jack, 1)。
由于patient2 的优先级高于patient1,所以当选择3-治疗所有队列中的对象时,顺序为patient2、patient1(尽管patient1 先进入队列)。
在这里插入图片描述

2. 代码

这里只给出部分代码,完成代码见GitHub: 点击链接
ermodel.py

"""
File: ermodel.py
"""

from linkedpriorityqueue import LinkedPriorityQueue


class Condition(object):
    """三个病情状态 1 2 3"""

    def __init__(self, rank):
        self._rank = rank

    def __eq__(self, other):
        return self._rank == other._rank

    def __lt__(self, other):
        return self._rank < other._rank

    def __le__(self, other):
        return self._rank <= other._rank

    def __str__(self):
        if self._rank == 1: return "critical"
        elif self._rank == 2:   return "serious"
        else:   return "fair"


class Patient(object):
    """病人对象, 包含名称、病情程度"""

    def __init__(self, name, condition):
        self._name = name
        self._condition = condition

    def __eq__(self, other):
        return self._condition == other._condition

    def __lt__(self, other):
        return self._condition < other._condition

    def __le__(self, other):
        return self._condition <= other._condition

    def __str__(self):
        return self._name + " / " + str(self._condition)


class ERModel(object):
    """模型调度器"""
    def __init__(self):
        self._patients_queue = LinkedPriorityQueue()

    def isEmpty(self):
        return self._patients_queue.isEmpty()

    def schedule(self, patient):
        """增加一个病人"""
        self._patients_queue.add(patient)

    def treat_next(self):
        """删除队头节点"""
        return self._patients_queue.pop()

errapp.py

"""
File: erapp.py
"""

from ermodel import ERModel, Patient, Condition

class ERView(object):
    """视图"""

    def __init__(self, model):
        self.model = model

    def run(self):
        menu = "Main menu\n" + \
               "  1  Schedule a patient\n" + \
               "  2  Treat the next patient\n" + \
               "  3  Treat all patients\n" \
               "  4  Exit the program"
        while True:
            command = self.get_command(4, menu)
            if command == 1:    self.schedule()
            elif command == 2:  self.treat_next()
            elif command == 3:  self.treat_all()
            else:   break

    def treat_next(self):
        """治疗下一个"""
        if self.model.isEmpty():
            print("No patients available to treat.")
        else:
            patient = self.model.treat_next()
            print(patient, "is being treated.")

    def treat_all(self):
        """治疗所有等待的患者"""
        if self.model.isEmpty():
            print("No patients available to treat.")
        else:
            while self.model.isEmpty() == False:
                self.treat_next()
            print()

    def schedule(self):
        """输入用户信息并调度用户"""
        name = input("\nEnter ther patinet's name: ")
        condition = self.get_condition()
        self.model.schedule(Patient(name, condition))
        print(name, "is added to the", condition, "list\n")

    def get_condition(self):
        """获得状态信息"""
        menu = "Patient's condition:\n" + \
               "  1  Critical\n" + \
               "  2  Serious\n" + \
               "  3  Fair"
        number = self.get_command(3, menu)
        return Condition(number)

    def get_command(self, high, menu):
        """返回一个状态"""
        prompt = "Enter a number [1-" + str(high) + "]: "
        command_range = list(map(str, range(1, high + 1)))
        error = "Error, number must be 1 to " + str(high)
        while True:
            print(menu)
            command = input(prompt)
            if command in command_range:
                return int(command)
            else:
                print(error)

def main():
    model = ERModel()
    view = ERView(model)
    view.run()

if __name__ == '__main__':
    main()

完整代码见GitHub:
https://github.com/GYT0313/Python-DataStructure/tree/master/6-queue/er

完!

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页