本人是一名暨南大学大二的学生,这是我的数据结构课程的一次编程作业,主要算法是弗洛伊德算法,希望各位大神多多指教.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 100
#define maxroad 10000
typedef struct{
char introduction[maxsize];//景点介绍
char name[maxsize];//景点名字
char number[maxsize];//景点名称代号
}viewtype;
typedef struct{
int num;//景点编号
viewtype data;//景点信息
}View;
typedef struct{
View view[maxsize];
int length[maxsize][maxsize];//存放路径长度
//第0单元不用
int m,n;//存放起点和终点
}MGraph;
int n = 16;//全局变量,判断有多少个景点
//展示景点
int shortest[maxsize][maxsize]; //定义全局变量存贮最小路径
int path[maxsize][maxsize]; //定义存贮路径
void display_map(){
printf(" -----------------------------------------------------------------------------------\n");
printf("| ------------- ------- |\n");
printf("| | 5 | |16.东门\n");
printf("| | T5 | ------- |\n");
printf("| ------------- |\n");
printf("| ------------- ------------- |\n");
printf("| | 4 | | 12 | |\n");
printf("| | T4 | | T12 | |\n");
printf("| ------------- ------------- |\n");
printf("| ------------- ------------- |\n");
printf("| | 3 | | 11 | |\n");
printf("| | T3 | | T11 | |\n");
printf("| ------------- ------------- |\n");
printf("| ------------- ------------- |\n");
printf("| | 2 T2 | | 10 | |\n");
printf("| | | | T10 | |\n");
printf("| ------------- ------------- |\n");
printf("| ------------ |\n");
printf("| | 1 | |\n");
printf("| | T1 | |\n");
printf("| ------------ |\n");
printf("| ------------ |\n");
printf("| | 15 | |\n");
printf("| | 运动馆 | |\n");
printf("| ------------ |\n");
printf("| ---------------------------------------- |\n");
printf("| | 13. | | 14. | |\n");
printf("| | 图 | | 恒 | |\n");
printf("| | 书 | | 大 | |\n");
printf("| | 馆 | | 楼 | |\n");
printf("| | | | | |\n");
printf("| | | | | \n");
printf("| -------------- -------------- \n");
printf("| \n");
printf("| -------------------------------------------------------------------------------- \n");
printf("| | |||| 9. 镜湖 |||| | \n");
printf("| -------------------------------------------------------------------------------- \n");
printf("| \n");
printf("| \n");
printf("| -------------------------------------------------------------------------------\n");
printf("| | 7. | | 8. | \n");
printf("| | 化 | | 药 | \n");
printf("| | | | | \n");
printf("| | 学 | | 学 | \n");
printf("| | | | | \n");
printf("| | 院 | | 院 | \n");
printf("| | | | | \n");
printf("| | | | | \n");
printf("| | | | | \n");
printf("| --------------------- --------------------- \n");
printf("| --------------------------------|6.南门|------------------------------------------ \n");
printf("| \n");
printf("| \n");
printf("| ---------------------------------------------------------------------------------- \n");
fflush(stdin);
}
//赋初值
void init(MGraph *G){
for(int i = 0; i < n; i++){
G->view[i].num = i + 1;//编号赋值
}
strcpy(G->view[0].data.name,"T1宿舍楼");
strcpy(G->view[0].data.number,"001");
strcpy(G->view[0].data.introduction,"又称麦彦桐楼,由暨南大学校友麦照平先生捐赠");
strcpy(G->view[1].data.name,"T2宿舍楼");
strcpy(G->view[1].data.number,"002");
strcpy(G->view[1].data.introduction,"暨大宿舍楼之一");
strcpy(G->view[2].data.name,"T3宿舍楼");
strcpy(G->view[2].data.number,"003");
strcpy(G->view[2].data.introduction,"暨大宿舍楼之一");
strcpy(G->view[3].data.name,"暨大宿舍楼之一");
strcpy(G->view[3].data.number,"004");
strcpy(G->view[3].data.introduction,"暨大宿舍楼之一");
strcpy(G->view[4].data.name,"T5宿舍楼");
strcpy(G->view[4].data.number,"005");
strcpy(G->view[4].data.introduction,"暨大宿舍楼之一");
strcpy(G->view[5].data.name,"南门");
strcpy(G->view[5].data.number,"006");
strcpy(G->view[5].data.introduction,"坐落于兴业大道东855号,交通便利");
strcpy(G->view[6].data.name,"化学院");
strcpy(G->view[6].data.number,"007");
strcpy(G->view[6].data.introduction,"是化学专业的同学们的大本营");
strcpy(G->view[7].data.name,"药学院");
strcpy(G->view[7].data.number,"008");
strcpy(G->view[7].data.introduction,"是药学院同学们学习的大本营");
strcpy(G->view[8].data.name,"镜湖");
strcpy(G->view[8].data.number,"009");
strcpy(G->view[8].data.introduction,"由南向北贯穿整个校园,是师生们散步娱乐的好地方");
strcpy(G->view[9].data.name,"T10宿舍楼");
strcpy(G->view[9].data.number,"010");
strcpy(G->view[9].data.introduction,"暨大宿舍楼之一");
strcpy(G->view[10].data.name,"T11宿舍楼");
strcpy(G->view[10].data.number,"011");
strcpy(G->view[10].data.introduction,"暨大宿舍楼之一");
strcpy(G->view[11].data.name,"T12宿舍楼");
strcpy(G->view[11].data.number,"012");
strcpy(G->view[11].data.introduction,"暨大宿舍楼之一");
strcpy(G->view[12].data.name,"图书馆");
strcpy(G->view[12].data.number,"013");
strcpy(G->view[12].data.introduction,"有来自世界各地的藏书,可以同时容纳一万多人进行阅读");
strcpy(G->view[13].data.name,"恒大楼");
strcpy(G->view[13].data.number,"014");
strcpy(G->view[13].data.introduction,"是暨大学子的学习的主阵地");
strcpy(G->view[14].data.name,"裕华堂");
strcpy(G->view[14].data.number,"015");
strcpy(G->view[14].data.introduction,"是暨南大学的饭堂,由来自全国各地的美食");
strcpy(G->view[15].data.name,"东门");
strcpy(G->view[15].data.number,"016");
strcpy(G->view[15].data.introduction,"落于兴业大道东855号,交通便利");
//下面补充路径问题
int i ,j;
for(i = 1;i <= n ;i++)
{
for(j = 1;j <= n; j++){
G->length[i][j] = maxroad;
}
}
//最短路径设定
for(i=1;i<=n;i++)
{
shortest[i][j]=0;
}
for(int k = 0;k <= n; k++){
G->length[k][k] = 0;
}
G->length[1][2]=G->length[2][1]=50;
G->length[2][3]=G->length[3][2]=50;
G->length[3][4]=G->length[4][3]=50;
G->length[4][5]=G->length[5][4]=50;
G->length[6][7]=G->length[7][6]=70;
G->length[6][9]=G->length[9][6]=220;
G->length[6][8]=G->length[8][6]=50;
G->length[7][8]=G->length[8][7]=50;
G->length[8][9]=G->length[9][8]=100;
G->length[7][9]=G->length[7][9]=150;
G->length[9][11]=G->length[11][9]=50;
G->length[9][13]=G->length[13][9]=30;
G->length[9][14]=G->length[14][9]=30;
G->length[13][14]=G->length[14][13]=20;
G->length[15][14]=G->length[15][14]=10;
G->length[13][15]=G->length[15][13]=60;
G->length[15][10]=G->length[10][15]=2;
G->length[1][15]=G->length[15][1]=50;
G->length[11][10]=G->length[10][11]=50;
G->length[11][12]=G->length[12][11]=50;
G->length[10][16]=G->length[16][10]=60;
G->length[15][16]=G->length[16][15]=100;
//初始化完全
}
//展示景点信息
//弗洛伊德算法
void floyd(MGraph *G){
int i,j,k;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
shortest[i][j]=G->length[i][j];
path[i][j]=0;
}
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;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_shortest(){
int a,b;
printf("请输入您想要查询的最短路径(格式:m->n):");
scanf("%d->%d",&a,&b);
fflush(stdin);
if(a<b){
printf("%d",b);
int i = a;
int j = b;
while(path[i][j]!=0){
printf("<-%d",path[i][j]);
if(i<j)
j=path[i][j];
else
i=path[j][i];
}
printf("<-%d\n\n",a);
printf("%d->%d的最短路径是: %d 米。\n",a,b,shortest[a][b]);
}
else{
int i = a;
int j = b;
printf("%d",a);
while(path[i][j]!=0)
{ /*把i到j的路径上所有经过的景点按顺序打印出来*/
printf("-->%d",path[i][j]);
if(i<j)
j=path[i][j];
else
i=path[j][i];
}
printf("->%d\n\n",b);
printf("%d->%d的最短路径是: %d 米。\n",a,b,shortest[a][b]);
}
}
void display_view(MGraph *G){
int op1;
printf("################################\n");
printf("1.景点编号输入\n");
printf("2.景点名称输入\n");
printf("3.景点代号输入\n");
printf("################################\n");
printf("请选择查找景点信息的方式:");
scanf("%d",&op1);
switch(op1){
case 1:{
int target;
printf("请输入景点编号:");
scanf("%d",&target);
if(target <= 0 || target > n){
printf("输入错误\n");
}
else{
for(int i = 0 ; i < n; i++){
if(target == G->view[i].num){
printf("景点名称:%s\n",G->view[i].data.name);
printf("景点代号:%s\n",G->view[i].data.number);
printf("景点介绍:%s\n",G->view[i].data.introduction);
}
}
}
break;
}
case 2:{
char *target;
printf("请输入景点名称:");
scanf("%s",target);
printf("%s",target);
for(int i = 0 ;i < n; i++){
if(strcmp(G->view[i].data.name,target)==0){
printf("景点代号:%s\n",G->view[i].data.number);
printf("景点编号:%d\n",G->view[i].num);
printf("景点介绍:%s\n",G->view[i].data.introduction);
}
}
break;
}
case 3:{
char *target;
printf("请输入景点编号:");
scanf("%s",target);
for(int i = 0;i < n; i++){
if(strcmp(G->view[i].data.number,target) == 0)
{
printf("景点名称:%s\n",G->view[i].data.name);
printf("景点编号:%d\n",G->view[i].num);
printf("景点介绍:%s\n",G->view[i].data.introduction);
}
if(i==n){
printf("没有找到目标\n");
}
}
break;
}
default:{
printf("选择错误,请重新选择!!!\n");
display_view(G);
break;
}
}
}
int main(void){
printf("罗文广\n2021102533\n\n\n\n");
MGraph G;//地图
init(&G);// 景点信息录入
int op;
while(1){
printf("#################################\n");
printf("1.景点信息查询\n");
printf("2.任意两景点之间的最短路径查询:\n");
printf("3.景点地图查询\n");
printf("4.退出服务\n");
printf("#################################\n");
printf("请选择服务(输入服务前编号如:1,2):");
scanf("%d",&op);
if(op <= 0 || op > 4){
do{
printf("该系统无此查询服务,请重新输入!!!\n");
scanf("%d",&op);
}while(op <= 0 || op > 4);
}
switch(op){
case 1:
{
display_view(&G);
break;
}//景点信息查询
case 2:
{
floyd(&G);
display_shortest();
break;
}
case 3:
{
display_map();//展示地图函数
break;
}
case 4:
{
printf("谢谢使用!!!");
exit(1);
}
}
}
return 0;
}