#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("你的输入命令有错 "); break; } } return 0; } void pass_space(void){ while (getchar()!= ' '); } /* 初始化车辆信息 */ 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号位 ", c_id, c_index+1); }else{ printf("%d号车在变道上等待 ", c_id); c_index = find_available_parking_index(waiting_car); waiting_car[c_index].id = c_id; } }