写一个公交出行路线查询系统,实现如下基本功能:公交线路查询、路程耗费计算、最佳出行路线推荐等。 要求:系统运行正常、功能完整;数据结构使用得当,算法有较高的效率;代码规范、可读性高,结构清晰;具备一定的健壮性、可靠性和可维护性。
时间: 2023-07-10 13:33:04 浏览: 44
以下是一个简单的公交出行路线查询系统的代码示例,实现了公交线路查询、路程耗费计算、最佳出行路线推荐等基本功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STATION_NUM 50
#define MAX_ROUTE_NUM 10
// 公交站点结构体
typedef struct {
char name[20]; // 站名
int distance; // 距离
} Station;
// 公交线路结构体
typedef struct {
char name[20]; // 线路名
int station_num; // 站点数量
Station stations[MAX_STATION_NUM]; // 站点数组
} Route;
// 所有公交线路数组
Route routes[MAX_ROUTE_NUM];
// 获取指定站点在指定线路中的位置
int get_station_index(char* station_name, Route* route) {
int i;
for (i = 0; i < route->station_num; i++) {
if (strcmp(station_name, route->stations[i].name) == 0) {
return i;
}
}
return -1;
}
// 计算两站之间的距离
int get_distance(char* start_station, char* end_station, Route* route) {
int start_index = get_station_index(start_station, route);
int end_index = get_station_index(end_station, route);
if (start_index == -1 || end_index == -1) {
return -1;
}
if (start_index > end_index) {
int temp = start_index;
start_index = end_index;
end_index = temp;
}
int distance = 0;
int i;
for (i = start_index; i < end_index; i++) {
distance += route->stations[i+1].distance - route->stations[i].distance;
}
return distance;
}
// 获取两个站点之间的最短距离
int get_shortest_distance(char* start_station, char* end_station) {
int i, j;
int shortest_distance = -1;
for (i = 0; i < MAX_ROUTE_NUM; i++) {
if (routes[i].station_num == 0) {
break;
}
int start_index = get_station_index(start_station, &routes[i]);
int end_index = get_station_index(end_station, &routes[i]);
if (start_index != -1 && end_index != -1) {
if (start_index > end_index) {
int temp = start_index;
start_index = end_index;
end_index = temp;
}
int distance = 0;
for (j = start_index; j < end_index; j++) {
distance += routes[i].stations[j+1].distance - routes[i].stations[j].distance;
}
if (shortest_distance == -1 || distance < shortest_distance) {
shortest_distance = distance;
}
}
}
return shortest_distance;
}
// 获取两个站点之间的最短路线
void get_shortest_route(char* start_station, char* end_station, char* shortest_route) {
int i, j, k;
int shortest_distance = -1;
for (i = 0; i < MAX_ROUTE_NUM; i++) {
if (routes[i].station_num == 0) {
break;
}
int start_index = get_station_index(start_station, &routes[i]);
int end_index = get_station_index(end_station, &routes[i]);
if (start_index != -1 && end_index != -1) {
if (start_index > end_index) {
int temp = start_index;
start_index = end_index;
end_index = temp;
}
int distance = 0;
for (j = start_index; j < end_index; j++) {
distance += routes[i].stations[j+1].distance - routes[i].stations[j].distance;
}
if (shortest_distance == -1 || distance < shortest_distance) {
shortest_distance = distance;
strcpy(shortest_route, routes[i].name);
strcat(shortest_route, ": ");
for (k = start_index; k <= end_index; k++) {
strcat(shortest_route, routes[i].stations[k].name);
if (k < end_index) {
strcat(shortest_route, "->");
}
}
}
}
}
}
int main() {
// 初始化公交线路数组
int i;
for (i = 0; i < MAX_ROUTE_NUM; i++) {
routes[i].station_num = 0;
}
// 添加公交线路1
Route route1;
strcpy(route1.name, "1号线");
route1.station_num = 4;
Station station11 = {"站点1", 0};
Station station12 = {"站点2", 3};
Station station13 = {"站点3", 6};
Station station14 = {"站点4", 9};
route1.stations[0] = station11;
route1.stations[1] = station12;
route1.stations[2] = station13;
route1.stations[3] = station14;
routes[0] = route1;
// 添加公交线路2
Route route2;
strcpy(route2.name, "2号线");
route2.station_num = 5;
Station station21 = {"站点1", 0};
Station station22 = {"站点2", 2};
Station station23 = {"站点3", 4};
Station station24 = {"站点4", 6};
Station station25 = {"站点5", 8};
route2.stations[0] = station21;
route2.stations[1] = station22;
route2.stations[2] = station23;
route2.stations[3] = station24;
route2.stations[4] = station25;
routes[1] = route2;
// 公交线路查询
printf("请输入公交线路名称:\n");
char route_name[20];
scanf("%s", route_name);
for (i = 0; i < MAX_ROUTE_NUM; i++) {
if (routes[i].station_num == 0) {