#include<bits/stdc++.h>
using namespace std;
#define MAX 10 // 从校园中取10个点
#define INTMAX 66666
//Floyd算法数据
int L[MAX+1][MAX+1]; // 存最短路径长度
int D[MAX+1][MAX+1]; // 存最短路径点
// 地点结构体
typedef struct
{
int siteNum; // 地点编号
string siteName; // 地点名称
string sitePresentation; // 地点介绍
}Site;
// 校园平面图
class SGraph
{
private:
Site site[MAX+1]; // 校园
int graph[MAX+1][MAX+1]; // 图的邻接矩阵
int siteSum; // 地点数
int pathSum;
public:
// 默认为科大校园信息
SGraph(){
siteSum=10;
pathSum=45;
site[1].siteName="东门";
site[2].siteName="春晖学堂";
site[3].siteName="图书馆";
site[4].siteName="文馨书院";
site[5].siteName="秋实楼";
site[6].siteName="逸夫楼";
site[7].siteName="体育馆";
site[8].siteName="颐园";
site[9].siteName="3号楼";
site[10].siteName="2号楼";
site[1].siteNum=1;
site[2].siteNum=2;
site[3].siteNum=3;
site[4].siteNum=4;
site[5].siteNum=5;
site[6].siteNum=6;
site[7].siteNum=7;
site[8].siteNum=8;
site[9].siteNum=9;
site[10].siteNum=10;
site[1].sitePresentation="北华正门";
site[2].sitePresentation="北华主教";
site[3].sitePresentation="北华最喜欢的地方";
site[4].sitePresentation="北华二教";
site[5].sitePresentation="北华实验楼,含高配机房";
site[6].sitePresentation="北华最气派的教学楼";
site[7].sitePresentation="北华体测地点";
site[8].sitePresentation="北华的食堂之一";
site[9].sitePresentation="北华女寝之一,常见许多男子楼下等候女友";
site[10].sitePresentation="北华混寝之一";
for(int i=0;i<MAX;i++)
{
graph[i][0]=INTMAX;
graph[0][i]=INTMAX;
}
graph[1][2]=79;
graph[2][1]=79;
graph[1][3]=567;
graph[3][1]=567;
graph[1][4]=667;
graph[4][1]=667;
graph[1][5]=901;
graph[5][1]=901;
graph[1][6]=1100;
graph[6][1]=1100;
graph[1][7]=1000;
graph[7][1]=1000;
graph[1][8]=928;
graph[8][1]=928;
graph[1][9]=872;
graph[9][1]=872;
graph[1][10]=364;
graph[10][1]=364;
graph[1][1]=INTMAX;
graph[2][3]=505;
graph[3][2]=505;
graph[2][4]=591;
graph[4][2]=591;
graph[2][5]=791;
graph[5][2]=791;
graph[2][6]=1000;
graph[6][2]=1000;
graph[2][7]=950;
graph[7][2]=950;
graph[2][8]=850;
graph[8][1]=850;
graph[2][9]=410;
graph[9][2]=410;
graph[2][10]=386;
graph[10][2]=386;
graph[2][2]=INTMAX;
graph[3][4]=384;
graph[4][3]=384;
graph[3][5]=435;
graph[5][3]=435;
graph[3][6]=829;
graph[6][3]=829;
graph[3][7]=905;
graph[7][3]=905;
graph[3][8]=756;
graph[8][3]=756;
graph[3][9]=450;
graph[9][3]=450;
graph[3][10]=667;
graph[10][3]=667;
graph[3][3]=INTMAX;
graph[4][5]=450;
graph[5][4]=450;
graph[4][6]=552;
graph[6][4]=552;
graph[4][7]=829;
graph[7][4]=829;
graph[4][8]=905;
graph[8][4]=905;
graph[4][9]=756;
graph[9][4]=756;
graph[4][10]=450;
graph[10][4]=450;
graph[4][4]=INTMAX;
graph[5][6]=444;
graph[6][5]=444;
graph[5][7]=561;
graph[7][5]=561;
graph[5][8]=511;
graph[8][5]=511;
graph[5][9]=752;
graph[9][5]=752;
graph[5][10]=1000;
graph[10][5]=1000;
graph[5][5]=INTMAX;
graph[6][7]=137;
graph[7][6]=137;
graph[6][8]=586;
graph[8][6]=586;
graph[6][9]=926;
graph[9][6]=926;
graph[6][10]=1200;
graph[10][6]=1200;
graph[6][6]=INTMAX;
graph[7][8]=521;
graph[8][7]=521;
graph[7][9]=861;
graph[9][7]=861;
graph[7][10]=1100;
graph[10][7]=1100;
graph[7][7]=INTMAX;
graph[8][9]=427;
graph[9][8]=427;
graph[8][10]=884;
graph[10][8]=884;
graph[8][8]=INTMAX;
graph[9][10]=458;
graph[10][9]=458;
graph[9][9]=INTMAX;
graph[10][10]=INTMAX;
}
~SGraph(){cout<<"\t\t\t\t\t平面图已释放"<<endl;}
// 获取地点编号
int getsiteNum(string siteName)
{
for(int i=1;i<=MAX;i++)
{
if(siteName==site[i].siteName)
{
return i;
}
}
cout<<"\t\t\t\t\t未找到该地点!"<<endl;
exit(0);
}
// 构建平面图
void createSGraph()
{
// 存入地点信息
for(int i=1;i<=MAX;i++)
{
site[i].siteNum=i;
cout<<"\t\t\t\t\t请输入地点名称:";
cin>>site[i].siteName;
cout<<"\t\t\t\t\t请输入地点介绍:";
cin>>site[i].sitePresentation;
}
// 存入平面图信息
siteSum=MAX;
cout<<"\t\t\t\t\t请输入边数:";
cin>>pathSum;
// 初始化边为无穷大
for(int i=1;i<=MAX;i++)
{
for(int j=1;j<=MAX;j++)
{
graph[i][j]=INTMAX;
}
}
// 存邻接矩阵
for(int k=1;k<=pathSum;k++)
{
int siteNum1,siteNum2; // 起始地点编号、终止地点编号
string siteName1,siteName2; // 起始地点名称、终止地点名称
int length;
cout<<"\t\t\t\t\t请输入起点:";
cin>>siteName1;
siteNum1=getsiteNum(siteName1);
cout<<"\t\t\t\t\t请输入终点:";
cin>>siteName2;
siteNum2=getsiteNum(siteName2);
cout<<"\t\t\t\t\t请输入长度:";
cin>>length;
graph[siteNum1][siteNum2]=length;
graph[siteNum2][siteNum1]=length;
}
cout<<"\t\t\t\t\t[添加信息成功!]"<<endl;
cout<<"\t\t\t\t\t";
system("pause");
}
// 在D://学校编号.txt文件中存入数据
void saveData()
{
cout<<"\t\t\t\t\t请输入学校编号:";
int ischoolNum;
cin>>ischoolNum;
// 合成文件路径
char cschoolNum[20];
sprintf(cschoolNum,"%d",ischoolNum); // 将数据按照格式写入字符串
char filePath[300]="D:\\计算机编程\\C_Dev-C++\\大二数据结构作业\\课程设计\\";
strcat(filePath,cschoolNum);
strcat(filePath,".txt");
// 存入文件
ofstream out(filePath);
if(!out)
{
cout<<"\t\t\t\t\t文件保存失败!"<<endl;
exit(0);
}
// 存邻接矩阵
for(int i=1;i<=MAX;i++)
{
for(int j=1;j<=MAX;j++)
{
out<<graph[i][j]<<' ';
}
}
out<<endl;
// 存边数和点数
out<<siteSum<<' '<<pathSum<<endl;
// 存地点信息
for(int i=1;i<=MAX;i++)
{
out<<site[i].siteNum<<endl;
out<<site[i].siteName<<endl;
out<<site[i].sitePresentation<<endl;
}
out.close();
cout<<"\t\t\t\t\t[已成功保存数据!]"<<endl;
cout<<"\t\t\t\t\t";
system("pause");
}
// 从D://学校编号.txt文件中读取数据
void readData()
{
cout<<"\t\t\t\t\t请输入学校编号:";
int ischoolNum;
cin>>ischoolNum;
// 合成文件路径
char cschoolNum[20];
sprintf(cschoolNum,"%d",ischoolNum); // 将数据按照格式写入字符串
char filePath[300]="D:\\计算机编程\\C_Dev-C++\\大二数据结构作业\\课程设计\\";
strcat(filePath,cschoolNum);
strcat(filePath,".txt");
ifstream in(filePath);
if(!in)
{
cout<<"\t\t\t\t\t!文件读取失败!"<<endl;
exit(0);
}
// 读邻接矩阵
for(int i=1;i<=MAX;i++)
{
for(int j=1;j<=MAX;j++)
{
in>>graph[i][j];
}
}
// 读边数和点数
in>>siteSum>>pathSum;
// 读地点信息
for(int i=1;i<=MAX;i++)
{
in>>site[i].siteNum;
in>>site[i].siteName;
in>>site[i].sitePresentation;
}
in.close();
cout<<"\t\t\t\t\t[已成功读取数据!]"<<endl;
cout<<"\t\t\t\t\t";
system("pause");
}
// 查询地点信息
void researchSite()
{
string siteName;
int siteNum;
cout<<"\t\t\t\t\t请输入你要查询的地点名称:";
cin>>siteName;
siteNum=getsiteNum(siteName);
cout<<"\t\t\t\t\t地点编号:"<<site[siteNum].siteNum<<endl;
cout<<"\t\t\t\t\t地点名称:"<<site[siteNum].siteName<<endl;
cout<<"\t\t\t\t\t地点介绍:"<<site[siteNum].sitePresentation<<endl;
cout<<"\t\t\t\t\t";
system("pause");
}
// Floyd算法求最短路径
void floyd()
{
// 初始化存最短路径长与最短路径的两个数组
for(int i=1;i<=MAX;i++)
{
for(int j=1;j<=MAX;j++)
{
L[i][j]=graph[i][j];
// 如果两个点有路径,就保存
if(i!=j&&graph[i][j]<INTMAX)
{
D[i][j]=i;
}
else
{
D[i][j]=-1;
}
}
}
// 找最短路径,并记录最短路径长
for(int k=1;k<=MAX;k++)
{
for(int i=1;i<=MAX;i++)
{
for(int j=1;j<=MAX;j++)
{
if((L[i][k]+L[k][j])<L[i][j])
{
L[i][j]=L[i][k]+L[k][j];
D[i][j]=D[k][j];
}
}
}
}
}
// 获取最短路径
void getMinlength()
{
while(1)
{
string siteName1,siteName2;
int siteNum1,siteNum2;
cout<<"\t\t\t\t\t请输入要查找的起点名称:";
cin>>siteName1;
siteNum1=getsiteNum(siteName1);
cout<<"\t\t\t\t\t请输入要查找的终点名称:";
cin>>siteName2;
siteNum2=getsiteNum(siteName2);
if(siteNum1==siteNum2||siteNum1>MAX||siteNum1<1||siteNum2>MAX||siteNum2<1)
{
cout<<"\t\t\t\t\t[输入路径有误,请重新输入]"<<endl;
continue;
}
else
{
cout<<"\t\t\t\t\t起点:"<<site[siteNum1].siteName<<endl;
cout<<"\t\t\t\t\t终点:"<<site[siteNum2].siteName<<endl;
cout<<"\t\t\t\t\t最短路径:";
int dNum=D[siteNum1][siteNum2];
if(dNum==-1)
{
cout<<"无"<<endl;
}
else
{
int minSite[MAX+1]; // 记录中间点
int ans=0; // 计数器
minSite[ans]=siteNum2;
ans++;
while(dNum!=siteNum1)
{
minSite[ans]=dNum;
ans++;
dNum=D[siteNum1][dNum];
}
minSite[ans]=siteNum1;
for(int i=ans;i>0;i--)
{
cout<<site[minSite[i]].siteName<<"--->";
}
cout<<site[minSite[0]].siteName<<", 路程为"<<L[siteNum1][siteNum2]<<"m"<<endl;
cout<<"\n\t\t\t\t\t";
system("pause");
break;
}
}
}
}
};
void display_MainActivity(); // 显示主界面
int main()
{
SGraph sg;
while(1)
{
display_MainActivity();
int choice;
cin>>choice;
if(choice==1)
{
sg.createSGraph();
}
else if(choice==2)
{
sg.floyd();
sg.getMinlength();
}
else if(choice==3)
{
sg.readData();
}
else if(choice==4)
{
sg.saveData();
}
else if(choice==5)
{
sg.researchSite();
}
else if(choice==6)
{
cout<<"\t\t\t\t\t[感谢您的使用]"<<endl;
exit(0);
}
else
{
cout<<"\t\t\t\t\t[指令有误,请重新输入]"<<endl;
cout<<"\t\t\t\t\t";
system("pause");
}
}
}
void display_MainActivity()
{
system("cls"); // 清屏
int i; // 下标
cout<<"\t\t\t\t\t# # # # # # # # # # # # #\n";
cout<<"\t\t\t\t\t# #\n";
cout<<"\t\t\t\t\t# 校园导航系统 #\n";
for(i=4; i<=5; i++)
{
cout<<"\t\t\t\t\t# #\n";
}
cout<<"\t\t\t\t\t# 1.校园平面添加 #\n";
cout<<"\t\t\t\t\t# #\n";
cout<<"\t\t\t\t\t# 2.最短路径查询 #\n";
cout<<"\t\t\t\t\t# #\n";
cout<<"\t\t\t\t\t# 3.读取数据 #\n";
cout<<"\t\t\t\t\t# #\n";
cout<<"\t\t\t\t\t# 4.保存数据 #\n";
cout<<"\t\t\t\t\t# #\n";
cout<<"\t\t\t\t\t# 5.查询地点信息 #\n";
cout<<"\t\t\t\t\t# #\n";
cout<<"\t\t\t\t\t# 6.退出系统 #\n";
cout<<"\t\t\t\t\t# #\n";
cout<<"\t\t\t\t\t# # # # # # # # # # # # #\n";
cout<<"\t\t\t\t\t 请输入您的指令:";
}
02-16
11-02
1331
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-21
1060
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)