学校数据结构的课程实验之一。
用到的数据结构:无向带权图
用到的算法:Floyd最短路径算法,深度优先搜索(递归实现)
需求分析:
设计一个校园导航程序,为访客提供各种信息查询服务:
1. 以图中各顶点表示校内各单位地点,存放单位名称,代号,简介等信息;以边表示路径,存放路径长度等相关信息。
2. 图中任意单位地点相关信息的查询。
3. 图中任意单位的问路查询,即查询任意两个单位之间的一条最短的路径。
4. 从图中任意单位地点出发的一条深度优先遍历路径。
主函数:
#include <iostream>
#include "School.h"
using namespace std;
int main()
{
School mySchool=School("School.txt");
char choice='y';
while(choice=='y')
{
cout << "请选择操作"<<endl;
cout << "--------------------------------" << endl;
cout << "1----查看地点列表" << endl;
cout << "2----查看地点简介" << endl;
cout << "3----查询最短路径" << endl;
cout << "4----从一点出发遍历" << endl;
cout << "5----任意一点的可直达地点" << endl;
cout << "--------------------------------" << endl;
int option;
cin >> option;
switch (option)
{
case 1: mySchool.display(); break;
case 2: mySchool.search(); break;
case 3: mySchool.path(); break;
case 4: mySchool.traverse(); break;
case 5: mySchool.reachable(); break;
}
cout << "继续吗?[y/n]";
cin >> choice;
}
return 0;
}
学校类:
#include <fstream>
#include <string>
#include "Digraph.h"
using namespace std;
struct Place//地点定义
{
int number;
string name;
string introduction;
Place(){}
Place(int num,string nam,string intro):number(num),name(nam),introduction(intro){}
void print()//显示此地点的信息
{
cout<<"---------------------------"<<endl;
cout<<"编号:"<<number<<endl;
cout<<"地点名:"<<name<<endl;
cout<<"简介:"<<introduction<<endl;
cout<<"---------------------------"<<endl;
}
};
ostream& operator<<(ostream &os, Place &aPlace)//重载输出运算符
{
os << aPlace.number << ":" << aPlace.name;
return os;
}
ofstream outFile;//输出流
class School
{
private:
int total;//总的地点数
Digraph<Place,13> places_graph;
void readFile(