银行排队系统c语言实现
时间: 2023-11-18 21:46:26 浏览: 64
银行排队系统的C语言实现可以分为以下几个部分:
1. 客户信息管理:包括客户的基本信息、账户信息、存款信息等。可以使用结构体或者数组存储客户信息。
2. 排队管理:使用队列数据结构来管理客户的排队,包括排队的进队和出队操作。
3. 服务窗口管理:使用数组或者结构体存储服务窗口的信息,包括服务窗口的编号、服务状态、正在服务的客户信息等。
4. 界面设计:使用图形界面或者命令行界面来实现银行排队系统的交互,包括客户信息录入、排队、服务窗口选择等。
下面是一个简单的银行排队系统的C语言实现示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_QUEUE_SIZE 100
#define MAX_WINDOW_NUM 10
#define MAX_NAME_LEN 20
#define MAX_ACCOUNT_NUM 10
// 客户信息结构体
typedef struct {
char name[MAX_NAME_LEN]; // 姓名
char account[MAX_ACCOUNT_NUM]; // 账号
float deposit; // 存款金额
} Customer;
// 队列结构体
typedef struct {
int front; // 队头指针
int rear; // 队尾指针
Customer data[MAX_QUEUE_SIZE]; // 队列数据
} Queue;
// 服务窗口结构体
typedef struct {
int id; // 窗口编号
int status; // 窗口状态,0表示空闲,1表示忙碌
Customer customer; // 正在服务的客户
} Window;
// 全局变量
Queue queue; // 队列
Window windows[MAX_WINDOW_NUM]; // 服务窗口数组
// 初始化队列
void initQueue(Queue *queue) {
queue->front = queue->rear = 0;
}
// 判断队列是否为空
int isQueueEmpty(Queue *queue) {
return queue->front == queue->rear;
}
// 判断队列是否已满
int isQueueFull(Queue *queue) {
return (queue->rear + 1) % MAX_QUEUE_SIZE == queue->front;
}
// 入队操作
void enqueue(Queue *queue, Customer customer) {
if (isQueueFull(queue)) {
printf("Queue is full!\n");
return;
}
queue->data[queue->rear] = customer;
queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
}
// 出队操作
Customer dequeue(Queue *queue) {
if (isQueueEmpty(queue)) {
printf("Queue is empty!\n");
return;
}
Customer customer = queue->data[queue->front];
queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
return customer;
}
// 初始化服务窗口
void initWindows(Window windows[], int n) {
for (int i = 0; i < n; i++) {
windows[i].id = i + 1;
windows[i].status = 0;
}
}
// 查找空闲服务窗口
Window *findFreeWindow(Window windows[], int n) {
for (int i = 0; i < n; i++) {
if (windows[i].status == 0) {
return &windows[i];
}
}
return NULL;
}
// 录入客户信息
Customer inputCustomerInfo() {
Customer customer;
printf("Enter customer name: ");
scanf("%s", customer.name);
printf("Enter customer account: ");
scanf("%s", customer.account);
printf("Enter deposit amount: ");
scanf("%f", &customer.deposit);
return customer;
}
// 显示客户信息
void displayCustomerInfo(Customer customer) {
printf("Name: %s\n", customer.name);
printf("Account: %s\n", customer.account);
printf("Deposit: %.2f\n", customer.deposit);
}
// 显示服务窗口信息
void displayWindowInfo(Window *window) {
printf("Window %d: ", window->id);
if (window->status == 0) {
printf("Free\n");
} else {
printf("Serving customer %s\n", window->customer.name);
}
}
// 显示所有服务窗口信息
void displayAllWindowInfo(Window windows[], int n) {
for (int i = 0; i < n; i++) {
displayWindowInfo(&windows[i]);
}
}
int main() {
int n; // 服务窗口数量
int choice; // 操作选项
Customer customer; // 客户信息
Window *window; // 服务窗口指针
printf("Enter number of windows: ");
scanf("%d", &n);
initQueue(&queue);
initWindows(windows, n);
while (1) {
printf("\n1. New customer\n");
printf("2. Serve customer\n");
printf("3. Display queue\n");
printf("4. Display windows\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
customer = inputCustomerInfo();
enqueue(&queue, customer);
printf("Customer %s has been added to the queue.\n", customer.name);
break;
case 2:
window = findFreeWindow(windows, n);
if (window == NULL) {
printf("No free window available!\n");
} else if (isQueueEmpty(&queue)) {
printf("Queue is empty!\n");
} else {
customer = dequeue(&queue);
window->status = 1;
window->customer = customer;
printf("Customer %s is being served at window %d.\n", customer.name, window->id);
}
break;
case 3:
if (isQueueEmpty(&queue)) {
printf("Queue is empty!\n");
} else {
printf("Queue:\n");
for (int i = queue.front; i != queue.rear; i = (i + 1) % MAX_QUEUE_SIZE) {
displayCustomerInfo(queue.data[i]);
}
}
break;
case 4:
displayAllWindowInfo(windows, n);
break;
case 0:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
```
该示例实现了基本的客户信息管理、排队管理、服务窗口管理和界面设计,可以根据需求进行扩展和优化。