【C语言程序设计试验与习题指导】试验十三 3 [未完成]

#include <stdio.h>
#include <assert.h>
#define MAX_N 10

int in_car_num = 0;

struct cars{
    int id;
    int in_time;
};

void come_in_car(int c_id, int c_time, int number_of_parking, struct cars *in_car, struct cars *waiting_car);
void init_cars(struct cars *car_ptr, int num);
int find_available_parking_index(struct cars *car_ptr);
void pass_space(void);

int main(void){
    struct cars in_car[MAX_N], waiting_car[MAX_N];
    int number_of_parking, c_id, c_time, is_break;
    char order;
    init_cars(in_car, MAX_N); init_cars(waiting_car, MAX_N);
    is_break = 0;
    scanf("%d", &number_of_parking);
    pass_space();
    while (1) {
        if (is_break) {
            break;
        }
        scanf("%c%d%d", &order, &c_id, &c_time);
        pass_space();
        switch (order) {
            case 'A':
                come_in_car(c_id, c_time, number_of_parking, in_car, waiting_car);
                break;
            case 'D':
                ;
                break;
            case 'E':
                is_break = 1;
                break;
            default:
                printf("你的输入命令有错\n");
                break;
        }
        
    }
    
    return 0;
}

void pass_space(void){
    while (getchar()!= '\n');
}

/* 初始化车辆信息 */
void init_cars(struct cars *car_ptr, int num){
    for (; num > 0; num--) {
        car_ptr->id = -1;
        car_ptr++;
    }
}

/* 查找可用的车位索引*/
int find_available_parking_index(struct cars *car_ptr){
    int i;
    for (i = 0; i < MAX_N; i++) {
        if (car_ptr->id == -1){
            return i;
        }
        car_ptr++;
    }
    assert(0);
    return 0;
}


/* 停车等级信息处理 */
void come_in_car(int c_id, int c_time, int number_of_parking, struct cars *in_car, struct cars *waiting_car){
    int c_index;
    if (in_car_num < number_of_parking) {
        c_index = find_available_parking_index(in_car);
        in_car[c_index].id = c_id;
        in_car[c_index].in_time = c_time;
        in_car_num++;
        printf("%d号车停入%d号位\n", c_id, c_index+1);
    }else{
        printf("%d号车在变道上等待\n", c_id);
        c_index = find_available_parking_index(waiting_car);
        waiting_car[c_index].id = c_id;
    }
}

 

posted @ 2021-10-09 20:15  就是想学习  阅读(38)  评论(0编辑  收藏  举报