校园导游系统.docx
《校园导游系统.docx》由会员分享,可在线阅读,更多相关《校园导游系统.docx(20页珍藏版)》请在冰点文库上搜索。
校园导游系统
#include
#include
#include
#defineMaxVertexNum13
#defineInfinity1000
#defineMAXSIZE100
typedefstruct{
intID;
charname[17];
charintroduction[256];
}Vexsinfo;
typedefstruct{
Vexsinfovexs[MaxVertexNum];
intarcs[MAXSIZE][MAXSIZE];
intarcNum;
intvexNum;
}MGraph;
intvisited[12];
intd[23];
MGraphcampus;
voidMainWork();
MGraphInitGraph();
voidIntroductionCompus(MGraphc);
voidBrowsepath(MGraphc);
voidShortestPath(MGraphc);
voidSeeAbout(MGraphc);
intLocateVex(MGraphc,intv);
intChangeGraph(MGraph*c);
intCreatGraph(MGraph*c);
intDelVex(MGraph*c);
intDelArc(MGraph*c);
intEnVex(MGraph*c);
intEnArc(MGraph*c);
intNewGraph(MGraph*c);
voidPrintMatrix(MGraphc);
//第一个函数菜单建立
voidMainWork()
{intyourChoice;
charisExit;
campus=InitGraph();
do{
system("cls");
yourChoice=0;
printf("*******欢迎使用校园导航系统*******\n");
printf("*******亲!
welcometo山东工商学院!
********\n");
printf("*******导游系统菜单********\n");
printf("-----1学校景点----\n");
printf("-----2查看路线----\n");
printf("-----3查询景点间最短路径----\n");
printf("-----4景点信息----\n");
printf("-----5更改图信息----\n");
printf("-----6打开邻接矩阵----\n");
printf("-----7退出----\n");
printf("请输入菜单选择的数字:
");
scanf("%d",&yourChoice);
switch(yourChoice){
case1:
system("cls");IntroductionCompus(campus);system("pause");break;
case2:
system("cls");Browsepath(campus);system("pause");break;
case3:
system("cls");ShortestPath(campus);system("pause");break;
case4:
system("cls");SeeAbout(campus);system("pause");break;
case5:
system("cls");ChangeGraph(&campus);break;
case6:
system("cls");PrintMatrix(campus);system("pause");break;
case7:
printf("您确定要退出系统?
(YES/NO):
");
getchar();isExit=getchar();system("cls");
if(isExit=='YES'||isExit=='yes')exit(0);
elseisExit='n';break;
default:
system("cls");printf("输入选择无法识别,请按照菜单指导输入\n");
system("pause");getchar();break;
}
}while(yourChoice!
=7||isExit=='n');
}
//第二个函数构造图
MGraphInitGraph()
{
inti=0,j=0;
MGraphc;
c.vexNum=MaxVertexNum;
c.arcNum=17;
for(i=0;ic.vexs[i].ID=i;
strcpy(c.vexs[0].name,"山东工商学院东门");
strcpy(c.vexs[0].introduction,"学校进出口之一");
strcpy(c.vexs[1].name,"办公楼");
strcpy(c.vexs[1].introduction,"一共11层");
strcpy(c.vexs[2].name,"南校门");
strcpy(c.vexs[2].introduction,"旁边有许多小吃");
strcpy(c.vexs[3].name,"女生宿舍群");
strcpy(c.vexs[3].introduction,"女生居住、日常生活场所");
strcpy(c.vexs[4].name,"篮球场");
strcpy(c.vexs[4].introduction,"学生课间娱乐、体育锻炼场所");
strcpy(c.vexs[5].name,"人工湖");
strcpy(c.vexs[5].introduction,"学校主要景点");
strcpy(c.vexs[6].name,"体育馆");
strcpy(c.vexs[6].introduction,"校运会、大型活动过举办场所");
strcpy(c.vexs[7].name,"图书馆");
strcpy(c.vexs[7].introduction,"包含自修室、电子阅览室、藏书室");
strcpy(c.vexs[8].name,"男生宿舍群");
strcpy(c.vexs[8].introduction,"男生居住、日常活动场所");
strcpy(c.vexs[9].name,"机械楼");
strcpy(c.vexs[9].introduction,"包含机械、动力、物理、信息实验室");
strcpy(c.vexs[10].name,"食堂");
strcpy(c.vexs[10].introduction,"学生日常主要就餐处,共三楼");
strcpy(c.vexs[11].name,"落雪湖");
strcpy(c.vexs[11].introduction,"二教学楼旁边");
strcpy(c.vexs[12].name,"商业街");
strcpy(c.vexs[12].introduction,"学校商业街");
for(i=0;ifor(j=0;jc.arcs[i][j]=Infinity;//初始化图的邻接矩阵
c.arcs[0][1]=3;c.arcs[0][2]=4;c.arcs[0][3]=5;
c.arcs[1][5]=2;c.arcs[1][4]=5;
c.arcs[2][6]=1;c.arcs[2][3]=3;
c.arcs[3][5]=3;c.arcs[3][6]=2;
c.arcs[4][7]=3;c.arcs[4][8]=2;
c.arcs[5][7]=1;
c.arcs[7][9]=2;
c.arcs[8][10]=1;
c.arcs[9][11]=1;
c.arcs[10][12]=1;c.arcs[10][11]=2;
for(i=0;ifor(j=0;jc.arcs[j][i]=c.arcs[i][j];
returnc;}
//第三个函数学校景点显示
voidIntroductionCompus(MGraphc)
{
inti;
printf("\n\n编号景点名称简介\n\n");
printf("*************************\n");
for(i=0;iprintf("%-10d%-25s%-80s\n",c.vexs[i].ID,c.vexs[i].name,c.vexs[i].introduction);
printf("*************************\n\n");
}
//第四个函数显示路径
voidBrowsepath(MGraphc)
{
intv,w,i,min,t=0,x,flag=1,v0;
intfinal[18],d[18],p[18][18];
printf("\n请输入一个起始景点的编号:
");
scanf("%d",&v0);
printf("\n\n");
while(v0<0||v0>c.vexNum)
{
printf("\n你所输入的景点编号不存在\n");
printf("请重新输入");
scanf("%d",&v0);
}
for(v=0;v{
final[v]=0;
d[v]=c.arcs[v0][v];
for(w=0;wp[v][w]=0;
if(d[v]{
p[v][v0]=1;
p[v][v]=1;
}
}
d[v0]=0;
final[v0]=1;
for(i=1;i{
min=Infinity;
for(w=0;wif(!
final[w])
if(d[w]{
v=w;min=d[w];
}
final[v]=1;
for(w=0;wif(!
final[w]&&(min+c.arcs[v][w]{
d[w]=min+c.arcs[v][w];
for(x=0;xp[w][x]=p[v][x];
p[w][w]=1;
}
}
for(v=0;v{
if(v!
=v0)
printf("%s",c.vexs[v0].name);
for(w=0;w{
if(p[v][w]&&w!
=v0&&w!
=v)
printf("----->%s",c.vexs[w].name);
}
printf("----->%s",c.vexs[v].name);
printf("\t总路线长度为%d百米",d[v]);
printf("\n");
}
}
//第五个函数显示最短路径
voidShortestPath(MGraphc)
{
inti,j,k,v,u,w,d[18][18],p[18][18][18];
for(v=0;vfor(w=0;wd[v][w]=c.arcs[v][w];
for(u=0;up[v][w][u]=0;
if(d[v][w]p[v][w][v]=1;
p[v][w][w]=1;
}
}
}
for(u=0;u{
for(v=0;vfor(w=0;wif(d[v][u]+d[u][w]{
d[v][w]=d[v][u]+d[u][w];
for(i=0;ip[v][w][i]=p[v][u][i]+p[u][w][i];
}
}
printf("\n请输入出发景点和目的地的编号");
scanf("%d%d",&k,&j);
printf("\n\n");
while(k<0||k>c.vexNum||j<0||j>c.vexNum)
{
printf("\n您输入的景点不正确!
\n请重新输入:
");
scanf("%d%d",&k,&j);printf("\n\n");
}
printf("%s",c.vexs[k].name);
if(k==j)
{
printf("----->%s",c.vexs[j].name);
printf("\n\n\n总长为0百米\n\n\n");
}
else{
for(u=0;uif(p[k][j][u]&&k!
=u&&j!
=u)
printf("----->%s",c.vexs[u].name);
printf("----->%s",c.vexs[j].name);
printf("\n\n\n总长为%d百米\n\n\n",d[k][j]);
}
}
//第六个函数景点信息查询
voidSeeAbout(MGraphc)
{intk;
printf("\n请输入要查询的景点编号\n");
scanf("%d",&k);
while(k<0||k>c.vexNum)
{
printf("\n您输入的景点不存在\n请重新输入:
");
scanf("%d",&k);
}
printf("\n编号:
%-4d\n",c.vexs[k].ID);
printf("\n景点名称:
%-10s\n",c.vexs[k].name);
printf("\n介绍:
%-80s\n",c.vexs[k].introduction);
}
//第七个函数查找景点的序号
intLocateVex(MGraphc,intv)
{
inti;
for(i=0;iif(v==c.vexs[i].ID)
returni;
elsereturn-1;
}
//第八个函数更改图的信息
intChangeGraph(MGraph*c)
{
intyourChoice;
do{
system("cls");
yourChoice=0;
printf("\n**********欢迎使用校园导航系统*************\n");
printf("\n请选择要完成的操作!
\n");
printf("\n菜单选择\n");
printf("\n1再次建图2删除节点3删除边\n");
printf("\n4增加节点5增加边6更新信息\n");
printf("\n7打印邻接矩阵8返回上一菜单\n");
printf("\n****************************\n");
printf("请输入您的选择\n");
scanf("%d",&yourChoice);
switch(yourChoice)
{
case1:
CreatGraph(c);break;
case2:
DelVex(c);break;
case3:
DelArc(c);break;
case4:
EnVex(c);break;
case5:
EnArc(c);break;
case6:
NewGraph(c);break;
case7:
PrintMatrix(*c);break;
case8:
return1;
default:
printf("输入选择不明确;请重新输入\n");break;
}
putchar('\n');system("pause");getchar();
}while(yourChoice!
=8);
return1;
}
//第九个函数用邻接矩阵方式创建图
intCreatGraph(MGraph*c)
{
inti,j,m,n,v0,v1,distance;
printf("请输入图的顶点数个边数:
\n");
scanf("%d%d",&c->vexNum,&c->arcNum);
printf("\n下面请输入景点的信息:
\n");
for(i=0;ivexNum;i++)
{
printf("请输入景点的编号:
");
scanf("%d",&c->vexs[i].ID);
printf("\n请输入景点的名称:
");
scanf("%s",&c->vexs[i].name);
printf("\n请输入景点的简介:
");
scanf("%s",c->vexs[i].introduction);
}
for(i=0;iarcNum;i++)
for(j=0;jarcNum;j++)
c->arcs[i][j]=Infinity;
printf("下面请输入图的边的信息:
\n");
for(i=1;i<=c->arcNum;i++)
{
printf("\n第%d条边的起点终点长度为:
",i);
scanf("%d%d%d",&v0,&v1,&distance);
m=LocateVex(*c,v0);
n=LocateVex(*c,v1);
if(m>=0&&n>=0)
{
c->arcs[m][n]=distance;
c->arcs[n][m]=c->arcs[m][n];
}
}
return1;
}
//第十个函数删除景点
intDelVex(MGraph*c)
{
inti=0,j;
intm,v;
if(c->vexNum<=0)
{
printf("图中已无丁点");
return1;
}
printf("\n下面请输入你要删除的景点编号:
");
scanf("%d",&v);
while(v<0||c->vexNum)
{
printf("\n输入错误!
请重新输入");scanf("%d",&v);
}
m=LocateVex(*c,v);
if(m<0)
{
printf("顶点%d不存在!
",v);
return1;
}
/*对定点信息所在顺序表进行删除m点的操作*/
for(i=m;ivexNum;i++)
{
c->vexs[i].ID=c->vexs[i+1].ID;
strcpy(c->vexs[i].name,c->vexs[i+1].name);
strcpy(c->vexs[i].introduction,c->vexs[i+1].introduction);
}
for(i=m;ivexNum-1;i++)
for(j=0;jvexNum;j++)
c->arcs[i][j]=c->arcs[i+1][j];
for(i=m;ivexNum-1;i++)
for(j=0;jvexNum;j++)
c->arcs[j][i]=c->arcs[j][i+1];
c->vexNum--;
return1;
}
//第十一个函数删除边
intDelArc(MGraph*c)
{
intm,n,v0,v1;
if(c->arcNum<=0)
{
printf("图中已无边,无法删除.");
return1;
}
printf("\n下面请输入你要删除的边的起点和终点编号:
");
scanf("%d%d",&v0,&v1);
m=LocateVex(*c,v0);
if(m<0)
{
printf("顶点%d不存在!
",v0);return1;
}
n=LocateVex(*c,v1);
if(n<0)
{
printf("定点%d不存在!
",v1);return1;
}
c->arcs[m][n]=Infinity;
c->arcs[n][m]=Infinity;
c->arcNum--;
printf("边(%d,%d)删除成功!
",v0,v1);
return1;
}
//第十二个函数添加景点
intEnVex(MGraph*c)
{
inti;
printf("请输入你要增加结点的信息:
");
printf("\n编号");scanf("%d",&c->vexs[c->vexNum].ID);
printf("名称:
");scanf("%s",c->vexs[c->vexNum].name);
printf("简介:
");
scanf("%s",c->vexs[c->vexNum].introduction);
c->vexNum=c->vexNum+1;
for(i=0;ivexNum;i++)
{
c->arcs[c->vexNum-1][i]=Infinity;
c->arcs[i][c->vexNum-1]=Infinity;
}
return1;
}
//第十三个函数添加边
intEnArc(MGraph*c)
{
intm,n,distance;
printf("\n请输入边的起点和终点编号,权值");
scanf("%d%d%d",&m,&n,&distance);
while(m<0||m>c->vexNum||n<0||n>c->vexNum)
{
printf("输入错误,请重新输入:
");scanf("%d%d",&m,&n);
}
if(LocateVex(*c,m)<0)
{
printf("此顶点%d不存在");return1;
}
if(LocateVex(*c,n)<0)
{
printf("此顶点%d不存在");return1;
}
c->arcs[m][n]=distance;
c->arcs[n][m]=c->arcs[m][n];
printf("边(%d,%d)添加成功!
",m,n);
return1;
}
//第十四个函数更新图信息
intNewGraph(MGraph*c)
{intchangenum;inti,m,n,t,distance,v0,v1;
printf("\n下面请输入您要修改的景点的个数:
\n");
scanf("%d",&changenum);
while(changenum<0||changenum>c->vexNum)
{
printf("\n输入错误!
请重新输入:
");
scanf("%d",&changenum);
}
for(i=0;iprintf("\n请输入景点的编号:
");scanf("%d",&m);
t=LocateVex(*c,m)