代码编写银行窗口排队管理系统,模拟用户排队、办理业务结束后离队
时间: 2023-08-16 18:06:30 浏览: 42
以下是一个简单的银行窗口排队管理系统的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_QUEUE_SIZE 10
typedef struct {
int id;
int time;
} Customer;
typedef struct {
Customer data[MAX_QUEUE_SIZE];
int front;
int rear;
} Queue;
void initQueue(Queue *q) {
q->front = q->rear = 0;
}
int isQueueEmpty(Queue *q) {
return q->front == q->rear;
}
int isQueueFull(Queue *q) {
return (q->rear + 1) % MAX_QUEUE_SIZE == q->front;
}
void enQueue(Queue *q, Customer c) {
if (isQueueFull(q)) {
printf("Queue is full.\n");
return;
}
q->data[q->rear] = c;
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
}
Customer deQueue(Queue *q) {
if (isQueueEmpty(q)) {
printf("Queue is empty.\n");
Customer c = {0, 0};
return c;
}
Customer c = q->data[q->front];
q->front = (q->front + 1) % MAX_QUEUE_SIZE;
return c;
}
void printQueue(Queue *q) {
if (isQueueEmpty(q)) {
printf("Queue is empty.\n");
return;
}
printf("Queue:\n");
int i = q->front;
while (i != q->rear) {
printf("Customer %d: %d minutes.\n", q->data[i].id, q->data[i].time);
i = (i + 1) % MAX_QUEUE_SIZE;
}
}
int main() {
Queue q;
initQueue(&q);
int id = 0;
srand(time(NULL));
while (1) {
printf("Customer %d arrives.\n", id);
Customer c = {id, rand() % 10 + 1};
enQueue(&q, c);
printQueue(&q);
while (!isQueueEmpty(&q)) {
Customer c = deQueue(&q);
printf("Customer %d is served for %d minutes.\n", c.id, c.time);
printQueue(&q);
}
printf("Customer %d leaves.\n", id);
id++;
}
return 0;
}
```
该代码使用队列来管理用户排队情况,每个用户包含一个编号和需要办理业务的时间。每次用户到达时,生成一个随机的业务办理时间,并将用户加入队列。每次窗口空闲时,从队列中取出一个用户进行业务办理,直到队列为空。然后该用户离开,等待下一个用户到达。