C语言,用链表表示队列,有新窗口先去新窗口服务,客户哪个队列先结束了服务,就去服务新的下一个客户,有四个服务窗口有取款、存款、还贷、挂失一共四个服务类型,取款服务时间为8、存款服务时间为12、还贷服务时间为15、挂失服务时间为10,一共四个服务类型,用序号表示服务类型,客户选择服务类型,文件读取客户排队序号、到达的时间、服务的类型,在营业时间段,逗留时间等于客户到达时间加服务时间,计算客户在银行内逗留的平均时间,统计每个窗口办理的每种业务数的数量和客户数量
时间: 2024-04-19 13:26:31 浏览: 66
C语言用队列的链式结构模拟银行排队问题
非常抱歉,我之前的回答有误。以下是经过修改后的代码,使用链表表示队列,并按照题目要求实现了客户排队和服务的功能,同时计算了客户在银行内逗留的平均时间以及每个窗口办理的每种业务数的数量和客户数量:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int arrivalTime;
int serviceType;
int serviceTime;
struct Node* next;
} Node;
typedef struct Queue {
Node* front;
Node* rear;
} Queue;
void initializeQueue(Queue* queue) {
queue->front = NULL;
queue->rear = NULL;
}
int isQueueEmpty(Queue* queue) {
return (queue->front == NULL);
}
void enqueue(Queue* queue, int arrivalTime, int serviceType, int serviceTime) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->arrivalTime = arrivalTime;
newNode->serviceType = serviceType;
newNode->serviceTime = serviceTime;
newNode->next = NULL;
if (isQueueEmpty(queue)) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
}
void dequeue(Queue* queue) {
if (isQueueEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
Node* temp = queue->front;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
}
free(temp);
}
float calculateAverageTime(int* totalServiceTime, int totalCustomers) {
if (totalCustomers == 0) {
return 0;
}
return (float)(*totalServiceTime) / totalCustomers;
}
void printWindowStats(int windowType, int totalCustomers, int* serviceTypeCount) {
printf("Window %d:\n", windowType);
printf("Total Customers: %d\n", totalCustomers);
printf("Service Type Count:\n");
for (int i = 0; i < 4; i++) {
printf("Service Type %d: %d\n", i + 1, serviceTypeCount[i]);
}
printf("\n");
}
int main() {
Queue* queues[4];
int serviceTime[4] = {8, 12, 15, 10};
int totalCustomers[4] = {0};
int totalServiceTime[4] = {0};
int serviceTypeCount[4][4] = {0};
for (int i = 0; i < 4; i++) {
queues[i] = (Queue*)malloc(sizeof(Queue));
initializeQueue(queues[i]);
}
FILE* file = fopen("customers.txt", "r");
if (file == NULL) {
printf("Failed to open file.\n");
return 1;
}
while (!feof(file)) {
int arrivalTime, serviceType;
fscanf(file, "%d %d", &arrivalTime, &serviceType);
if (serviceType >= 1 && serviceType <= 4) {
enqueue(queues[serviceType-1], arrivalTime, serviceType, serviceTime[serviceType-1]);
} else {
printf("Invalid service type for customer at arrival time %d.\n", arrivalTime);
}
}
fclose(file);
int currentTime = 0;
int totalCustomersCount = 0;
int totalServiceTimeCount = 0;
int serviceTypeCountTotal[4] = {0};
while (1) {
int shortestQueue = -1;
int shortestQueueLength = -1;
for (int i = 0; i < 4; i++) {
if (!isQueueEmpty(queues[i]) && (shortestQueueLength == -1 || shortestQueueLength > queues[i]->front->arrivalTime)) {
shortestQueue = i;
shortestQueueLength = queues[i]->front->arrivalTime;
}
}
if (shortestQueue == -1) {
break;
}
currentTime += queues[shortestQueue]->front->serviceTime;
Node* customer = queues[shortestQueue]->front;
totalCustomers[shortestQueue]++;
totalServiceTime[shortestQueue] += customer->arrivalTime + customer->serviceTime;
serviceTypeCount[shortestQueue][customer->serviceType - 1]++;
serviceTypeCountTotal[customer->serviceType - 1]++;
printf("Window %d is serving customer %d (Service Type: %d, Service Time: %d).\n", shortestQueue + 1, customer->arrivalTime, customer->serviceType, customer->serviceTime);
dequeue(queues[shortestQueue]);
}
for (int i = 0; i < 4; i++) {
totalCustomersCount += totalCustomers[i];
totalServiceTimeCount += totalServiceTime[i];
}
float averageTime = calculateAverageTime(&totalServiceTimeCount, totalCustomersCount);
printf("\n");
printf("Average time spent by customers in the bank: %.2f\n", averageTime);
printf("Number of customers served by each window:\n");
for (int i = 0; i < 4; i++) {
printf("Window %d: %d\n", i + 1, totalCustomers[i]);
}
printf("\n");
printf("Number of customers served by each service type:\n");
for (int i = 0; i < 4; i++) {
printf("Service Type %d: %d\n", i + 1, serviceTypeCountTotal[i]);
}
printf("\n");
for (int i = 0; i < 4; i++) {
printWindowStats(i + 1, totalCustomers[i], serviceTypeCount[i]);
}
return 0;
}
```
请确保在同一目录下存在名为 `customers.txt` 的文件,该文件应包含客户的到达时间和服务类型,每行一个客户,格式为 `到达时间 服务类型`,例如:
```
1 2
3 1
5 3
7 4
```
运行以上代码后,将会输出每个窗口服务的客户信息、平均停留时间以及每个窗口和服务类型的客户数量。
需要注意的是,以上代码只提供了一种实现方式,可能还有其他更高效的实现方式。另外,代码中没有对输入文件的有效性进行检查,请确保输入文件的格式正确。
阅读全文