窗口排队状态、新增、暂停、删除功能c语言

时间: 2023-07-10 15:09:57 浏览: 50
下面是一个窗口排队状态、新增、暂停、删除功能的 C 语言代码示例: ```c #include <stdio.h> #include <stdlib.h> struct Customer { int id; int service_time; struct Customer *next; }; typedef struct { struct Customer *front; struct Customer *rear; } Queue; typedef struct { Queue *queue; int status; // 0:关闭,1:开放 } Window; void initQueue(Queue *q) { q->front = q->rear = NULL; } int isEmpty(Queue *q) { return q->front == NULL; } void enqueue(Queue *q, int id, int service_time) { struct Customer *customer = (struct Customer *) malloc(sizeof(struct Customer)); customer->id = id; customer->service_time = service_time; customer->next = NULL; if (q->rear == NULL) { q->front = q->rear = customer; } else { q->rear->next = customer; q->rear = customer; } } void dequeue(Queue *q) { if (isEmpty(q)) { return; } struct Customer *customer = q->front; q->front = q->front->next; if (q->front == NULL) { q->rear = NULL; } printf("顾客 %d 开始服务,服务时间为 %d\n", customer->id, customer->service_time); free(customer); } void openWindow(Window *window) { window->status = 1; } void closeWindow(Window *window) { window->status = 0; } void pauseWindow(Window *window) { closeWindow(window); } void resumeWindow(Window *window) { openWindow(window); } void addCustomer(Window *window, int id, int service_time) { if (window->status == 0) { printf("窗口已关闭,无法添加顾客\n"); return; } enqueue(window->queue, id, service_time); printf("顾客 %d 已加入排队\n", id); } void removeCustomer(Window *window) { if (isEmpty(window->queue)) { printf("当前无顾客排队\n"); return; } dequeue(window->queue); } void printQueueStatus(Window *window) { if (isEmpty(window->queue)) { printf("当前无顾客排队\n"); return; } Queue *q = window->queue; struct Customer *customer = q->front; printf("当前排队顾客:\n"); while (customer != NULL) { printf("顾客 %d,服务时间为 %d\n", customer->id, customer->service_time); customer = customer->next; } } int main() { Window window; Queue queue; window.queue = &queue; window.status = 1; initQueue(&queue); while (1) { printf("\n"); printf("请选择操作:\n"); printf("1. 查看当前排队状态\n"); printf("2. 新增顾客\n"); printf("3. 暂停排队\n"); printf("4. 恢复排队\n"); printf("5. 删除当前顾客\n"); printf("6. 关闭窗口\n"); printf("7. 退出程序\n"); int option; scanf("%d", &option); switch (option) { case 1: printQueueStatus(&window); break; case 2: { int id, service_time; printf("请输入顾客编号和服务时间:\n"); scanf("%d %d", &id, &service_time); addCustomer(&window, id, service_time); break; } case 3: pauseWindow(&window); printf("排队已暂停\n"); break; case 4: resumeWindow(&window); printf("排队已恢复\n"); break; case 5: removeCustomer(&window); break; case 6: closeWindow(&window); printf("窗口已关闭\n"); break; case 7: printf("程序已退出\n"); return 0; default: printf("无效操作\n"); break; } } } ``` 在这个示例中,我们使用了一个 `Window` 结构体来表示窗口,包含一个指向队列的指针和一个表示窗口状态的整数。使用 `initQueue` 函数初始化队列,`isEmpty` 函数判断队列是否为空,`enqueue` 函数将顾客加入队列,`dequeue` 函数从队列中取出顾客并开始服务。 新增顾客的 `addCustomer` 函数需要判断窗口状态是否为开放,删除顾客的 `removeCustomer` 函数需要判断队列是否为空。暂停和恢复排队的函数只需要修改窗口状态即可。 在 `main` 函数中,我们使用一个无限循环来等待用户输入操作选项。根据用户的选择调用不同的函数来执行相应的操作,直到用户选择退出程序。 这只是一个简单的示例,实际的排队管理系统可能需要更复杂的逻辑和功能。

相关推荐

最新推荐

recommend-type

C语言高效有限状态机(FSM)详细设计说明书.docx

有限状态机是一种用来进行对象行为建模的工具,其作用主要是描述对象在它的生命周期内所经历的状态序列,以及如何响应来自外界的各种事件。在计算机科学中,有限状态机被广泛用于建模应用行为、硬件电路系统设计、...
recommend-type

两例信捷C语言功能块应用案例.docx

C语言功能块的工作原理:进入参数修改页面后,瞬间通知功能块,把原参数的数据(数据求和)赋值给外部的一个软元件地址存放,然后可以检测是否修改了参数,如果任意一个参数别修改,则重新读到的参数和就与之前存放...
recommend-type

C语言实现任何文件的加密解密功能

主要为大家详细介绍了C语言实现任何文件的加密解密功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言使用openSSL库AES模块实现加密功能详解

主要介绍了C语言使用openSSL库AES模块实现加密功能,详细分析了C语言加密的相关概念、原理及AES模块加密具体实现技巧,需要的朋友可以参考下
recommend-type

C语言使用openSSL库DES模块实现加密功能详解

主要介绍了C语言使用openSSL库DES模块实现加密功能,简单讲解了DES加密的相关概念,并结合实例形式分析了DES加密的具体实现技巧,需要的朋友可以参考下
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用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

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