地铁查询系统c
时间: 2023-11-18 20:11:41 浏览: 56
地铁查询系统可以用C语言编写,以下是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STATION_NUM 100 // 最大站点数
// 定义一个站点结构体
typedef struct Station {
char name[20]; // 站点名称
int line; // 所属地铁线路
} Station;
// 定义一个地铁线路结构体
typedef struct SubwayLine {
int id; // 线路编号
char name[20]; // 线路名称
int station_num; // 站点数量
Station stations[MAX_STATION_NUM]; // 站点信息
} SubwayLine;
// 全局变量,存储所有地铁线路信息
SubwayLine subway_lines[] = {
{1, "Line 1", 3, {{"Station A", 1}, {"Station B", 1}, {"Station C", 1}}},
{2, "Line 2", 4, {{"Station D", 2}, {"Station E", 2}, {"Station F", 2}, {"Station G", 2}}},
{3, "Line 3", 5, {{"Station H", 3}, {"Station I", 3}, {"Station J", 3}, {"Station K", 3}, {"Station L", 3}}}
};
// 根据站点名称查找所在地铁线路
int find_line_by_station(const char* station_name) {
for (int i = 0; i < sizeof(subway_lines) / sizeof(SubwayLine); i++) {
for (int j = 0; j < subway_lines[i].station_num; j++) {
if (strcmp(subway_lines[i].stations[j].name, station_name) == 0) {
return subway_lines[i].id;
}
}
}
return -1; // 没有找到站点
}
// 根据地铁线路编号查找线路名称
const char* find_line_name_by_id(int line_id) {
for (int i = 0; i < sizeof(subway_lines) / sizeof(SubwayLine); i++) {
if (subway_lines[i].id == line_id) {
return subway_lines[i].name;
}
}
return NULL; // 没有找到线路
}
int main() {
char start_station[20];
char end_station[20];
printf("Please enter the start station: ");
scanf("%s", start_station);
printf("Please enter the end station: ");
scanf("%s", end_station);
int start_line = find_line_by_station(start_station);
int end_line = find_line_by_station(end_station);
if (start_line == -1 || end_line == -1) {
printf("Sorry, the station is not found.\n");
return 0;
}
if (start_line == end_line) {
printf("You are already on %s line, no need to transfer.\n", find_line_name_by_id(start_line));
} else {
printf("You need to transfer from %s line to %s line.\n", find_line_name_by_id(start_line), find_line_name_by_id(end_line));
}
return 0;
}
```
上面的代码定义了一个`Station`结构体,表示一个站点的信息,包括名称和所属地铁线路。还定义了一个`SubwayLine`结构体,表示一个地铁线路的信息,包括编号、名称、站点数量和站点信息。在`main`函数中,用户输入起点站和终点站,通过查找站点所在地铁线路,判断是否需要换乘,并输出结果。
你可以根据需求扩展这个示例代码,实现更完善的地铁查询系统。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)