写C语言,用链表表示队列,哪个队列空闲先排队服务,客户哪个队列先结束了服务,就去服务新的下一个客户,有四个服务窗口有取款、存款、还贷、挂失一共四个服务类型,取款服务时间为8、存款服务时间为12、还贷服务时间为15、挂失服务时间为10,一共四个服务类型,用序号表示服务类型,客户选择服务类型,文件读取客户排队序号、到达的时间、需要服务的类型,在营业时间段,逗留时间等于客户到达时间加服务时间,计算客户在银行内逗留的平均时间和每个窗口办理的客户数及办理的每种业务数

时间: 2024-04-14 07:32:11 浏览: 7
以下是使用链表表示队列的C语言代码,实现根据服务类型和服务时间进行客户排队和服务的功能: ```c #include <stdio.h> #includestdlib.h> typedef struct { 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); } void processQueue(Queue* queues[], int windowType, int* totalCustomers, int* totalServiceTime, int* serviceTypeCount) { if (isQueueEmpty(queues[windowType])) { printf("Window %d is idle.\n", windowType); return; } Node* customer = queues[windowType]->front; (*totalCustomers)++; (*totalServiceTime) += customer->serviceTime; (*serviceTypeCount)++; printf("Window %d is serving customer %d (Service Type: %d, Service Time: %d).\n", windowType, customer->arrivalTime, customer->serviceType, customer->serviceTime); dequeue(queues[windowType]); } int main() { Queue* queues[4]; int serviceTime[4] = {8, 12, 15, 10}; int totalCustomers[4] = {0}; int totalServiceTime[4] = {0}; int serviceTypeCount[4] = {0}; int currentTime = 0; int totalCustomersCount = 0; int totalServiceTimeCount = 0; int serviceTypeCountTotal = 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); 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; processQueue(queues, shortestQueue, &totalCustomers[shortestQueue], &totalServiceTime[shortestQueue], &serviceTypeCount[shortestQueue]); } for (int i = 0; i < 4; i++) { totalCustomersCount += totalCustomers[i]; totalServiceTimeCount += totalServiceTime[i]; serviceTypeCountTotal += serviceTypeCount[i]; } float averageTime = (float)totalServiceTimeCount / totalCustomersCount; 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("Number of customers served by each service type:\n"); for (int i = 0; i < 4; i++) { printf("Service Type %d: %d\n", i+1, serviceTypeCount[i]); } return 0; } ``` 请确保在同一目录下存在名为 `customers.txt` 的文件,该文件应包含客户的到达时间和服务类型,每行一个客户,格式为 `到达时间 服务类型`,例如: ``` 1 2 3 1 5 3 7 4 ``` 运行以上代码后,将会输出每个窗口服务的客户信息、平均停留时间以及每个窗口和服务类型的客户数量。 需要注意的是,以上代码只提供了一种实现方式,可能还有其他更高效的实现方式。另外,代码中没有对输入文件的有效性进行检查,请确保输入文件的格式正确。

相关推荐

最新推荐

recommend-type

C语言链表题目(附答案).docx

大一初学C语言时的期末作业,涉及到链表的建立和功能的实现,涉及指针、函数、动态结构建立等方面的知识,初学者可以参考参考尝试尝试哟!!!
recommend-type

C语言数据结构实现链表逆序并输出

主要介绍了C语言数据结构实现链表逆序并输出的相关资料,需要的朋友可以参考下
recommend-type

C语言基于循环链表解决约瑟夫环问题的方法示例

主要介绍了C语言基于循环链表解决约瑟夫环问题的方法,简单描述了约瑟夫环问题并结合实例形式分析了C语言使用循环链表解决约瑟夫环问题的具体操作技巧,需要的朋友可以参考下
recommend-type

数据结构 实验二 链表、栈、队列的

数据结构 实验二 链表、栈、队列的数据结构 实验二 链表、栈、队列的数据结构 实验二 链表、栈、队列的数据结构 实验二 链表、栈、队列的数据结构 实验二 链表、栈、队列的
recommend-type

C语言实现带头结点的链表的创建、查找、插入、删除操作

主要介绍了C语言实现带头结点的链表的创建、查找、插入、删除操作方法,对于了解数据结构中链表的各项操作有很好的借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。