旅游景点咨询系统的设计与实现源代码c语言创建一个至少有15个点的有向网表示的某
时间: 2024-01-10 14:01:12 浏览: 78
旅游景点咨询系统的设计与实现源代码C语言的一个示例如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
#define MAX_DESCRIPTION_LENGTH 100
// 旅游景点结构体
typedef struct {
char name[MAX_NAME_LENGTH];
char description[MAX_DESCRIPTION_LENGTH];
} TouristSpot;
// 有向网的邻接矩阵表示
typedef struct {
int** adjacencyMatrix; // 邻接矩阵
int numVertices; // 顶点数
TouristSpot* spots; // 景点数组
} DirectedGraph;
// 创建有向网
DirectedGraph* createDirectedGraph(int numVertices) {
DirectedGraph* graph = malloc(sizeof(DirectedGraph));
graph->numVertices = numVertices;
// 分配邻接矩阵内存
graph->adjacencyMatrix = (int**)malloc(numVertices * sizeof(int*));
for (int i = 0; i < numVertices; i++) {
graph->adjacencyMatrix[i] = (int*)malloc(numVertices * sizeof(int));
memset(graph->adjacencyMatrix[i], 0, numVertices * sizeof(int));
}
// 分配景点数组内存
graph->spots = (TouristSpot*)malloc(numVertices * sizeof(TouristSpot));
return graph;
}
// 添加景点
void addTouristSpot(DirectedGraph* graph, int index, char* name, char* description) {
strncpy(graph->spots[index].name, name, MAX_NAME_LENGTH);
strncpy(graph->spots[index].description, description, MAX_DESCRIPTION_LENGTH);
}
// 添加边
void addEdge(DirectedGraph* graph, int start, int end) {
graph->adjacencyMatrix[start][end] = 1;
}
// 查询某个景点的名称和描述
void queryTouristSpot(DirectedGraph* graph, int index) {
printf("景点名称:%s\n", graph->spots[index].name);
printf("景点描述:%s\n", graph->spots[index].description);
}
int main() {
DirectedGraph* graph = createDirectedGraph(15);
// 添加景点
addTouristSpot(graph, 0, "景点A", "这是景点A的描述");
// ...
addTouristSpot(graph, 14, "景点O", "这是景点O的描述");
// 添加边
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
// ...
// 查询景点信息
queryTouristSpot(graph, 6);
// ...
return 0;
}
```
以上示例代码实现了一个旅游景点咨询系统的有向网的表示,其中使用邻接矩阵来表示有向边的连接关系,并存储了景点的名称和描述信息。可以根据具体需求自行修改和扩展代码。
阅读全文