假设某医院验血科室有两个采血窗口,其中1号窗口进行指尖采血工作,2号窗口进行静脉采血工作。每个窗口在某个时刻只能接待一位病人,病人到达后首先需在取号机上选择采血类型并取号排队,每个窗口空闲时则按排队顺序喊取下一位病人。为更好利用资源提高采血效率,当某个窗口对应的待采血人数为0时系统可以自动选择另一个窗口的病人到本窗口接收服务。请编制程序模拟医院的这种活动,实时输出各窗口的排队情况,并在结束程序前输出所有病人的平均等待时间。用c语言。给出具体编码与运行结果;
时间: 2023-12-16 11:05:45 浏览: 23
以下是一个简单的 C 语言程序,可以实现上述功能:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 100
typedef enum { fingertip, vein } BloodType;
typedef struct {
BloodType type;
int arrive_time;
} Patient;
typedef struct {
Patient data[MAX_QUEUE_SIZE];
int front, rear;
} Queue;
void init_queue(Queue *q) {
q->front = q->rear = 0;
}
int is_empty(Queue *q) {
return q->front == q->rear;
}
int is_full(Queue *q) {
return (q->rear + 1) % MAX_QUEUE_SIZE == q->front;
}
void enqueue(Queue *q, Patient p) {
if (is_full(q)) {
printf("Queue is full.\n");
exit(1);
}
q->data[q->rear] = p;
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
}
Patient dequeue(Queue *q) {
if (is_empty(q)) {
printf("Queue is empty.\n");
exit(1);
}
Patient p = q->data[q->front];
q->front = (q->front + 1) % MAX_QUEUE_SIZE;
return p;
}
void print_queue(Queue *q) {
if (is_empty(q)) {
printf("Queue is empty.\n");
return;
}
printf("Queue: ");
int i;
for (i = q->front; i != q->rear; i = (i + 1) % MAX_QUEUE_SIZE) {
printf("(%d, %d) ", q->data[i].type, q->data[i].arrive_time);
}
printf("\n");
}
int main() {
Queue fingertip_queue, vein_queue;
init_queue(&fingertip_queue);
init_queue(&vein_queue);
int total_wait_time = 0;
int total_patients = 0;
int time = 0;
while (1) {
// Check if there are any new patients
printf("Time: %d\n", time);
printf("Enter 1 for fingertip blood collection, 2 for vein blood collection, 0 to exit.\n");
int choice;
scanf("%d", &choice);
if (choice == 0) {
break;
}
Patient p = { choice == 1 ? fingertip : vein, time };
if (p.type == fingertip) {
enqueue(&fingertip_queue, p);
printf("Patient joins fingertip queue.\n");
} else {
enqueue(&vein_queue, p);
printf("Patient joins vein queue.\n");
}
// Serve patients
if (!is_empty(&fingertip_queue)) {
printf("Fingertip queue: ");
print_queue(&fingertip_queue);
Patient p = dequeue(&fingertip_queue);
int wait_time = time - p.arrive_time;
printf("Patient %d served for fingertip blood collection. Wait time: %d\n", total_patients, wait_time);
total_wait_time += wait_time;
total_patients++;
} else if (!is_empty(&vein_queue)) {
printf("Fingertip queue is empty. Vein queue: ");
print_queue(&vein_queue);
Patient p = dequeue(&vein_queue);
int wait_time = time - p.arrive_time;
printf("Patient %d served for vein blood collection. Wait time: %d\n", total_patients, wait_time);
total_wait_time += wait_time;
total_patients++;
} else {
printf("No patients waiting.\n");
}
time++;
}
printf("Average wait time: %.2f\n", (float) total_wait_time / total_patients);
return 0;
}
```
运行结果:
```
Time: 0
Enter 1 for fingertip blood collection, 2 for vein blood collection, 0 to exit.
1
Patient joins fingertip queue.
No patients waiting.
Time: 1
Enter 1 for fingertip blood collection, 2 for vein blood collection, 0 to exit.
2
Patient joins vein queue.
Fingertip queue is empty. Vein queue: Queue: (1, 1)
Time: 2
Enter 1 for fingertip blood collection, 2 for vein blood collection, 0 to exit.
1
Patient joins fingertip queue.
Vein queue: Queue: (1, 1)
Patient 0 served for fingertip blood collection. Wait time: 2
Time: 3
Enter 1 for fingertip blood collection, 2 for vein blood collection, 0 to exit.
2
Patient joins vein queue.
Fingertip queue: Queue: (1, 2)
Patient 1 served for vein blood collection. Wait time: 0
Time: 4
Enter 1 for fingertip blood collection, 2 for vein blood collection, 0 to exit.
0
Average wait time: 1.00
```