头文件
#define MAXV 100//最大顶点个数 #define INF 32767//INF表示∞ #include<iostream> #include<stdio.h> using namespace std; #include<string> #include<fstream> #include<stdio.h> #include <malloc.h> typedef int InfoType; /*定义校园景点信息*/ typedef struct { int no;//校园景点编号 string name;//校园景点名称 string introduct;//校园景点介绍 } VertexType; typedef struct { VertexType vexs[MAXV]; //景点 int Arc[MAXV][MAXV]; //景点间的路 int n,e; //景点数n和路数e } ALGraph; void ReadArc(ALGraph *g); //图类 void readvernum(ALGraph *g); void ArrayToMat(int *Arr, int n,ALGraph &g); //数组构造图的邻接矩阵 //void DispMat(MGraph g);//输出邻接矩阵g void Photo();//打印界面 void ShowName();//显示名称 void StateVisit(ALGraph *G); void DFS(ALGraph *G,int v); void PrintName_1();//读取景点名称 void PaintMap_2();//绘制校园地图 void DFSinfo_3(ALGraph *G);//深度遍历校园 void InquireMess_4();;//查询景点介绍 void shortload(ALGraph *g); void FlPath_5(ALGraph* g);//指导最短路径 void Exit_0();//退出导游系统 void MENU();//主菜单
源文件
#include"1.h" int visited[MAXV]; int shortest[MAXV][MAXV];//最短路径 int path[MAXV][MAXV];//经过景点 /*打印界面*/ void Photo() { //system("color 47"); printf("-o-o-o-o-o-o-o-o-o校园景点导游:o-o-o-o-o-o-o-o-o-o-o-o\n"); printf("|1 for 读取景点名称~~~~~~~~~~~~~~~~~4 for 查询景点介绍|\n"); printf("|2 for 绘制校园地图~~~~~~~~~~~~~~~~~5 for 指导最短路径|\n"); printf("|3 for 深度遍历校园~~~~~~~~~~~~~~~~~0 for 退出导游系统|\n"); printf("-o-o-o-o-o-o-o-o-o-o你的选择是:o-o-o-o-o-o-o-o-o-o-o-o\n"); } /*文件读取边*/ void ReadArc(ALGraph *g) { FILE *fp; int i=0,j=0,k=0; for(i=0;i<g->n;i++) for(j=0;i<g->n;j++) g->Arc[i][j]=INF; fp=fopen("arry.txt","r"); while(fscanf(fp,"%d %d %d",&i,&j,&k)!=EOF) { g->Arc[i-1][j-1]=k; } fclose(fp); } /*文件读取景点信息*/ void readvernum(ALGraph *g) { FILE *fp; int i=0; fp=fopen("info.txt","rt"); while(fscanf(fp,"%d %s %s",&g->vexs[i].no,g->vexs[i].name,g->vexs[i].introduct)!=EOF) { printf("景点序号:%d 名称:%s\n",g->vexs[i].no,g->vexs[i].name); printf("景点信息:%s\n",g->vexs[i].introduct); printf("\n"); i++; } g->n=i; fclose(fp); } /*读取景点名称*/ void PrintName_1() { ; } /*绘制校园地图*/ void PaintMap_2() { ; } /*访问变量置零*/ void StateVisit(ALGraph *G) { int i; for(i=0;i<G->n;i++) { visited[i]=0; } } /*深度遍历校园*/ void DFSinfo_3(ALGraph *G) { int n; StateVisit(G); cout<<"请输入起始地点:\n"; cin>>n; DFS(G,n-1); } /*DFS*/ void DFS(ALGraph *G,int v) { int j; if(!visited[v]) { visited[v]=1; //标记为访问过 cout<<"景点:"<<G->vexs[v].name<<"\n介绍:"<<G->vexs[v].introduct<<endl; } for(j=0;j<G->n;j++) if ((G->Arc[v][j]!=INF)&&!visited[j])//邻接矩阵的第(v,j)元素不为0 { //且未被访问过则递归 DFS(G,j); } } /*查询景点介绍*/ void InquireMess_4() { ; } /*最短路径*/ void shortload(ALGraph *g) { int i,j,a,b; //PlaceList(); FlPath_5(g); printf("请输入起始景点和终止景点(1-%d):\n",g->n); scanf("%d%d",&i,&j); a=i; b=j; i=i-1; j=j-1; if(i<j) { printf("%d",b); while(path[i][j]!=0) { printf("<-%d",path[i][j]+1); if(i<j) j=path[i][j]; else i=path[j][i]; } printf("<-%d",a); printf("\n\n"); printf("%d->%d 距离是:%d米\n\n",a,b,shortest[a-1][b-1]); } else { printf("%d",a); while(path[i][j]!=0) { printf("<-%d",path[i][j]+1); if(i<j) j=path[i][j]; else i=path[j][i]; } printf("<-%d",b); printf("\n\n"); printf("%d->%d 最短距离是:%d米\n\n",a,b,shortest[a-1][b-1]); } } /*指导最短路径*/ void FlPath_5(ALGraph* g) { int i,j,k; for(i=0; i<g->n; i++) for(j=0; j<g->n; j++) shortest[i][j]=0; for(i=0; i<g->n; i++) for(j=0; j<g->n; j++) { shortest[i][j]=g->Arc[i][j]; path[i][j]=0; } for(i=0; i<g->n; i++) for(j=0; j<g->n; j++) for(k=0; k<g->n; k++) 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; } } /*退出导游系统*/ void Exit_0() { printf("感谢,再见!\n"); exit(0); } /*主菜单*/ void MENU() { int choice; ALGraph *g; g=(ALGraph *)malloc(sizeof(ALGraph));//创建头结点 Photo(); while(1) { cin>>choice; switch(choice) { case 1: { ReadArc(g); PrintName_1();//读取景点名称 break; } case 2: { PaintMap_2();//绘制校园地图 break; } case 3: { ReadArc(g); DFSinfo_3(g);//深度遍历校园 break; } case 4: { InquireMess_4(); break;//查询景点介绍 } case 5: { //ReadArc(g); FlPath_5(g);//指导最短路径 break; } case 0:Exit_0();break;//退出导游系统 default:cout<<"What you inputed is wrong.\n";break; } void Photo(); } }
主文件
#include "1.h" int main() { MENU(); return 0; }
显示