以下是使用队列实现上述问题的C代码:

#include <stdio.h>
#include <stdlib.h>

#define MAX_QUEUE_SIZE 100

// 客户业务类型
typedef enum { DEPOSIT, WITHDRAW, LOSS_REPORT, REPAYMENT } BusinessType;

// 客户信息结构体
typedef struct {
    int arrivalTime;  // 到达时间
    int number;       // 编号
    BusinessType businessType;  // 业务类型
} Customer;

// 队列结构体
typedef struct {
    int front;  // 队头指针
    int rear;   // 队尾指针
    Customer data[MAX_QUEUE_SIZE];  // 队列元素
} Queue;

// 初始化队列
void initQueue(Queue *queue) {
    queue->front = 0;
    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("Error: Queue is full.\n");
        exit(1);
    }
    queue->data[queue->rear] = customer;
    queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
}

// 出队列
Customer dequeue(Queue *queue) {
    if (isQueueEmpty(queue)) {
        printf("Error: Queue is empty.\n");
        exit(1);
    }
    Customer customer = queue->data[queue->front];
    queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
    return customer;
}

// 模拟银行服务过程
void simulateBankService(int numWindows, int serviceTime[]) {
    Queue waitingQueue;
    initQueue(&waitingQueue);
    int numCustomers = 0;
    int totalWaitingTime = 0;
    int numCustomersServed[numWindows];
    int numBusinessTypeServed[numWindows][4];

    // 初始化统计数据
    for (int i = 0; i < numWindows; i++) {
        numCustomersServed[i] = 0;
        for (int j = 0; j < 4; j++) {
            numBusinessTypeServed[i][j] = 0;
        }
    }

    // 读取客户信息文件
    FILE *file = fopen("customer_info.txt", "r");
    if (file == NULL) {
        printf("Error: Failed to open file.\n");
        exit(1);
    }

    // 模拟银行服务
    int currentTime = 0;
    while (!feof(file) || !isQueueEmpty(&waitingQueue)) {
        // 处理等待队列中的客户
        for (int i = 0; i < numWindows; i++) {
            if (!isQueueEmpty(&waitingQueue)) {
                if (numCustomersServed[i] == 0) {
                    Customer customer = dequeue(&waitingQueue);
                    int waitingTime = currentTime - customer.arrivalTime;
                    totalWaitingTime += waitingTime;
                    numCustomersServed[i]++;
                    numBusinessTypeServed[i][customer.businessType]++;
                    printf("Window %d: Customer %d served. Waiting time: %d\n", i + 1, customer.number, waitingTime);
                }
            }
        }

        // 处理到达的新客户
        if (!feof(file)) {
            Customer customer;
            fscanf(file, "%d %d %d", &customer.arrivalTime, &customer.number, &customer.businessType);
            enqueue(&waitingQueue, customer);
            numCustomers++;
            printf("New customer %d arrived. Business type: %d\n", customer.number, customer.businessType);
        }

        // 更新时间
        currentTime++;
    }

    // 关闭文件
    fclose(file);

    // 输出统计结果
    printf("\n");
    printf("Average waiting time: %.2f\n", (float)totalWaitingTime / numCustomers);
    for (int i = 0; i < numWindows; i++) {
        printf("Window %d: Served %d customers\n", i + 1, numCustomersServed[i]);
        printf("Business type breakdown: Deposit: %d, Withdraw: %d, Loss report: %d, Repayment: %d\n",
               numBusinessTypeServed[i][DEPOSIT], numBusinessTypeServed[i][WITHDRAW],
               numBusinessTypeServed[i][LOSS_REPORT], numBusinessTypeServed[i][REPAYMENT]);
    }
}

int main() {
    int numWindows;
    int serviceTime[4];

    // 读取营业时间和服务时间文件
    FILE *file = fopen("business_time.txt", "r");
    if (file == NULL) {
        printf("Error: Failed to open file.\n");
        exit(1);
    }
    fscanf(file, "%d", &numWindows);
    for (int i = 0; i < 4; i++) {
        fscanf(file, "%d", &serviceTime[i]);
    }
    fclose(file);

    // 模拟银行服务过程
    simulateBankService(numWindows, serviceTime);

    return 0;
}

其中,customer_info.txt文件用于存储客户信息,格式为:

到达时间 编号 业务类型
...

business_time.txt文件用于存储营业时间和服务时间,格式为:

窗口数
存款服务时间 取款服务时间 挂失服务时间 还贷服务时间

请根据实际需要创建并填充这两个文件

设银行有四个服务窗口一个等待队列每个窗口均可以办理存款、取款、挂失、还贷等业务每种业务所需的服务时间不同。客户到达银行后先到打号机上打号号票上包括到达时间、编号和需要办理的业务然后在银行内等候当任一服务窗口空闲时处理等候客户中排在最前面的客户的业务。通过模拟方法求出客户在银行内逗留的平均时间和每个窗口办理的客户数及办理的每种业务数。文件取名为bankc要求:用队列实现营业时间由用户设定号票上的信息

原文地址: http://www.cveoy.top/t/topic/hQxr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录