📄 源程序.txt
字号:
#include "stdio.h"
#include "iostream.h"
#include "malloc.h"
#include "conio.h"
#include "stdlib.h"
#define Num 11 //最多顶点个数
#define uplimit 100000 //定义一个无穷大的值
float Edge[Num][Num]; //Edge为带权邻接矩阵
float dist[Num]; //dist为最短路程
int path[Num]; //path为最短路径上该顶点的前一顶点的顶点号
int S[Num]; //S为已求得的在最短路径上的顶点号
int D[Num];
/**
* 生成地图,输入地图的基本信息
*
**/
void BuildMap(){
int i,j;
/* 初始化平面图矩阵 */
for ( i=0;i<11;i++){
for ( j=0;j<11;j++){
Edge[0][0]=0 , Edge[0][1]=25 , Edge[0][2]=25 ;
Edge[0][3]=90, Edge[0][4]=uplimit, Edge[0][5]=uplimit ;
Edge[0][6]=10 , Edge[0][7]=uplimit , Edge[0][8]=uplimit;
Edge[0][9]=uplimit, Edge[0][10]=uplimit;
Edge[1][0]=25 , Edge[1][1]=0 , Edge[1][2]=10 ;
Edge[1][3]=32, Edge[1][4]=uplimit, Edge[1][5]=uplimit ;
Edge[1][6]=10 , Edge[1][7]=uplimit , Edge[1][8]=21;
Edge[1][9]=16, Edge[1][10]=uplimit;
Edge[2][0]=25 , Edge[2][1]=10 , Edge[2][2]=0 ;
Edge[2][3]=uplimit, Edge[2][4]=uplimit, Edge[2][5]=uplimit ;
Edge[2][6]=uplimit, Edge[2][7]=uplimit , Edge[2][8]=uplimit;
Edge[2][9]=uplimit, Edge[2][10]=uplimit;
Edge[3][0]=90 , Edge[3][1]=32 , Edge[3][2]=uplimit ;
Edge[3][3]=0 , Edge[3][4]=uplimit, Edge[3][5]=uplimit ;
Edge[3][6]=uplimit, Edge[3][7]=uplimit , Edge[3][8]=26;
Edge[3][9]=uplimit, Edge[3][10]=uplimit;
Edge[4][0]=uplimit, Edge[4][1]=uplimit , Edge[4][2]=uplimit ;
Edge[4][3]=uplimit, Edge[4][4]=0, Edge[4][5]=9 ;
Edge[4][6]=uplimit, Edge[4][7]=uplimit , Edge[4][8]=uplimit;
Edge[4][9]=uplimit, Edge[4][10]=60;
Edge[5][0]=uplimit , Edge[5][1]=uplimit , Edge[5][2]=uplimit ;
Edge[5][3]=uplimit, Edge[5][4]=9, Edge[5][5]=0 ;
Edge[5][6]=uplimit , Edge[5][7]=15 , Edge[5][8]=50;
Edge[5][9]=14, Edge[5][10]=uplimit;
Edge[6][0]=10 , Edge[6][1]=10 , Edge[6][2]=uplimit;
Edge[6][3]=uplimit, Edge[6][4]=uplimit, Edge[6][5]=uplimit ;
Edge[6][6]=0 , Edge[6][7]=35 , Edge[6][8]=uplimit;
Edge[6][9]=30, Edge[6][10]=uplimit;
Edge[7][0]=uplimit , Edge[7][1]=uplimit , Edge[7][2]=uplimit ;
Edge[7][3]=uplimit, Edge[7][4]=uplimit, Edge[7][5]=15 ;
Edge[7][6]=35 , Edge[7][7]=0 , Edge[7][8]=uplimit;
Edge[7][9]=13, Edge[7][10]=uplimit;
Edge[8][0]=uplimit , Edge[8][1]=21 , Edge[8][2]=uplimit ;
Edge[8][3]=26, Edge[8][4]=uplimit; Edge[8][5]=50 ;
Edge[8][6]=uplimit , Edge[8][7]=uplimit , Edge[8][8]=0;
Edge[8][9]=22, Edge[8][10]=10;
Edge[9][0]=uplimit , Edge[9][1]=16 , Edge[9][2]=uplimit ;
Edge[9][3]=uplimit, Edge[9][4]=uplimit, Edge[9][5]=14 ;
Edge[9][6]=30 , Edge[9][7]=13 , Edge[9][8]=22;
Edge[9][9]=0, Edge[9][10]=uplimit;
Edge[10][0]=uplimit , Edge[10][1]=uplimit , Edge[10][2]=uplimit;
Edge[10][3]=uplimit, Edge[10][4]=60; Edge[10][5]=uplimit ;
Edge[10][6]=uplimit , Edge[10][7]=uplimit , Edge[10][8]=10;
Edge[10][9]=uplimit, Edge[10][10]=0;
}
}
}
/* 找出场所间的最短距离--迪杰斯特拉算法 */
void ShortestDist(int s){
for ( int i=0;i<11;i++){ //dist和path数组初始化
dist[i]=Edge[s][i]; //邻接矩阵第s行元素赋值到dist中
S[i]=0; //已求出最短路径的顶点集合初始化
if(i!=s && dist[i]<uplimit){
path[i]=s;
}
else path[i]=-1; //路径存放数组初始化
}
S[s]=1; //顶点s加入顶点集合
dist[s]=0;
/* 循环计算该场所与邻接场所之间的最短距离 */
for (i=0;i<11-1;i++){ //从顶点s确定n-1条路径
float min=uplimit;
int u=s;
for (int j=0;j<11;j++){ //选择当前不在集合S中具有最短路径的顶点u
/* 如果有路径比目前的最小值还小,则替换这个最小值 */
if (!S[j] && dist[j]<min){
u=j;
min=dist[j];
}
}
S[u]=1; //将顶点u加入集合S,表示它已在最短路径上
for (int w=0;w<11;w++){ //修改
if (!S[w] && Edge[u][w]<uplimit && dist[u]+Edge[u][w]<dist[w]){
dist[w]=dist[u]+Edge[u][w];
path[w]=u;
}
}
}
}
void bh() //显示场所名称
{
cout<<"\t0.女生公寓 1.图书馆 2.黄华康体育馆"<<endl;
cout<<"\t3.艺术楼 4.田家炳教育书院 5.文科楼"<<endl;
cout<<"\t6.第一餐厅 7.办公楼 8.化学楼 生物楼"<<endl;
cout<<"\t9.物理楼 数学楼 10.实验楼"<<endl;
}
/*将顶点序列号转换成场所名称*/
void Outpath(int c)
{ switch(c)
{
case 0: cout<<"女生公寓";break;
case 1: cout<< "图书馆";break;
case 2: cout<< "黄华康体育馆";break;
case 3: cout<< "艺术楼";break;
case 4: cout<< "田家炳教育书院";break;
case 5: cout<< "文科楼";break;
case 6: cout<< "第一餐厅";break;
case 7: cout<< "办公楼";break;
case 8: cout<< "化学楼 生物楼";break;
case 9: cout<< "物理楼 数学楼";break;
case 10:cout<<"实验楼";break;
}
}
/* 输出两个场所之间的最短距离,和最短路径 */
void getdata(int s,int e){
D[0]=e;
int k;
for (k=0;D[k]!=s;k++){
D[k+1]=path[D[k]];
}
if(S[e]){
cout<<"\n\t场所"<<s<<","<<e<<"之间的最短距离是:"<<dist[e]<<endl;
cout<<"\n\t场所"<<s<<","<<e<<"之间的最短路径是:";
for(; k!=-1;k--){
Outpath(D[k]);
if (k!=0){
cout<<" --> ";
}
}
}
else
cout<<"\n\t场所"<<s<<"到场所"<<e<<"之间没有路径!"<<endl;
}
void Begin(){
int flag=1;
int s,e;
while ( flag ){
bh();
cout<<"\n\t请输入起始场所号与目的场所号:";
cin>>s>>e;
cout<<endl;
if(s<11 && s>=0 && e<11 && e>=0){
flag=0;
}
else
cout<<"\n场所号非法,请重新输入!";
}
ShortestDist(s);
getdata(s,e);
}
/*显示场所的具体信息*/
void info(int c) //c为场所对应的数字号
{
switch(c)
{
case 0:
cout<<"\t▲ 女生公寓,具体指女生第十一栋宿舍楼,住宿条件较好。"<<endl;
break;
case 1:
cout<<"\t▲ 图书馆,内部藏有丰富的书籍,供同学们学习参考,也可以自习。"<<endl;
break;
case 2:
cout<<"\t▲ 黄华康体育馆,由黄华康先生投资建造,供同学们进行体育活动。"<<endl;
break;
case 3:
cout<<"\t▲ 艺术楼,供艺术系的同学们学习使用,它拥有现代化的建造风格。"<<endl;
break;
case 4:
cout<<"\t▲ 田家炳教育书院,由田家炳先生投资建造,供同学们上课和自习使用。"<<endl;
break;
case 5:
cout<<"\t▲ 文科楼,供同学们上课和自习使用。"<<endl;
break;
case 6:
cout<<"\t▲ 第一餐厅,有两层楼,是同学们用餐的地方。"<<endl;
break;
case 7:
cout<<"\t▲ 办公楼,是教师办公的地方。"<<endl;
break;
case 8:
cout<<"\t▲ 化学楼 生物楼,这栋楼一边是化学楼,主要供化学系的同学上课和做实验使用;"<<endl;
cout<<"\t 另一边是生物楼,主要供生物系的同学上课和做实验使用。"<<endl;
break;
case 9:
cout<<"\t▲ 物理楼 数学楼,这栋楼一边是物理楼,主要供物理系的同学上课使用;"<<endl;
cout<<"\t 另一边是数学楼,主要供数学系的同学上课使用。"<<endl;
break;
case 10:
cout<<"\t▲ 实验楼,是同学们计算机上机的地方。"<<endl;
break;
case 11:
system("cls");
break;
default:
cout<<"\t输入不合法,请重新输入!"<<endl;
break;
}
}
void num(){
cout<<"\t\t****************************************"<<endl;
cout<<"\t\t***** 校园导航系统 *****"<<endl;
cout<<"\t\t****************************************"<<endl;
cout<<"\t\t 0.女生公寓"<<endl;
cout<<"\t\t 1.图书馆"<<endl;
cout<<"\t\t 2.黄华康体育馆"<<endl;
cout<<"\t\t 3.艺术楼"<<endl;
cout<<"\t\t 4.田家炳教育书院"<<endl;
cout<<"\t\t 5.文科楼"<<endl;
cout<<"\t\t 6.第一餐厅"<<endl;
cout<<"\t\t 7.办公楼"<<endl;
cout<<"\t\t 8.化学楼 生物楼"<<endl;
cout<<"\t\t 9.物理楼 数学楼"<<endl;
cout<<"\t\t 10.实验楼"<<endl;
}
void main(){
int c;
char option='0';
cout<<"\t----------------------------------------------------"<<endl;
cout<<"\t* * **** * **** **** * * **** "<<endl;
cout<<"\t* * * * * * * * * ** ** * "<<endl;
cout<<"\t * * * * **** * * * * * * * * **** "<<endl;
cout<<"\t ** ** * * * * * * * * * * "<<endl;
cout<<"\t * * **** **** **** **** * * **** "<<endl;
cout<<"\t-----------------------------------------------------"<<endl;
cout<<endl;
cout<<endl;
cout<<"\t*****************************************************"<<endl;
cout<<"\t\t\t欢迎光临海南师范大学"<<endl;
cout<<"\t*****************************************************"<<endl;
cout<<endl
<<"\t———————————————————————————"<<endl;
cout<<"\t\t\t1.显示场所的编号"<<endl;
cout<<"\t\t\t2.查看场所的具体信息"<<endl;
cout<<"\t\t\t3.找出最短路径及计算路径长度"<<endl;
cout<<"\t\t\t4.退出"<<endl;
cout<<"\t———————————————————————————"<<endl;
cout<<endl<<"\n\tWhat do you want to do?请输入选择:";
cin>>option;
while (option!='0'){
switch(option)
{
case '1':
num();
cout<<endl
<<"\t\t***************************************"<<endl;
cout<<"\t\t\t1.显示场所的编号"<<endl;
cout<<"\t\t\t2.查看场所的具体信息"<<endl;
cout<<"\t\t\t3.找出最短路径及计算路径长度"<<endl;
cout<<"\t\t\t4.退出"<<endl;
cout<<"\t\t***************************************"<<endl;
cout<<"\n\tWhat do you want to do ?请输入选择:";
cin>>option;
system("cls"); //清屏
break;
case '2': //具体信息
cout<<endl;
cout<<endl;
cout<<"\t请从0~10中选择任意字母,查看所对应场所的具体信息:"<<endl;
cout<<"\t选择11则退出该命令"<<endl;
bh(); //显示所有场所
cin>>c;
info(c);
if(c==11){
cout<<endl
<<"\t\t***************************************"<<endl;
cout<<"\t\t\t1.显示场所的编号"<<endl;
cout<<"\t\t\t2.查看场所的具体信息"<<endl;
cout<<"\t\t\t3.找出最短路径及计算路径长度"<<endl;
cout<<"\t\t\t4.退出"<<endl;
cout<<"\t\t***************************************"<<endl;
cout<<"\n\tWhat do you want to do ?请输入选择:";
cin>>option;
}
break;
case '3': //查询
BuildMap();
Begin();
cout<<endl
<<"\t\t***************************************"<<endl;
cout<<"\t\t\t1.显示场所的编号"<<endl;
cout<<"\t\t\t2.查看场所的具体信息"<<endl;
cout<<"\t\t\t3.找出最短路径及计算路径长度"<<endl;
cout<<"\t\t\t4.退出"<<endl;
cout<<"\t\t***************************************"<<endl;
cout<<"\n\tWhat do you want to do ?请输入选择:";
cin>>option;
system("cls");
break;
case '4': //退出
cout<<"\t----------------------------------------------------"<<endl;
cout<<"\t **** **** **** *** *** * * **** "<<endl;
cout<<"\t * * * * * * * * * * * * "<<endl;
cout<<"\t * * * * * * * *** * **** "<<endl;
cout<<"\t * *** * * * * * * * * * * "<<endl;
cout<<"\t **** * **** **** *** *** * **** "<<endl;
cout<<"\t----------------------------------------------------"<<endl;
getch();
exit(0);
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -