①列车到达,首先判断站台是否已满,若有空位置,提示列车可以进站; ②输入进站列车序号,列车进站; ③若没有空位,提示站台已满; ④若列车从站台离开,输出离站列车的序号和站台剩余空位个数; ⑤在列车进站前和出站后,可查询站台空位的个数。c语言代码
时间: 2024-03-16 11:42:15 浏览: 14
以下是符合要求的C语言代码:
```c
#include <stdio.h>
int main() {
int station_capacity = 10; // 站台容量
int current_passengers = 0; // 当前站台上的乘客数量
while (1) {
// 判断站台是否已满
if (current_passengers < station_capacity) {
printf("列车可以进站,当前站台空位:%d\n", station_capacity - current_passengers);
} else {
printf("站台已满,列车无法进站\n");
break;
}
// 输入进站列车序号
int train_number;
printf("请输入进站列车序号:");
scanf("%d", &train_number);
// 列车进站
current_passengers++;
printf("列车%d进站,当前站台空位:%d\n", train_number, station_capacity - current_passengers);
// 判断站台是否已满
if (current_passengers >= station_capacity) {
printf("站台已满,无法继续进站\n");
break;
}
// 查询站台空位
printf("列车进站前,当前站台空位:%d\n", station_capacity - current_passengers);
// 列车离站
current_passengers--;
printf("列车%d离站,当前站台空位:%d\n", train_number, station_capacity - current_passengers);
// 查询站台空位
printf("列车离站后,当前站台空位:%d\n", station_capacity - current_passengers);
}
return 0;
}
```
在上述代码中,使用了while循环来模拟列车进站、离站的过程。首先定义了站台容量和当前站台上的乘客数量,然后使用while循环来判断站台是否已满。如果当前站台上的乘客数量小于站台容量,则提示列车可以进站,并让用户输入进站列车的序号。列车进站后,当前站台上的乘客数量加1,并输出当前站台的空位数量。如果当前站台上的乘客数量大于等于站台容量,则提示站台已满,退出循环。在列车进站前和出站后,使用printf语句查询当前站台的空位数量。列车离站后,当前站台上的乘客数量减1,并输出当前站台的空位数量。最后通过return语句结束程序。