设银行有四个服务窗口,每个窗口均可以办理存款、取款、挂失、还贷业务,每种业务所需的服务时间不同,客户到达银行后,先到打号机上打号,号票上包括到达时间、编号和需要办理的业务,然后在银行内等候。每个窗口办理完一个客户的业务后,办理等候客户中排在最前面的客户的业务。写一个上述银行业务的模拟系统,通过模拟方法求出客户在银行内逗留的平均时间和每个窗口办理的客户数及办理的每种业务数。C语言代码
时间: 2024-03-03 20:49:31 浏览: 28
以下是一个简单的C语言代码实现:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WINDOW_NUM 4 // 窗口数量
#define BUSINESS_NUM 4 // 业务数量
#define MAX_CUSTOMER_NUM 100 // 最大客户数量
// 客户结构体
typedef struct {
int arrive_time; // 到达时间
int business_type; // 业务类型
int service_time; // 服务时间
int start_time; // 开始服务时间
int end_time; // 结束服务时间
} Customer;
int main() {
int i, j, k;
int current_time = 0; // 当前时间
int customer_num = 0; // 客户数量
int current_customer_num = 0; // 当前在银行内等待的客户数量
int customer_count[WINDOW_NUM][BUSINESS_NUM] = {0}; // 每个窗口办理每种业务的客户数
int business_count[BUSINESS_NUM] = {0}; // 每种业务的客户总数
int total_service_time[BUSINESS_NUM] = {0}; // 每种业务的总服务时间
float avg_stay_time = 0; // 平均逗留时间
Customer customer_list[MAX_CUSTOMER_NUM]; // 客户列表
// 随机生成客户到达时间和业务类型
srand((unsigned)time(NULL));
for (i = 0; i < MAX_CUSTOMER_NUM; i++) {
customer_list[i].arrive_time = current_time + rand() % 10;
customer_list[i].business_type = rand() % BUSINESS_NUM;
customer_list[i].service_time = rand() % 8 + 3;
current_time = customer_list[i].arrive_time;
customer_num++;
if (customer_num >= MAX_CUSTOMER_NUM) {
break;
}
}
// 模拟银行业务
current_time = 0;
while (current_customer_num < customer_num) {
// 处理客户到达事件
for (i = 0; i < customer_num; i++) {
if (customer_list[i].arrive_time == current_time) {
current_customer_num++;
}
}
// 处理银行业务
for (i = 0; i < WINDOW_NUM; i++) {
// 如果当前窗口没有客户,处理下一个窗口
if (customer_count[i][0] == 0 && customer_count[i][1] == 0 && customer_count[i][2] == 0 && customer_count[i][3] == 0) {
continue;
}
// 如果当前窗口有客户,处理这个客户的业务
for (j = 0; j < BUSINESS_NUM; j++) {
for (k = 0; k < customer_count[i][j]; k++) {
if (current_time >= customer_list[k].end_time) {
customer_count[i][j]--;
business_count[j]++;
total_service_time[j] += customer_list[k].end_time - customer_list[k].start_time;
}
}
}
// 处理等待客户中排在最前面的客户的业务
for (j = 0; j < BUSINESS_NUM; j++) {
for (k = 0; k < customer_num; k++) {
if (customer_list[k].arrive_time <= current_time && customer_list[k].start_time == 0 && customer_list[k].business_type == j) {
customer_list[k].start_time = current_time;
customer_list[k].end_time = current_time + customer_list[k].service_time;
customer_count[i][j]++;
break;
}
}
}
}
current_time++;
}
// 计算平均逗留时间和每个窗口办理的客户数及办理的每种业务数
for (i = 0; i < customer_num; i++) {
if (customer_list[i].start_time != 0) {
avg_stay_time += customer_list[i].end_time - customer_list[i].arrive_time;
}
}
avg_stay_time /= customer_num;
printf("平均逗留时间:%.2f\n", avg_stay_time);
printf("\n窗口办理的客户数:\n");
for (i = 0; i < WINDOW_NUM; i++) {
printf("窗口%d:", i + 1);
for (j = 0; j < BUSINESS_NUM; j++) {
printf("业务%d:%d ", j + 1, customer_count[i][j]);
}
printf("\n");
}
printf("\n办理的每种业务数:\n");
for (i = 0; i < BUSINESS_NUM; i++) {
printf("业务%d:%d\n", i + 1, business_count[i]);
}
return 0;
}
```
该程序通过随机生成客户到达时间和业务类型,模拟银行业务的过程,并计算出平均逗留时间和每个窗口办理的客户数及办理的每种业务数。