C语言实现银行业务模拟问题,单队列,多窗口,键盘输入营业时间
时间: 2024-04-12 21:32:47 浏览: 68
基于c语言的银行业务模拟
4星 · 用户满意度95%
好的,我明白了。你想要实现一个银行业务模拟程序,使用单队列和多个窗口,并且需要键盘输入营业时间。我将为你提供一个基本的框架,你可以在此基础上进行扩展和进一步开发。
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_CUSTOMERS 100
typedef struct {
int id;
int arrival_time;
int service_time;
} Customer;
typedef struct {
Customer queue[MAX_CUSTOMERS];
int front;
int rear;
} Queue;
typedef struct {
Queue* queues;
int num_windows;
} Bank;
void init_queue(Queue* q) {
q->front = 0;
q->rear = -1;
}
bool is_queue_empty(Queue* q) {
return q->rear < q->front;
}
bool is_queue_full(Queue* q) {
return q->rear == MAX_CUSTOMERS - 1;
}
void enqueue(Queue* q, Customer customer) {
if (is_queue_full(q)) {
printf("Queue is full. Cannot enqueue customer.\n");
return;
}
q->rear++;
q->queue[q->rear] = customer;
}
Customer dequeue(Queue* q) {
if (is_queue_empty(q)) {
printf("Queue is empty. Cannot dequeue customer.\n");
return;
}
Customer customer = q->queue[q->front];
q->front++;
return customer;
}
void init_bank(Bank* bank, int num_windows) {
bank->queues = (Queue*)malloc(num_windows * sizeof(Queue));
for (int i = 0; i < num_windows; i++) {
init_queue(&(bank->queues[i]));
}
bank->num_windows = num_windows;
}
void simulate_bank(Bank* bank, int minutes) {
int current_minute = 0;
while (current_minute < minutes) {
printf("Minute %d:\n", current_minute);
// Process arrival of customers
if (rand() % 4 == 0) { // Randomly generate customer arrival every 4 minutes
Customer customer;
customer.id = rand() % 1000; // Randomly generate customer ID
customer.arrival_time = current_minute;
customer.service_time = 1 + rand() % 5; // Randomly generate service time (1-5 minutes)
int min_queue_length = MAX_CUSTOMERS;
int min_queue_index = 0;
// Find the queue with the minimum length
for (int i = 0; i < bank->num_windows; i++) {
int queue_length = bank->queues[i].rear - bank->queues[i].front + 1;
if (queue_length < min_queue_length) {
min_queue_length = queue_length;
min_queue_index = i;
}
}
enqueue(&(bank->queues[min_queue_index]), customer);
printf("Customer %d arrived and joined queue %d\n", customer.id, min_queue_index);
}
// Process service at each window
for (int i = 0; i < bank->num_windows; i++) {
if (!is_queue_empty(&(bank->queues[i]))) {
Customer customer = dequeue(&(bank->queues[i]));
customer.service_time--;
if (customer.service_time > 0) {
enqueue(&(bank->queues[i]), customer);
printf("Customer %d served at window %d (Remaining service time: %d)\n", customer.id, i, customer.service_time);
} else {
printf("Customer %d served and left window %d\n", customer.id, i);
}
}
}
current_minute++;
printf("\n");
}
}
int main() {
int num_windows;
int minutes;
printf("Enter the number of windows: ");
scanf("%d", &num_windows);
printf("Enter the simulation time in minutes: ");
scanf("%d", &minutes);
Bank bank;
init_bank(&bank, num_windows);
simulate_bank(&bank, minutes);
free(bank.queues);
return 0;
}
```
这个程序使用了 `Customer` 结构体来表示每个顾客的信息,包括顾客的ID、到达时间和服务时间。`Queue` 结构体用于实现队列,其中的 `front` 和 `rear` 分别表示队首和队尾的索引。`Bank` 结构体用于表示银行,包含了多个窗口的队列数组和窗口的数量。
程序中的 `simulate_bank` 函数用于模拟银行的运行过程,其中的随机数生成部分可以根据实际需求进行调整。在每个分钟,程序会首先处理顾客到达的情况,然后处理每个窗口的服务情况。
你可以根据需要修改和扩展这个程序,例如添加显示等待时间的功能或者更复杂的业务逻辑。希望这个程序能帮到你!
阅读全文