我只有C++的~~
#include
#include
using namespace std;
#define MaxVertexNum 50 /*景点个数最大50*/
#define MAXCOST 1000 /*定义路径的无穷大*/
#define T 8 /*目前景点个数*/
typedef struct
{
char name[20]; /*景点名称*/
char number[15]; /*景点代号*/
char introduce[100]; /*景点简介*/
}Elemtype;
typedef struct
{
int num; /*顶点编号*/
Elemtype date; /*顶点信息*/
}Vertex; /*定义顶点*/
typedef struct
{
Vertex vexs[MaxVertexNum]; /*存放顶点的一维数组,数组第零个单元没有用上*/
unsigned int edges[MaxVertexNum][MaxVertexNum]; /*存放路径的长度*/
int n,e;
}MGraph;
MGraph MGr; /*全局变量,定义MGr为MGraph类型*/
int shortest[MaxVertexNum][MaxVertexNum]; /*定义全局变量存贮最小路径*/
int path[MaxVertexNum][MaxVertexNum]; /*定义存贮路径*/
void init()
{
int i,j;
MGr.vexs[1].num=1;
strcpy(MGr.vexs[1].date.name,"学校东门");
strcpy(MGr.vexs[1].date.number,"001");
strcpy(MGr.vexs[1].date.introduce,"挨着三好街,购物很方便。");
MGr.vexs[2].num=2;
strcpy(MGr.vexs[2].date.name,"综合楼");
strcpy(MGr.vexs[2].date.number,"002");
strcpy(MGr.vexs[2].date.introduce,"学校最新的大楼。");
MGr.vexs[3].num=3;
strcpy(MGr.vexs[3].date.name,"逸夫楼");
strcpy(MGr.vexs[3].date.number,"003");
strcpy(MGr.vexs[3].date.introduce,"上课的地方。");
MGr.vexs[4].num=4;
strcpy(MGr.vexs[4].date.name,"教学馆");
strcpy(MGr.vexs[4].date.number,"004");
strcpy(MGr.vexs[4].date.introduce,"上课的地方。");
MGr.vexs[5].num=5;
strcpy(MGr.vexs[5].date.name,"篮球场");
strcpy(MGr.vexs[5].date.number,"005");
strcpy(MGr.vexs[5].date.introduce,"打篮球的地方。");
MGr.vexs[6].num=6;
strcpy(MGr.vexs[6].date.name,"大活");
strcpy(MGr.vexs[6].date.number,"006");
strcpy(MGr.vexs[6].date.introduce,"开晚会搞活动的地方。");
MGr.vexs[7].num=7;
strcpy(MGr.vexs[7].date.name,"汉卿会堂");
strcpy(MGr.vexs[7].date.number,"007");
strcpy(MGr.vexs[7].date.introduce,"开讲座的地方。");
MGr.vexs[8].num=8;
strcpy(MGr.vexs[8].date.name,"主楼");
strcpy(MGr.vexs[8].date.number,"008");
strcpy(MGr.vexs[8].date.introduce,"做实验的地方。");
for(i=1;i<=T;i++)
{
for(j=1;j<=T;j++)
{
MGr.edges[i][j]=MAXCOST;
}
}
for(i=1;i<=T;i++)
{
shortest[i][i]=0;
} /*初始化*/
MGr.edges[1][2]=MGr.edges[2][1]=25;
MGr.edges[1][5]=MGr.edges[5][1]=15;
MGr.edges[1][3]=MGr.edges[3][1]=10;
MGr.edges[2][8]=MGr.edges[8][2]=30;
MGr.edges[5][7]=MGr.edges[7][5]=32;
MGr.edges[7][8]=MGr.edges[8][7]=12;
MGr.edges[6][7]=MGr.edges[7][6]=6;
MGr.edges[3][4]=MGr.edges[4][3]=24;
MGr.edges[4][6]=MGr.edges[6][4]=50;
MGr.edges[1][1]=MGr.edges[2][2]=MGr.edges[3][3]=MGr.edges[4][4]=0;
MGr.edges[5][5]=MGr.edges[6][6]=MGr.edges[7][7]=MGr.edges[8][8]=0;
}
void introduce()
{
int n;
cout<<"请输入查询景点编号:"<
switch(n)
{
case 1:
cout<<"景点编号:"<
case 2:
cout<<"景点编号:"<
case 3:
cout<<"景点编号:"<
case 4:
cout<<"景点编号:"<
case 5:
cout<<"景点编号:"<
case 6:
cout<<"景点编号:"<
case 7:
cout<<"景点编号:"<
case 8:
cout<<"景点编号:"<
default:
cout<<"输入序号错误。";
break;
}
}
void floyd()
{
int i,j,k;
for(i=1;i<=T;i++)
{
for(j=1;j<=T;j++)
{
shortest[i][j]=MGr.edges[i][j];
path[i][j]=0;
}
} /*初始化数组*/
for(k=1;k<=T;k++)
{
for(i=1;i<=T;i++)
{
for(j=1;j<=T;j++)
{
if(shortest[i][j]>(shortest[i][k]+shortest[k][j]))
{
shortest[i][j]=shortest[i][k]+shortest[k][j];
path[i][j]=k;
path[j][i]=k;/*记录经过的路径*/
}//end_if
}
}
}//end_for
}
void display(int i,int j)
{/* 打印两个景点的路径及最短距离 */
int a,b;
a=i;
b=j;
cout<<"您要查询的两景点间最短路径是:\n\n";
if(shortest[i][j]!=MaxVertexNum)
{
if(i
cout< while(path[i][j]!=0)
{/* 把i到j的路径上所有经过的景点按逆序打印出来*/
cout<<"<-"<
else
i=path[j][i];
}
cout<<"<-"< cout<<"\n\n";
cout<"< }
else
{
cout< while(path[i][j]!=0)
{/* 把i到j的路径上所有经过的景点按顺序打印出来*/
cout<<"->"<
else
i=path[j][i];
}
cout<<"->"< cout<<"\n\n";
cout<"< }
}
else
cout<<"输入错误!不存在此路!\n\n";
}/*display*/
int shortestdistance()
{/*要查找的两景点的最短距离*/
int i,j;
cout<<"请输入要查询的两个景点的编号(1->8的数字编号并用' '间隔):";
cin>>i>>j;
if(i>T||i<=0||j>T||j<0)
{
cout<<"输入信息错误!\n\n";
cout<<" 请输入要查询的两个景点的编号(1->8的数字编号并用' '间隔):\n";
cin>>i>>j;
}
else
{
floyd();
display(i,j);
}
return 1;
}/*shortestdistance*/
void main()
{
char k;
init();
cout<<"*******************************************************************\n";
cout<<"* *\n";
cout<<"* *\n";
cout<<"* 欢迎使用校园导游咨询 *\n";
cout<<"* *\n";
cout<<"******************************************************************\n";
while(1)
{
cout<<"1.景点信息查询请按 i 键\n";
cout<<"2.景点最短路径查询请按 s 键\n";
cout<<"3.退出系统请按 e 键\n";
cout<<"请选择服务:";
cin>>k;
switch(k)
{
case 'i':
cout<<"景点简介查询(请输入1~8)。";
introduce();
break;
case 's':
cout<<"景点最短路径查询。";
shortestdistance();
break;
case 'e':
exit(0);
}
}
system("pause");
}