用c语言设计并实现一个银行窗口排队管理系统,模拟用户排队、办理业务结束后 离队等过程。具体要求如下: 1.窗口配置及状态管理 窗口应有编号、业务类型、排队队列、工作人员编号(唯一)等属 性; 窗口数量无限制;
时间: 2023-12-24 18:05:33 浏览: 165
下面是一个简单的银行窗口排队管理系统的实现,包括窗口、顾客和队列三个类:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_WINDOW_NUM 10
#define MAX_QUEUE_LENGTH 100
// 窗口状态
enum WindowStatus {
IDLE,
BUSY,
PAUSED
};
// 业务类型
enum ServiceType {
SERVICE_A,
SERVICE_B,
SERVICE_C
};
// 顾客
struct Customer {
int number; // 顾客编号
enum ServiceType service_type; // 顾客需要办理的业务类型
time_t enter_time; // 顾客进入队列的时间
time_t start_time; // 顾客开始服务的时间
time_t end_time; // 顾客完成服务的时间
};
// 队列
struct Queue {
int front; // 队头
int rear; // 队尾
struct Customer *customers; // 顾客数组
};
// 窗口
struct Window {
int number; // 窗口编号
enum ServiceType service_type; // 窗口可以处理的业务类型
struct Queue queue; // 窗口前的队列
enum WindowStatus status; // 窗口状态
int worker_number; // 工作人员编号
};
// 初始化队列
void init_queue(struct Queue *queue) {
queue->front = queue->rear = 0;
queue->customers = (struct Customer *)malloc(sizeof(struct Customer) * MAX_QUEUE_LENGTH);
}
// 判断队列是否为空
int is_queue_empty(const struct Queue *queue) {
return queue->front == queue->rear;
}
// 判断队列是否已满
int is_queue_full(const struct Queue *queue) {
return (queue->rear + 1) % MAX_QUEUE_LENGTH == queue->front;
}
// 添加顾客到队列
void enqueue(struct Queue *queue, const struct Customer *customer) {
if (is_queue_full(queue)) {
printf("Error: queue is full!\n");
return;
}
queue->customers[queue->rear] = *customer;
queue->rear = (queue->rear + 1) % MAX_QUEUE_LENGTH;
}
// 从队列中取出顾客
struct Customer dequeue(struct Queue *queue) {
if (is_queue_empty(queue)) {
printf("Error: queue is empty!\n");
exit(1);
}
struct Customer customer = queue->customers[queue->front];
queue->front = (queue->front + 1) % MAX_QUEUE_LENGTH;
return customer;
}
// 初始化窗口
void init_window(struct Window *window, int number, enum ServiceType service_type, int worker_number) {
window->number = number;
window->service_type = service_type;
init_queue(&(window->queue));
window->status = IDLE;
window->worker_number = worker_number;
}
// 判断窗口是否空闲
int is_window_idle(const struct Window *window) {
return window->status == IDLE;
}
// 判断窗口是否正在服务中
int is_window_busy(const struct Window *window) {
return window->status == BUSY;
}
// 判断窗口是否已暂停服务
int is_window_paused(const struct Window *window) {
return window->status == PAUSED;
}
// 将窗口状态设置为空闲
void set_window_idle(struct Window *window) {
window->status = IDLE;
}
// 将窗口状态设置为正在服务
void set_window_busy(struct Window *window) {
window->status = BUSY;
}
// 将窗口状态设置为暂停服务
void set_window_paused(struct Window *window) {
window->status = PAUSED;
}
// 将顾客加入窗口前的队列
void add_customer_to_window_queue(struct Window *window, const struct Customer *customer) {
enqueue(&(window->queue), customer);
}
// 开始服务第一个顾客
void start_service(struct Window *window) {
if (is_window_busy(window) || is_queue_empty(&(window->queue))) {
return;
}
struct Customer customer = dequeue(&(window->queue));
customer.start_time = time(NULL);
set_window_busy(window);
printf("窗口 %d 开始为顾客 %d 办理业务\n", window->number, customer.number);
}
// 完成服务
void finish_service(struct Window *window) {
if (is_window_idle(window)) {
return;
}
set_window_idle(window);
printf("窗口 %d 完成业务\n", window->number);
}
// 处理顾客
void process_customer(struct Window *window, struct Customer *customer) {
add_customer_to_window_queue(window, customer);
if (is_window_idle(window)) {
start_service(window);
}
}
// 主函数
int main() {
// 初始化窗口
struct Window windows[MAX_WINDOW_NUM];
for (int i = 0; i < MAX_WINDOW_NUM; i++) {
init_window(&(windows[i]), i + 1, SERVICE_A, i + 1);
}
// 模拟顾客到来
struct Customer customer1 = {.number = 1, .service_type = SERVICE_A, .enter_time = time(NULL)};
process_customer(&(windows[0]), &customer1);
struct Customer customer2 = {.number = 2, .service_type = SERVICE_B, .enter_time = time(NULL)};
process_customer(&(windows[1]), &customer2);
struct Customer customer3 = {.number = 3, .service_type = SERVICE_C, .enter_time = time(NULL)};
process_customer(&(windows[2]), &customer3);
// 模拟服务
sleep(5);
finish_service(&(windows[0]));
sleep(3);
finish_service(&(windows[1]));
start_service(&(windows[1]));
sleep(4);
finish_service(&(windows[1]));
sleep(2);
finish_service(&(windows[2]));
return 0;
}
```
这个实现只是一个简单的示例,可以根据实际需求进行扩展和改进。
阅读全文