c语言银行排队系统,功能包含窗口有编号,有业务类型,有排队队列,有工作人员编号,窗口数量无限制,窗口可以删除及增加
时间: 2024-03-11 22:50:29 浏览: 22
下面是一个包含窗口编号、业务类型、排队队列、工作人员编号、窗口数量无限制、窗口可以删除及增加的银行排队系统的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_QUEUE_SIZE 100 // 最多排队人数
#define MAX_WINDOW_NUM 100 // 最多窗口数量
// 定义排队人员结构体
typedef struct customer {
int id; // 顾客编号
int type; // 业务类型
} Customer;
// 定义队列结构体
typedef struct queue {
Customer data[MAX_QUEUE_SIZE];
int front;
int rear;
} Queue;
// 初始化队列
Queue *initQueue() {
Queue *q = (Queue *)malloc(sizeof(Queue));
q->front = q->rear = 0;
return q;
}
// 判断队列是否为空
int isQueueEmpty(Queue *q) {
return q->front == q->rear;
}
// 判断队列是否已满
int isQueueFull(Queue *q) {
return (q->rear + 1) % MAX_QUEUE_SIZE == q->front;
}
// 入队
void enQueue(Queue *q, Customer x) {
if (isQueueFull(q)) {
printf("队列已满,无法入队!\n");
return;
}
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
}
// 出队
Customer deQueue(Queue *q) {
Customer empty_customer = {-1, -1}; // 空顾客
if (isQueueEmpty(q)) {
printf("队列为空,无法出队!\n");
return empty_customer;
}
Customer x = q->data[q->front];
q->front = (q->front + 1) % MAX_QUEUE_SIZE;
return x;
}
// 打印队列
void printQueue(Queue *q) {
if (isQueueEmpty(q)) {
printf("队列为空!\n");
return;
}
printf("队列中的元素为:");
int i = q->front;
while (i != q->rear) {
printf("(%d,%d) ", q->data[i].id, q->data[i].type);
i = (i + 1) % MAX_QUEUE_SIZE;
}
printf("\n");
}
// 定义窗口结构体
typedef struct window {
int id; // 窗口编号
int type; // 业务类型
Queue *queue; // 排队队列
int worker_id; // 工作人员编号
} Window;
// 初始化窗口
Window *initWindow(int id) {
Window *w = (Window *)malloc(sizeof(Window));
w->id = id;
w->type = -1;
w->queue = initQueue();
w->worker_id = -1;
return w;
}
// 添加业务类型
void addType(Window *w, int type) {
w->type = type;
}
// 添加工作人员编号
void addWorkerID(Window *w, int worker_id) {
w->worker_id = worker_id;
}
// 打印窗口信息
void printWindow(Window *w) {
printf("窗口编号:%d,业务类型:%d,工作人员编号:%d\n", w->id, w->type, w->worker_id);
printf("当前排队队列:");
printQueue(w->queue);
}
// 定义窗口数组
Window *windows[MAX_WINDOW_NUM];
int window_num = 0;
// 添加窗口
void addWindow() {
if (window_num == MAX_WINDOW_NUM) {
printf("窗口数量已达到上限,无法添加!\n");
return;
}
Window *w = initWindow(window_num + 1); // 窗口编号从1开始
windows[window_num++] = w;
}
// 删除窗口
void deleteWindow(int id) {
if (id <= 0 || id > window_num) {
printf("无效的窗口编号!\n");
return;
}
free(windows[id - 1]); // 释放窗口占用的内存
for (int i = id; i < window_num; i++) {
windows[i - 1] = windows[i]; // 后面的窗口编号减1
}
window_num--;
}
// 查找空闲窗口
Window *findIdleWindow(int type) {
for (int i = 0; i < window_num; i++) {
if (windows[i]->type == type && isQueueEmpty(windows[i]->queue)) {
return windows[i];
}
}
return NULL;
}
// 处理顾客
void processCustomer(Customer c) {
Window *w = findIdleWindow(c.type);
if (w != NULL) { // 找到空闲窗口
enQueue(w->queue, c);
printf("顾客%d进入窗口%d排队等待处理!\n", c.id, w->id);
} else { // 找不到空闲窗口,需要排队等待
enQueue(windows[0]->queue, c); // 默认排到第一个窗口的队列中
printf("顾客%d进入窗口%d排队等待处理!\n", c.id, 1);
}
}
int main() {
int customer_num, type_num, worker_num;
printf("请输入顾客数量、业务类型数量、工作人员数量:");
scanf("%d%d%d", &customer_num, &type_num, &worker_num);
// 初始化窗口
for (int i = 0; i < worker_num; i++) {
addWindow();
}
// 添加业务类型
printf("请添加业务类型(格式:类型编号 业务名称):\n");
for (int i = 0; i < type_num; i++) {
int type;
char name[100];
scanf("%d%s", &type, name);
for (int j = 0; j < window_num; j++) {
addType(windows[j], type);
}
}
// 添加工作人员编号
printf("请添加工作人员编号(格式:窗口编号 工作人员编号):\n");
for (int i = 0; i < worker_num; i++) {
int id, worker_id;
scanf("%d%d", &id, &worker_id);
addWorkerID(windows[id - 1], worker_id);
}
// 处理顾客
printf("请依次输入每个顾客的业务类型编号(1~%d):\n", type_num);
for (int i = 1; i <= customer_num; i++) {
int type;
scanf("%d", &type);
Customer c = {i, type};
processCustomer(c);
}
// 打印窗口信息
printf("\n窗口信息如下:\n");
for (int i = 0; i < window_num; i++) {
printWindow(windows[i]);
}
// 逐个窗口处理顾客
for (int i = 0; i < window_num; i++) {
Window *w = windows[i];
printf("\n窗口%d处理顾客:\n", w->id);
while (!isQueueEmpty(w->queue)) {
Customer c = deQueue(w->queue);
printf("顾客%d处理完毕,离开窗口%d!\n", c.id, w->id);
}
}
// 删除窗口
printf("\n请输入要删除的窗口编号:");
int id;
scanf("%d", &id);
deleteWindow(id);
// 打印剩余窗口信息
printf("\n剩余窗口信息如下:\n");
for (int i = 0; i < window_num; i++) {
printWindow(windows[i]);
}
return 0;
}
```
以上代码中,使用了一个窗口数组来存储窗口信息,窗口编号从1开始。每个窗口有一个业务类型、一个排队队列、一个工作人员编号,可以动态添加和删除窗口。当一个顾客到来时,程序会查找空闲窗口来处理,如果找不到空闲窗口,顾客就需要排队等待。程序会打印每个窗口的排队队列和处理顾客的过程。最后,程序会让用户输入要删除的窗口编号,然后删除该窗口。