c语言 银行排队系统 VIP和普通用户分别窗口 代码

时间: 2024-01-17 08:04:53 浏览: 27
以下是一个简单的C语言银行排队系统的代码示例,其中包括了VIP和普通用户分别窗口的实现: ```c #include <stdio.h> #include <stdlib.h> // 定义队列 #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int front; // 队头指针 int rear; // 队尾指针 } Queue; // 初始化队列 void initQueue(Queue *q) { q->front = 0; q->rear = 0; } // 入队 void enQueue(Queue *q, int x) { if ((q->rear + 1) % MAXSIZE == q->front) { printf("Queue is full.\n"); exit(0); } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; } // 出队 int deQueue(Queue *q) { if (q->front == q->rear) { printf("Queue is empty.\n"); exit(0); } int x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return x; } // 判断队列是否为空 int isEmpty(Queue q) { return q.front == q.rear; } // 主函数 int main() { Queue vipQueue, normalQueue; initQueue(&vipQueue); initQueue(&normalQueue); int n, m; printf("Please input the number of VIP users: "); scanf("%d", &n); printf("Please input the number of normal users: "); scanf("%d", &m); // VIP用户进入队列 for (int i = 0; i < n; i++) { printf("Please input the arrival time and service time of VIP user %d: ", i+1); int arrivalTime, serviceTime; scanf("%d %d", &arrivalTime, &serviceTime); if (isEmpty(vipQueue)) { enQueue(&vipQueue, arrivalTime); } else { // 按照到达时间插入队列 int j = vipQueue.rear-1; while (j >= vipQueue.front && vipQueue.data[j] > arrivalTime) { vipQueue.data[j+1] = vipQueue.data[j]; j--; } vipQueue.data[j+1] = arrivalTime; } } // 普通用户进入队列 for (int i = 0; i < m; i++) { printf("Please input the arrival time and service time of normal user %d: ", i+1); int arrivalTime, serviceTime; scanf("%d %d", &arrivalTime, &serviceTime); if (isEmpty(normalQueue)) { enQueue(&normalQueue, arrivalTime); } else { // 按照到达时间插入队列 int j = normalQueue.rear-1; while (j >= normalQueue.front && normalQueue.data[j] > arrivalTime) { normalQueue.data[j+1] = normalQueue.data[j]; j--; } normalQueue.data[j+1] = arrivalTime; } } // 开始服务 int currentTime = 0; // 当前时间 int vipIndex = 0; // VIP队列中等待服务的用户的下标 int normalIndex = 0; // 普通队列中等待服务的用户的下标 while (!isEmpty(vipQueue) || !isEmpty(normalQueue)) { int vipArrivalTime = MAXSIZE, normalArrivalTime = MAXSIZE; if (!isEmpty(vipQueue)) vipArrivalTime = vipQueue.data[vipQueue.front]; if (!isEmpty(normalQueue)) normalArrivalTime = normalQueue.data[normalQueue.front]; // 选择最早到达的用户 int arrivalTime = vipArrivalTime < normalArrivalTime ? vipArrivalTime : normalArrivalTime; currentTime = arrivalTime; // VIP用户优先服务 if (!isEmpty(vipQueue) && vipArrivalTime <= normalArrivalTime && vipQueue.data[vipIndex] <= currentTime) { printf("VIP user %d is being serviced at VIP window.\n", vipIndex+1); vipIndex++; } else if (!isEmpty(normalQueue) && normalQueue.data[normalIndex] <= currentTime) { printf("Normal user %d is being serviced at normal window.\n", normalIndex+1); normalIndex++; } // 处理完服务时间最短的用户 if (!isEmpty(vipQueue) && vipArrivalTime <= normalArrivalTime && vipQueue.data[vipIndex-1] <= currentTime) { deQueue(&vipQueue); } else if (!isEmpty(normalQueue) && normalQueue.data[normalIndex-1] <= currentTime) { deQueue(&normalQueue); } } return 0; } ``` 以上代码仅供参考,具体实现方式和细节还需要根据实际需求进行调整和完善。

相关推荐

最新推荐

recommend-type

C语言实现银行管理系统

主要为大家详细介绍了C语言实现银行管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

餐馆点菜系统C语言源代码

主要为大家详细介绍了餐馆点菜系统C语言源代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

银行客户排队等候系统模拟

(1)输入的形式和输入值的范围:客户进行排队时输入1、2、3分别代表VIP客户、理财客户、一般客户三种类型的客户,以得到相对应的排队号;银行端每处理完一位用户,输入Y,使得系统重新打印当前排队队列; (2)输出...
recommend-type

C语言实现简单航班管理系统

主要为大家详细介绍了C语言实现简单航班管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
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://www.mathworks.com/discovery/image-segmentation/_jcr_content/mainParsys3/discoverysubsection_1185333930/mainParsys3/image_copy.adapt.full.medium.jpg/1712813808277.jpg) # 1. MATLAB开根号的理论基础 开根号运算在数学和科学计算中无处不在。在MATLAB中,开根号可以通过多种函数实现,包括`sqrt()`和`nthroot()`。`sqrt()`函数用于计算正实数的平方根,而`nt
recommend-type

react的函数组件的使用

React 的函数组件是一种简单的组件类型,用于定义无状态或者只读组件。 它们通常接受一个 props 对象作为参数并返回一个 React 元素。 函数组件的优点是代码简洁、易于测试和重用,并且它们使 React 应用程序的性能更加出色。 您可以使用函数组件来呈现简单的 UI 组件,例如按钮、菜单、标签或其他部件。 您还可以将它们与 React 中的其他组件类型(如类组件或 Hooks)结合使用,以实现更复杂的 UI 交互和功能。
recommend-type

JSBSim Reference Manual

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