信言0 2023-12-09 16:09 采纳率: 100%
浏览 20
已结题

c语言停车场模型。。

1.设置n个入口
2.t个出口
3.共f个车位
4.5个车位为一个小型立体车库,只用散列表进行模拟入库及出库
5.若停满车则在便道等候
6.15分钟以下免费,15分钟以上白天0.5元15分钟,晚上1元15分钟
7.可通过管理员页面更以上功能

  • 写回答

8条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-12-09 16:09
    关注


    以下引自强大而免费的微信小程序:皆我知道

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define MAX_CARPORTS 10 // 最大车库数
    #define MAX_CAPACITY 10 // 最大每个车库的车位数
    #define MAX_PLATE_LEN 10 // 最大车牌号长度
    
    typedef struct {
        char plate[MAX_PLATE_LEN];
        time_t in_time;
    } Car;
    
    typedef struct {
        int capacity;
        Car cars[MAX_CAPACITY];
    } CarPort;
    
    CarPort carports[MAX_CARPORTS];
    int carport_num = 0; // 实际车库数
    
    void init_carports() {
        printf("请输入车库个数:");
        scanf("%d", &carport_num);
    
        for (int i = 0; i < carport_num; i++) {
            printf("请输入第 %d 个车库的车位个数:", i + 1);
            scanf("%d", &carports[i].capacity);
        }
    }
    
    int find_carport(const char *plate) {
        for (int i = 0; i < carport_num; i++) {
            for (int j = 0; j < carports[i].capacity; j++) {
                if (strcmp(plate, carports[i].cars[j].plate) == 0) {
                    return i;
                }
            }
        }
        return -1;
    }
    
    int find_carport_by_plate(const char *plate, int carport) {
        for (int j = 0; j < carports[carport].capacity; j++) {
            if (strcmp(plate, carports[carport].cars[j].plate) == 0) {
                return j;
            }
        }
        return -1;
    }
    
    void print_carports() {
        for (int i = 0; i < carport_num; i++) {
            printf("车库 %d 车位情况:", i + 1);
            for (int j = 0; j < carports[i].capacity; j++) {
                if (strlen(carports[i].cars[j].plate) > 0) {
                    printf("%s ", carports[i].cars[j].plate);
                }
            }
            printf("\n");
        }
    }
    
    void print_waiting_cars() {
        printf("等待车辆:");
        for (int i = 0; i < carport_num; i++) {
            for (int j = carports[i].capacity; j < MAX_CAPACITY; j++) {
                if (strlen(carports[i].cars[j].plate) > 0) {
                    printf("%s ", carports[i].cars[j].plate);
                }
            }
        }
        printf("\n");
    }
    
    void in_car() {
        int carport;
        char plate[MAX_PLATE_LEN];
    
        printf("请输入车库编号(1-%d):", carport_num);
        scanf("%d", &carport);
        carport--;
    
        if (carport < 0 || carport >= carport_num) {
            printf("车库编号无效\n");
            return;
        }
    
        printf("请输入车牌号:");
        scanf("%s", plate);
    
        int index = find_carport(plate);
        if (index >= 0) {
            printf("车辆 %s 已在库\n", plate);
            return;
        }
    
        for (int i = 0; i < carports[carport].capacity; i++) {
            if (strlen(carports[carport].cars[i].plate) == 0) {
                strcpy(carports[carport].cars[i].plate, plate);
                carports[carport].cars[i].in_time = time(NULL);
                printf("车辆 %s 入库成功\n", plate);
                print_carports();
                print_waiting_cars();
                return;
            }
        }
    
        for (int i = carports[carport].capacity; i < MAX_CAPACITY; i++) {
            if (strlen(carports[carport].cars[i].plate) == 0) {
                strcpy(carports[carport].cars[i].plate, plate);
                carports[carport].cars[i].in_time = time(NULL);
                printf("车辆 %s 在便道等候\n", plate);
                print_carports();
                print_waiting_cars();
                return;
            }
        }
    
        printf("车库 %d 已满,无法入库\n", carport + 1);
    }
    
    void out_car() {
        char plate[MAX_PLATE_LEN];
    
        printf("请输入出库车牌号:");
        scanf("%s", plate);
    
        int carport = find_carport(plate);
        if (carport < 0) {
            printf("车辆 %s 不在库\n", plate);
            return;
        }
    
        int index = find_carport_by_plate(plate, carport);
        if (index < 0) {
            printf("车辆 %s 不在库\n", plate);
            return;
        }
    
        time_t out_time = time(NULL);
        double duration = difftime(out_time, carports[carport].cars[index].in_time);
        double fee;
    
        if (duration <= 900) {
            fee = 0;
        } else {
            struct tm *out_tm = localtime(&out_time);
            int hour = out_tm->tm_hour;
    
            if (hour >= 6 && hour < 18) {
                fee = duration / 900 * 0.5;
            } else {
                fee = duration / 900;
            }
        }
    
        printf("车辆 %s 出库成功,停车时长 %.1f 分钟,收费 %.2f 元\n", plate, duration / 60, fee);
    
        carports[carport].cars[index].plate[0] = '\0';
    
        for (int i = carports[carport].capacity; i < MAX_CAPACITY; i++) {
            if (strlen(carports[carport].cars[i].plate) > 0) {
                strcpy(carports[carport].cars[index].plate, carports[carport].cars[i].plate);
                carports[carport].cars[index].in_time = carports[carport].cars[i].in_time;
                carports[carport].cars[i].plate[0] = '\0';
                printf("等候车辆 %s 入库\n", carports[carport].cars[index].plate);
                print_carports();
                print_waiting_cars();
                return;
            }
        }
    
        print_carports();
        print_waiting_cars();
    }
    
    void menu() {
        int choice;
    
        do {
            printf("\n停车场管理系统\n");
            printf("1. 入库\n");
            printf("2. 出库\n");
            printf("3. 查看车库\n");
            printf("4. 查看等候车辆\n");
            printf("0. 退出\n");
            printf("请输入您的选择:");
            scanf("%d", &choice);
    
            switch (choice) {
                case 1:
                    in_car();
                    break;
                case 2:
                    out_car();
                    break;
                case 3:
                    print_carports();
                    break;
                case 4:
                    print_waiting_cars();
                    break;
                case 0:
                    printf("再见!\n");
                    break;
                default:
                    printf("无效选择\n");
                    break;
            }
        } while (choice != 0);
    }
    
    int main() {
        init_carports();
        menu();
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(7条)

报告相同问题?

问题事件

  • 系统已结题 12月17日
  • 已采纳回答 12月9日
  • 创建了问题 12月9日