数据结构课程设计校园导游.docx
《数据结构课程设计校园导游.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计校园导游.docx(14页珍藏版)》请在冰豆网上搜索。
数据结构课程设计校园导游
用无向网表示你所在学校的校园景点平面图,图中顶点表示主要景点,存放景点的编号、名称、简介等信息,图中的边表示景点间的道路,存放路径长度等信息。
要求能够回答有关景点介绍、游览路径等问题。
基本要求:
1、查询各景点的相关信息。
2、查询图中任意两个景点间的最短路径。
3、查询图中任意两个景点间的所有路径。
4、增加、删除、更新有关景点和道路的信息。
/*校园导游程序*//*[问题描述]
用无向网表示学校的校园景点平面图,图中顶点表示主要景点,
存放景点的编号、名称、简介等信息,图中的边表示景点间的道路,存放路径长度等信息。
要求能够回答有关景点介绍、游览路径等问题。
游客通过终端可询问:
(1)从某一景点到另一景点的最短路径。
(2)游客从公园进入,选取一条最佳路线。
(3)使游客可以不重复地浏览各景点,最后回到出口(出口就在入口旁边)。
[基本要求]
(1)将导游图看作一张带权无向图,顶点表示公园的各个景点,边表示各景点之间
的道路,
边上的权值表示距离.为此图选择适当的数据结构。
(2)把各种路径都显示给游客,由游客自己选择浏览路线。
(3)画出景点分布图于屏幕上。
[实现提示]
(1)构造一个无向图G并用邻接矩阵来存储。
(2)利用迪杰斯特拉算法来计算出起点到各个顶点之间的最短路径用二维数组p[i][]
来记录,
最短路径长度就用一维数组d[i]存放;i的范围:
0~20。
(3)一维数组have[]是用来记录最短路径出现顶点的顺序。
(4)根据起点和终点输出最短路径和路径长度。
*/
#defineINFINITY10000/*无穷大*/
#defineMAX_VERTEX_NUM40
#defineMAX40
#include
#include
#include
#include
typedefstructArCell
{
intadj;//路径长度
}ArCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedefstruct//图中顶点表示主要景点,存放景点的编号、名称、简介等信息,{
charname[30];
intnum;
charintroduction[100];//简介
}infotype;
typedefstruct
{
infotypevexs[MAX_VERTEX_NUM];
AdjMatrixarcs;
intvexnum,arcnum;
}MGraph;
MGraphb;
voidcmd(void);
MGraphInitGraph(void);
voidMenu(void);
voidBrowser(MGraph*G);
voidShortestPath_DIJ(MGraph*G);
voidFloyd(MGraph*G);
voidSearch(MGraph*G);
intLocateVex(MGraph*G,char*v);
MGraph*CreatUDN(MGraph*G);
voidprint(MGraph*G);
/******************************************************/
voidmain(void)
{
system("color1f");
system("modecon:
cols=140lines=130");
cmd();
}
/******************************************************/
voidcmd(void)
{
inti;
b=InitGraph();
Menu();
scanf("%d",&i);
while(i!
=5)
{
switch(i)
{
case1:
system("cls");Browser(&b);Menu();break;
case2:
system("cls");ShortestPath_DIJ(&b);Menu();break;
case3:
system("cls");Floyd(&b);Menu();break;
case4:
system("cls");Search(&b);Menu();break;
case5:
exit
(1);break;
default:
break;
}
scanf("%d",&i);
}
}
MGraphInitGraph(void)
{
MGraphG;
inti,j;
G.vexnum=10;
G.arcnum=14;
for(i=0;iG.vexs[i].num=i;
strcpy(G.vexs[0].name,"综合食堂");
strcpy(G.vexs[0].introduction,"新建标准化食堂");
strcpy(G.vexs[1].name,"东西办公楼");
strcpy(G.vexs[1].introduction,"全体教师办公场所,楼高12层,各种设施齐全");strcpy(G.vexs[2].name,"5号学生宿舍楼");
strcpy(G.vexs[2].introduction,"计算机系男生宿舍楼,苏式建筑");
strcpy(G.vexs[3].name,"医院");
strcpy(G.vexs[3].introduction,"校医院,设施不是很齐全,只能看小病,收费较贵");strcpy(G.vexs[4].name,"图书馆");
strcpy(G.vexs[4].introduction,"藏书60万册,设施良好,2楼为电子阅览室,环境幽雅");strcpy(G.vexs[5].name,"足球场");
strcpy(G.vexs[5].introduction,"现代化塑胶跑道,人造草坪,适宜锻炼身体的场所");strcpy(G.vexs[6].name,"沁园");
strcpy(G.vexs[6].introduction,"绿树成荫,适宜休息和读书");
strcpy(G.vexs[7].name,"主教学楼");
strcpy(G.vexs[7].introduction,"学院最大的教学楼,共5层,环形建筑,适宜学习");strcpy(G.vexs[8].name,"西教学楼");
strcpy(G.vexs[8].introduction,"学院第二大教学楼,环境较差");
strcpy(G.vexs[9].name,"多媒体楼");
strcpy(G.vexs[9].introduction,"多媒体教学场所,设施先进,环境良好");
for(i=0;ifor(j=0;jG.arcs[i][j].adj=INFINITY;
G.arcs[0][1].adj=100;
G.arcs[0][2].adj=200;
G.arcs[0][6].adj=400;
G.arcs[1][7].adj=300;
G.arcs[2][3].adj=120;
G.arcs[3][6].adj=220;
G.arcs[3][4].adj=100;
G.arcs[4][5].adj=300;
G.arcs[4][9].adj=250;
G.arcs[5][9].adj=350;
G.arcs[6][7].adj=60;
G.arcs[6][9].adj=200;
G.arcs[7][8].adj=50;
G.arcs[8][9].adj=20;
for(i=0;ifor(j=0;jG.arcs[j][i].adj=G.arcs[i][j].adj;
returnG;
}//InitGraphend
voidMenu()
{
printf("\n石家庄铁道学院导游图\n");
printf("┏━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃1.浏览校园全景┃\n");
printf("┃2.查看所有游览路线┃\n");
printf("┃3.选择出发点和目的地┃\n");
printf("┃4.查看景点信息┃\n");
printf("┃5.退出系统┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━┛\n");
printf("Option-:
");
}
voidBrowser(MGraph*G)
{
intv;
printf("┏━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃编号┃景点名称┃简介
┃\n");
for(v=0;vvexnum;v++)
printf("┃%-4d┃%-16s┃%-56s┃
\n",G->vexs[v].num,G->vexs[v].name,G->vexs[v].introduction);
printf("┗━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
}
//迪杰斯特拉算法来计算出起点到各个顶点之间的最短路径,v0为起点
voidShortestPath_DIJ(MGraph*G)
{
intv,w,i,min,t=0,x,flag=1,v0;
intfinal[20],D[20],p[20][20];
while(flag)
{
printf("请输入一个起始景点编号:
");
scanf("%d",&v0);
if(v0<0||v0>G->vexnum)
{
printf("景点编号不存在!
请重新输入景点编号:
");
scanf("%d",&v0);
}
if(v0>=0&&v0vexnum)
flag=0;
}
for(v=0;vvexnum;v++)
{
final[v]=0;
D[v]=G->arcs[v0][v].adj;
for(w=0;wvexnum;w++)
p[v][w]=0;
if(D[v]{
p[v][v0]=1;p[v][v]=1;
}
}
D[v0]=0;final[v0]=1;
for(i=1;ivexnum;i++)
{
min=INFINITY;
for(w=0;wvexnum;w++)
if(!
final[w])
if(D[w]final[v]=1;
for(w=0;wvexnum;w++)
if(!
final[w]&&(min+G->arcs[v][w].adj{
D[w]=min+G->arcs[v][w].adj;
for(x=0;xvexnum;x++)
p[w][x]=p[v][x];
p[w][w]=1;
}
}
for(v=0;vvexnum;v++)
{
if(v0!
=v)printf("%s",G->vexs[v0].name);
for(w=0;wvexnum;w++)
{
if(p[v][w]&&w!
=v0)printf("-->%s",G->vexs[w].name);
t++;
}
if(t>G->vexnum-1&&v0!
=v)printf("总路线长%dm\n\n",D[v]);}
}//ShortestPath_DIJend
voidFloyd(MGraph*G)
{
intv,u,i,w,k,j,flag=1,p[10][10][10],D[10][10];
for(v=0;vvexnum;v++)
for(w=0;wvexnum;w++)
{
D[v][w]=G->arcs[v][w].adj;
for(u=0;uvexnum;u++)
p[v][w][u]=0;
if(D[v][w]{
p[v][w][v]=1;p[v][w][w]=1;
}
}
for(u=0;uvexnum;u++)
for(v=0;vvexnum;v++)
for(w=0;wvexnum;w++)
if(D[v][u]+D[u][w]{
D[v][w]=D[v][u]+D[u][w];
for(i=0;ivexnum;i++)
p[v][w][i]=p[v][u][i]||p[u][w][i];
}
while(flag)
{
printf("请输入出发点和目的地的编号:
");
scanf("%d%d",&k,&j);
if(k<0||k>G->vexnum||j<0||j>G->vexnum)
{
printf("景点编号不存在!
请重新输入出发点和目的地的编号:
");
scanf("%d%d",&k,&j);
}
if(k>=0&&kvexnum&&j>=0&&jvexnum)
flag=0;
}
printf("%s",G->vexs[k].name);
for(u=0;uvexnum;u++)
if(p[k][j][u]&&k!
=u&&j!
=u)
printf("-->%s",G->vexs[u].name);
printf("-->%s",G->vexs[j].name);
printf("总路线长%dm\n",D[k][j]);
}//Floydend
voidSearch(MGraph*G)
{
intk,flag=1;
while(flag)
{
printf("请输入要查询的景点编号:
");
scanf("%d",&k);
if(k<0||k>G->vexnum)
{
printf("景点编号不存在!
请重新输入景点编号:
");
scanf("%d",&k);
}
if(k>=0&&kvexnum)
flag=0;
}
printf("┏━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃编号┃景点名称┃简介
┃\n");
printf("┃%-4d┃%-16s┃%-56s┃
\n",G->vexs[k].num,G->vexs[k].name,G->vexs[k].introduction);
printf("┗━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
}//Searchend
intLocateVex(MGraph*G,char*v)
{
intc=-1,i;
for(i=0;ivexnum;i++)
if(strcmp(v,G->vexs[i].name)==0)
{c=i;break;}
returnc;
}
MGraph*CreatUDN(MGraph*G)//初始化图形,接受用户输入
{
inti,j,k,w;
charv1[20],v2[20];
printf("请输入图的顶点数,弧数:
");
scanf("%d%d",&G->vexnum,&G->arcnum);
printf("请输入景点的编号:
、名称、简介:
\n");
for(i=0;ivexnum;i++)
{
printf("景点编号:
");
scanf("%d",&G->vexs->num);
printf("景点名称:
");
scanf("%s",G->vexs[i].name);
printf("景点简介:
");
scanf("%s",G->vexs->introduction);
}
for(i=0;ivexnum;i++)
for(j=0;jvexnum;j++)
G->arcs[i][j].adj=INFINITY;
printf("请输入路径长度:
\n");
for(k=0;karcnum;k++)
{
printf("第%d条边:
\n",k+1);
printf("景点对(x,y):
");
scanf("%s",v1);
scanf("%s",v2);
printf("路径长度:
");
scanf("%d",&w);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
if(i>=0&&j>=0)
{
G->arcs[i][j].adj=w;
G->arcs[j][i]=G->arcs[i][j];
}
}
returnG;
}
voidprint(MGraph*G)
{
intv,w,t=0;
for(v=0;vvexnum;v++)
for(w=0;wvexnum;w++)
{if(G->arcs[v][w].adj==INFINITY)printf("∞");
elseprintf("%-7d",G->arcs[v][w].adj);
t++;
if(t%G->vexnum==0)
printf("\n");
}
}