C语言代码银行业务模拟问题 单队列多窗口,键盘输入营业时间,文件输入客户到达时间、序号、业务处理时间,求出客户在银行内逗留的平均时间和每个窗口办理的客户数及办理的每种业务数。(文件取名为bank.c) 要求:用队列实现,营业时间由用户设定,号票上的信息由文件输入。
时间: 2024-04-19 12:28:13 浏览: 157
你好!根据你的要求,我可以帮你编写一个使用队列实现的银行业务模拟程序。你可以提供以下信息:银行的营业时间、客户到达时间、序号和业务处理时间的文件。我将根据这些信息计算出客户在银行内逗留的平均时间,每个窗口办理的客户数以及每种业务的办理数。
这里是一个简单的示例程序,满足你的需求:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 100
typedef struct {
int arrival_time;
int sequence;
int service_time;
} Customer;
typedef struct {
Customer data[MAX_QUEUE_SIZE];
int front;
int rear;
} Queue;
void enqueue(Queue *queue, Customer customer) {
if ((queue->rear + 1) % MAX_QUEUE_SIZE == queue->front) {
printf("Queue is full\n");
return;
}
queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
queue->data[queue->rear] = customer;
}
Customer dequeue(Queue *queue) {
if (queue->front == queue->rear) {
printf("Queue is empty\n");
Customer empty_customer = {-1, -1, -1};
return empty_customer;
}
queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
return queue->data[queue->front];
}
int main() {
int business_hours;
printf("请输入银行的营业时间(单位:分钟):");
scanf("%d", &business_hours);
FILE *file = fopen("bank.txt", "r");
if (!file) {
printf("无法打开文件\n");
return 1;
}
Queue queue = {0, 0};
int total_customers = 0;
int total_service_time = 0;
int window_count = 0;
int *window_customers = (int *)calloc(business_hours, sizeof(int));
int *window_services = (int *)calloc(business_hours, sizeof(int));
int arrival_time, sequence, service_time;
while (fscanf(file, "%d %d %d", &arrival_time, &sequence, &service_time) != EOF) {
Customer customer = {arrival_time, sequence, service_time};
enqueue(&queue, customer);
total_customers++;
total_service_time += service_time;
}
fclose(file);
printf("\n");
printf("营业时间:%d分钟\n", business_hours);
printf("总客户数:%d\n", total_customers);
printf("总业务处理时间:%d分钟\n", total_service_time);
printf("客户在银行内逗留的平均时间:%.2f分钟\n", (float)total_service_time / total_customers);
printf("\n");
printf("窗口办理的客户数及办理的每种业务数:\n");
while (queue.front != queue.rear) {
Customer customer = dequeue(&queue);
printf("客户%d在窗口%d办理业务,耗时%d分钟\n", customer.sequence, window_count % business_hours + 1, customer.service_time);
window_customers[window_count % business_hours]++;
window_services[window_count % business_hours] += customer.service_time;
window_count++;
}
printf("\n");
for (int i = 0; i < business_hours; i++) {
printf("窗口%d办理的客户数:%d\n", i + 1, window_customers[i]);
printf("窗口%d办理的每种业务数:%d\n", i + 1, window_services[i]);
}
free(window_customers);
free(window_services);
return 0;
}
```
你需要在同一目录下创建一个名为 "bank.txt" 的文件,按照以下格式填入客户到达时间、序号和业务处理时间的信息:
```
到达时间 序号 业务处理时间
到达时间 序号 业务处理时间
...
```
请替换示例程序中的文件名和路径,以适应你的实际情况。希望这能对你有所帮助!如果有任何问题,请随时问我。
阅读全文