C语言银行排队系统问题

时间: 2024-03-16 13:37:45 浏览: 22
银行排队系统是一个常见的仿真问题,可以用C语言实现。以下是一个基本的单队列、多窗口的银行排队系统的实现,包括客户进入排队系统、客户离开、查询当前客户前面还有几人、查询截至目前总共办理多少客户等功能: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX_QUEUE_SIZE 100 // 队列最大长度 #define MAX_WINDOW_NUM 10 // 窗口最大数量 typedef struct { int id; // 客户编号 int type; // 客户类型,0表示普通客户,1表示VIP客户 int arrive_time; // 到达时间 int service_time; // 服务时间 int start_time; // 开始服务时间 int end_time; // 结束服务时间 } Customer; typedef struct { Customer data[MAX_QUEUE_SIZE]; // 队列数据 int front; // 队头指针 int rear; // 队尾指针 } Queue; typedef struct { int id; // 窗口编号 int type; // 窗口类型,0表示普通窗口,1表示VIP窗口 int busy; // 窗口是否忙碌,0表示空闲,1表示忙碌 int start_time; // 开始服务时间 int end_time; // 结束服务时间 } Window; int customer_id = 0; // 客户编号计数器 int total_customer_num = 0; // 总共办理客户数量 int total_wait_time = 0; // 总共等待时间 int total_service_time = 0; // 总共服务时间 int window_num = 0; // 窗口数量 Window windows[MAX_WINDOW_NUM]; // 窗口数组 Queue queue; // 队列 // 初始化队列 void init_queue(Queue *q) { q->front = q->rear = 0; } // 判断队列是否为空 int is_queue_empty(Queue *q) { return q->front == q->rear; } // 判断队列是否已满 int is_queue_full(Queue *q) { return (q->rear + 1) % MAX_QUEUE_SIZE == q->front; } // 入队 void enqueue(Queue *q, Customer c) { if (is_queue_full(q)) { printf("Queue is full!\n"); return; } q->data[q->rear] = c; q->rear = (q->rear + 1) % MAX_QUEUE_SIZE; } // 出队 Customer dequeue(Queue *q) { if (is_queue_empty(q)) { printf("Queue is empty!\n"); exit(1); } Customer c = q->data[q->front]; q->front = (q->front + 1) % MAX_QUEUE_SIZE; return c; } // 初始化窗口 void init_windows() { for (int i = 0; i < window_num; i++) { windows[i].id = i + 1; windows[i].type = 0; windows[i].busy = 0; windows[i].start_time = 0; windows[i].end_time = 0; } } // 获取空闲窗口 int get_idle_window() { for (int i = 0; i < window_num; i++) { if (windows[i].busy == 0) { return i; } } return -1; } // 获取最早结束服务的窗口 int get_earliest_window() { int earliest = 0; for (int i = 1; i < window_num; i++) { if (windows[i].end_time < windows[earliest].end_time) { earliest = i; } } return earliest; } // 获取VIP窗口 int get_vip_window() { for (int i = 0; i < window_num; i++) { if (windows[i].type == 1 && windows[i].busy == 0) { return i; } } return -1; } // 生成随机数 int random_int(int min, int max) { return rand() % (max - min + 1) + min; } // 生成客户 Customer generate_customer(int arrive_time) { Customer c; c.id = ++customer_id; c.type = random_int(0, 1); c.arrive_time = arrive_time; c.service_time = random_int(1, 10); c.start_time = 0; c.end_time = 0; return c; } // 处理客户 void process_customer(Customer c) { int idle_window = get_idle_window(); if (idle_window != -1) { // 有空闲窗口 windows[idle_window].busy = 1; windows[idle_window].type = c.type; windows[idle_window].start_time = c.arrive_time; windows[idle_window].end_time = c.arrive_time + c.service_time; c.start_time = windows[idle_window].start_time; c.end_time = windows[idle_window].end_time; total_service_time += c.service_time; total_wait_time += c.start_time - c.arrive_time; } else { // 没有空闲窗口,加入队列 enqueue(&queue, c); } } // 处理窗口 void process_window(int current_time) { for (int i = 0; i < window_num; i++) { if (windows[i].busy && windows[i].end_time == current_time) { // 窗口服务结束 windows[i].busy = 0; total_customer_num++; if (!is_queue_empty(&queue)) { // 队列不为空,取出队首客户进行服务 Customer c = dequeue(&queue); windows[i].busy = 1; windows[i].type = c.type; windows[i].start_time = current_time; windows[i].end_time = current_time + c.service_time; c.start_time = windows[i].start_time; c.end_time = windows[i].end_time; total_service_time += c.service_time; total_wait_time += c.start_time - c.arrive_time; } } } } // 打印客户信息 void print_customer(Customer c) { printf("Customer %d: type=%d, arrive_time=%d, service_time=%d, start_time=%d, end_time=%d\n", c.id, c.type, c.arrive_time, c.service_time, c.start_time, c.end_time); } // 打印窗口信息 void print_window(Window w) { printf("Window %d: type=%d, busy=%d, start_time=%d, end_time=%d\n", w.id, w.type, w.busy, w.start_time, w.end_time); } // 打印客户队列信息 void print_queue(Queue *q) { printf("Queue: "); for (int i = q->front; i != q->rear; i = (i + 1) % MAX_QUEUE_SIZE) { printf("%d ", q->data[i].id); } printf("\n"); } // 打印窗口信息 void print_windows() { for (int i = 0; i < window_num; i++) { print_window(windows[i]); } } // 打印排队系统信息 void print_system(int current_time) { printf("Current time: %d\n", current_time); printf("Total customer num: %d\n", total_customer_num); printf("Total wait time: %d\n", total_wait_time); printf("Total service time: %d\n", total_service_time); printf("Average wait time: %.2f\n", (float) total_wait_time / total_customer_num); printf("Window status:\n"); print_windows(); printf("Queue status:\n"); print_queue(&queue); } int main() { srand(time(NULL)); printf("Please input the number of windows: "); scanf("%d", &window_num); init_windows(); init_queue(&queue); int current_time = 0; while (current_time < 480) { // 一天的运行时间为480分钟 Customer c = generate_customer(current_time); printf("Customer %d arrives at %d, type=%d, service_time=%d\n", c.id, c.arrive_time, c.type, c.service_time); process_customer(c); print_system(current_time); process_window(current_time); current_time++; } printf("Simulation ends.\n"); return 0; } ```

相关推荐

最新推荐

recommend-type

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

客户到银行办理业务,...银行端每处理完一位用户,输入Y,使得系统重新打印当前排队队列; (2)输出的形式:输出当前所有排队用户的客户信息以及号数; (3)程序所能达到的功能:演示出较为合理的银行等候系统模拟;
recommend-type

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板
recommend-type

wireshark安装教程入门

wireshark安装教程入门
recommend-type

基于C++负数据库的隐私保护在线医疗诊断系统

【作品名称】:基于C++负数据库的隐私保护在线医疗诊断系统 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 基于负数据库的隐私保护在线医疗诊断系统 NDBMedicalSystem 客户端及服务器端 本项目是在保护用户隐私的前提下,完成了对新冠肺炎、乳腺癌、眼疾等多种疾病的智能诊断。
recommend-type

基本的嵌入式操作系统给

任务管理
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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

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