使用队列编写病人看病模拟程序
时间: 2023-07-11 10:17:46 浏览: 94
好的,我可以为您编写一个使用队列的病人看病模拟程序。首先,我们需要定义一个病人类:
```python
class Patient:
def __init__(self, name, condition):
self.name = name
self.condition = condition
```
病人有姓名和病情这两个属性。
接下来,我们可以创建一个队列来存储等待看病的病人:
```python
from queue import Queue
patient_queue = Queue()
```
现在,我们可以定义一些函数来模拟病人看病的过程。首先是病人来看病的过程:
```python
def patient_arrives(patient):
patient_queue.put(patient)
print(f"{patient.name} arrives to see the doctor with {patient.condition}")
```
这个函数会将病人加入到队列中,并打印出病人的信息。
接下来是看病的过程:
```python
def see_doctor():
if not patient_queue.empty():
patient = patient_queue.get()
print(f"{patient.name} is seeing the doctor for {patient.condition}")
else:
print("No patients to see")
```
这个函数会检查队列中是否有病人,如果有,则取出队首的病人进行诊断,并打印出病人的信息。否则,打印出“没有病人”的信息。
最后是病人离开的过程:
```python
def patient_leaves(patient):
print(f"{patient.name} leaves the clinic after seeing the doctor for {patient.condition}")
```
这个函数会打印出病人离开的信息。
现在,我们可以编写一个主程序来模拟病人看病的过程:
```python
if __name__ == "__main__":
patient_arrives(Patient("John", "fever"))
patient_arrives(Patient("Jane", "cold"))
see_doctor()
patient_arrives(Patient("Bob", "headache"))
see_doctor()
see_doctor()
patient_leaves(Patient("John", "fever"))
see_doctor()
patient_leaves(Patient("Jane", "cold"))
```
这个主程序会模拟三个病人来看病,其中第一个病人有发烧,第二个病人有感冒,第三个病人有头痛。接着,会模拟两个病人看病的过程,然后第一个病人离开,再模拟一次看病的过程,最后第二个病人离开。
当您运行这个程序时,它应该会输出以下内容:
```
John arrives to see the doctor with fever
Jane arrives to see the doctor with cold
John is seeing the doctor for fever
Bob arrives to see the doctor with headache
Jane is seeing the doctor for cold
John leaves the clinic after seeing the doctor for fever
Bob is seeing the doctor for headache
Jane leaves the clinic after seeing the doctor for cold
```
阅读全文