数据结构与算法综合实验(二)之图与景区信息管理系统实践(一)

实验目的

1、掌握图的定义和图的存储结构。
2、掌握图的创建方法和图的应用。
3、使用C++语言,定义图的数据结构,结合迭代开发思路实现“景区信息管理系统”。

需要注意的是,此次试验我在Graph.h中对图的定义与PPT里所给的有些许差异,并且在graph的定义上,为了避免多次链接的问题,采用了extern进行声明的形式。

main.cpp
主程序源文件

#include"Tourism.h"
#include
using namespace std;
int main() {
	menu();
	return 0;
}

Graph.h
图头文件

#ifndef GRAPH_H
#define GRAPH_H
struct Vex {
	int num;    //景点编号
	char name[20];   //景点名字
	char infor[1024];  //景点介绍
	/*重载==运算符
	bool operator==(const Vex &t) {
		return num==t.num;
	}
	*/
};
struct Edge {
	int vex1,vex2;
	int weight; //权值
};
struct Graph {
	int map[20][20];   //邻接矩阵
	Vex vexs[20];  //顶点数组
	int VexNum;   //顶点个数
};
void Init();
int InsertVex(Vex sVex);
int InsertEdge(Edge sEdge);
Vex getVeX(int nVex);
int FindEdge(int nVex, Edge aEdge[]);
int GetVexnum();
#endif 



Graph.cpp
图源文件

#include "Graph.h"
#include
Graph graph;
using namespace std;
void Init() {
	for (int i = 0; i < 20; i++) {
		for (int j = 0; j < 20; j++) {
			if (i == j) graph.map[i][j] = 0;
			else graph.map[i][j] = 0xffff;
		}
	}
	graph.VexNum = 0;
}
int InsertVex(Vex sVex) {
	if (graph.VexNum >= 20) return 0;
	graph.vexs[graph.VexNum] = sVex;
	graph.VexNum++;
	return 1;
}
int InsertEdge(Edge sEdge) {
	int num1 = sEdge.vex1;
	int num2 = sEdge.vex2;
	if (num1 != num2) {
		graph.map[num1][num2] = sEdge.weight;
		graph.map[num2][num1] = sEdge.weight;
		return 1;
	}
	else return 0;
}
Vex getVeX(int nVex) {
	return graph.vexs[nVex];
}
int FindEdge(int nVex, Edge aEdge[]) {
	int k = 0;
	for (int i = 0; i < graph.VexNum; i++) {
		if (graph.map[i][nVex] > 0 && graph.map[i][nVex] < 0xffff) {
			aEdge[k].vex1 = nVex;
			aEdge[k].vex2 = i;
			aEdge[k].weight = graph.map[i][nVex];
			k++;
		}
	}
	return k;
}
int GetVexnum() {
	return graph.VexNum;
}

Tourism.h
旅游头文件

#ifndef TOURISM_H
#define TOURISM_H
void CreateGraph();
void GetSpotInfo();
void menu();
#endif

Tourism.cpp
旅游源文件

#include
#include
#include"Tourism.h"
#include"Graph.h"
using namespace std;
extern Graph graph;
void CreateGraph() {
	ifstream is;
	Init();
	is.open("Vex.txt");
	int n;
	is >> n;
	cout << "顶点数目:" << n << endl;
	cout << "----- 顶点 -----" << endl;
	for (int i = 0; i < n; i++) {
		Vex vex;
		is >> vex.num >> vex.name >> vex.infor;
		cout << vex.num << "-" << vex.name << endl;
		if (InsertVex(vex) == 0) {
			cout << "读取文件信息失败!" << endl;
			break;
		}
	}
	is.close();
	is.open("Edge.txt");
	Edge edges[190];
	int k = 0;
	cout << "----- 边 -----" << endl;
	while (is >> edges[k].vex1 >> edges[k].vex2 >> edges[k].weight) {
		cout << " " << edges[k].weight << endl;
		if (InsertEdge(edges[k]) == 0) {
			cout << "读取文件信息失败!" << endl;
			break;
		}
		k++;
	}
	for (int i = 0; i < k; i++) {
		int num1 = edges[i].vex1;
		int num2 = edges[i].vex2;
		int weight = edges[i].weight;
		graph.map[num1][num2] = weight;
		graph.map[num2][num1] = weight;
	}
	is.close();
}
void GetSpotInfo() {
	cout << "===== 查询景点信息 =====" << endl;
	for (int i = 0; i < graph.VexNum; i++) {
		cout << graph.vexs[i].num << "-" << graph.vexs[i].name << endl;
	}
	cout << "请输入想要查询的景点编号:";
	int k;
	cin >> k;
	for (int i = 0; i < graph.VexNum; i++) {
		if (graph.vexs[i].num == k) {
			cout << graph.vexs[i].name << endl;
			cout << graph.vexs[i].infor << endl;
			break;
		}
	}
	cout << "----- 周边景区 -----" << endl;
	Edge aEdges[180];
	int m = FindEdge(k, aEdges);
	cout << m << endl;
	for (int i = 0; i < m; i++) {
		cout << graph.vexs[aEdges[i].vex1].name << "->" << graph.vexs[aEdges[i].vex2].name << " " << aEdges[i].weight << "m" << endl;
	}
}
void menu() {
	cout << "===== 景区信息管理系统 =====" << endl;
	cout << "1.创建景区景点图" << endl;
	cout << "2.查询景点信息" << endl;
	cout << "3.旅游景点导航" << endl;
	cout << "4.搜索最短路径" << endl;
	cout << "5.铺设电路规划" << endl;
	cout << "0.退出" << endl;
	cout << "请输入操作编号(0~5):";
	int i;
	cin >> i;
	switch (i) {
	case 0:exit(0); break;
	case 1: {
		CreateGraph(); 
		cout << "景区景点图创建完成!" << endl;
		menu();
		break;
	}
	case 2: {
		GetSpotInfo();
		menu();
		break;
	}
	case 3://旅游景点导航
	case 4://搜索最短路径
	case 5://铺设电路规划
	default: {
		cout << "请重新输入0~5的数字:";
		menu();
		break;
	}
	}
}

你可能感兴趣的:(数据结构)