数据结构(c语言)课设4——校园导游咨询(图)
题目描述:
设计一个校园导游程序,为来访的客人提供各种信息查询服务。
(1)设计学校的校园平面图,所含景点不少于10个,以图中顶点表示校内各景点,存放景点名称、代号、简介等信息;以边表示路径,存放路径长度等相关信息;
(2)提供基本信息的修改功能;
(3)为来访客人提供图中任意景点相关信息的查询;
(4)为来访客人提供景点的问路查询,即已知一个景点,查询到某景点之间的一条最短路径及长度。
#include <iostream>
using namespace std;
#define MAXInt 32767
#define MAXNUM 100
typedef struct
{
string Name;
string Num;
string Info;
}places;
typedef struct//邻接矩阵存储
{
places vexs[MAXNUM];
int arcs[MAXNUM][MAXNUM];
int vexnum, arcnum;
}TGraph;
void CreateUDN(TGraph &G, int DNum, int BNum)//创建
{
G.vexnum = DNum;
G.arcnum = BNum;
for(int i = 0; i < G.vexnum; ++i)
{
cin>>G.vexs[i].Num>>G.vexs[i].Name>>G.vexs[i].Info;//输入顶点信息
}
for(int i = 0; i < G.vexnum; ++i)//初始化
{
for(int j = 0; j < G.vexnum; ++j)
{
G.arcs[i][j] = MAXInt;
}
}
for(int k = 0; k < G.arcnum; ++k)
{
int v1, v2, w;
cin>>v1>>v2>>w;
int i = v1 - 1, j = v2 - 1;
G.arcs[i][j] = w;
G.arcs[j][i] = G.arcs[i][j];
}
}
void Query(TGraph &G, string name)//查询
{
for(int i = 0; i < G.vexnum; i++)
{
if(G.vexs[i].Name == name)
{
cout<<G.vexs[i].Num<<" "<<G.vexs[i].Name<<" "<<G.vexs[i].Info<<endl;
}
}
}
int LocateVex(TGraph &G, string v)//返回顶点的下标
{
for(int i = 0; i < G.vexnum; ++i)
{
if(v == G.vexs[i].Name)
{
return i;
}
}
return -1;
}
int D[MAXNUM][MAXNUM];
int Path[MAXNUM][MAXNUM];
void print_Floyd(TGraph G, int i, int j) { //根据Floyd算法利用递归输出最短路线PATH
if(Path[i][j] == -1)
cout << G.vexs[j].Name;
else {
print_Floyd(G, i, Path[i][j]);//递归
cout << "->" << G.vexs[j].Name;
}
}
void ShortestPath_Floyd(TGraph &G, string name1, string name2)//最短路径
{
int n = LocateVex(G, name1);
int m = LocateVex(G, name2);
for(int i = 0; i < G.vexnum; ++i)
{
for(int j = 0; j < G.vexnum; ++j)
{
D[i][j] = G.arcs[i][j];
if(D[i][j] != MAXInt && i != j)
{
Path[i][j] = i;
}
else
{
Path[i][j] = -1;
}
}
}
for(int i = 0; i < G.vexnum; i++)
D[i][i] = 0;
for(int k = 0; k < G.vexnum; ++k)
{
for(int i = 0; i < G.vexnum; ++i)
{
for(int j = 0; j < G.vexnum; ++j)
{
if(D[i][k] + D[k][j] < D[i][j])
{
D[i][j] = D[i][k] + D[k][j];
Path[i][j] = Path[k][j];
}
}
}
}
cout << D[n][m] << endl;
print_Floyd(G, n, m);
cout << endl;
}
void Modify(TGraph &G, string name1, string num, string name2, string info)
{
places p;
int u = LocateVex(G, name1);
p.Num = num;
p.Name = name2;
p.Info = info;
G.vexs[u] = p;
}
int main()
{
TGraph G;
int a;
string str1, str2, str3, str4, str5;
int op, t;
int m, n;
cin>>m>>n;
CreateUDN(G, m, n);
cin>>op;
while(op--)
{
cin>>str1;
if(str1 == "Query")
{
cin>>t;
if(t == 1)
{
a = 1;
}
else if(t == 2)
{
a = 2;
}
}
else if(str1 == "Modify")
{
a = 3;
}
switch(a)
{
case 1:
{
cin>>str2;
Query(G, str2);
break;
}
case 2:
{
cin>>str2>>str3;
ShortestPath_Floyd(G, str2, str3);
break;
}
case 3:
{
cin>>str2>>str3>>str4>>str5;
Modify(G, str2, str3, str4, str5);
break;
}
default:break;
}
}
return 0;
}
/*5 6
TS001 Library good
TS002 Gymnasium good
TS003 IndustrialTrainingCentre good
TS004 T-PARK good
TS005 WetlandPark good
1 2 1
1 3 3
2 3 4
3 4 3
4 5 1
3 5 1
4
Query 1 Library
Modify Library TS001 Library nice
Query 1 Library
Query 2 Library T-PARK*/