用c语言写一个银行排队系统
时间: 2023-10-10 14:09:40 浏览: 321
好的,我可以为您提供一个简单的银行排队系统的C语言实现。
首先,我们需要定义一个结构体来表示每个顾客的信息,包括顾客的姓名、银行卡号、到达时间、业务类型和等待时间。代码如下:
```
struct customer {
char name[20];
int card_number;
time_t arrival_time;
int service_type;
int waiting_time; // in minutes
};
```
接下来,我们可以定义一个队列来存储等待服务的顾客。为了简化问题,我们假设银行只有一个窗口,顾客按照到达时间的先后顺序依次接受服务。代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_CUSTOMERS 100
struct customer {
char name[20];
int card_number;
time_t arrival_time;
int service_type;
int waiting_time; // in minutes
};
struct node {
struct customer data;
struct node *next;
};
struct queue {
struct node *front;
struct node *rear;
};
void enqueue(struct queue *q, struct customer c) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = c;
new_node->next = NULL;
if (q->rear == NULL) {
q->front = new_node;
q->rear = new_node;
} else {
q->rear->next = new_node;
q->rear = new_node;
}
}
struct customer dequeue(struct queue *q) {
if (q->front == NULL) {
printf("Queue is empty.\n");
exit(1);
}
struct node *front_node = q->front;
struct customer c = front_node->data;
q->front = front_node->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(front_node);
return c;
}
int main() {
struct queue q = {NULL, NULL};
int n;
printf("Enter the number of customers: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
struct customer c;
printf("Enter the name of customer %d: ", i + 1);
scanf("%s", c.name);
printf("Enter the card number of customer %d: ", i + 1);
scanf("%d", &c.card_number);
c.arrival_time = time(NULL);
printf("Enter the service type of customer %d (1 for deposit, 2 for withdrawal): ", i + 1);
scanf("%d", &c.service_type);
c.waiting_time = 0;
enqueue(&q, c);
}
printf("Customers are waiting in the queue...\n");
while (q.front != NULL) {
struct customer c = dequeue(&q);
time_t now = time(NULL);
c.waiting_time += (int)(now - c.arrival_time) / 60;
printf("Serving customer %s (card number: %d, service type: %d, waiting time: %d minutes)\n",
c.name, c.card_number, c.service_type, c.waiting_time);
}
return 0;
}
```
在这个程序中,我们使用了队列来存储等待服务的顾客。通过调用`enqueue`函数将新到达的顾客加入队列,通过调用`dequeue`函数将队列中的顾客依次取出进行服务。在服务过程中,我们记录了每个顾客的等待时间,并在服务完成后打印出顾客的信息。
以上是一个简单的银行排队系统的C语言实现,希望可以帮助您了解如何使用队列来管理顾客的等待。
阅读全文