校园导游咨询——数据结构课程设计

来源:互联网 发布:网络最近流行的舞蹈 编辑:程序博客网 时间:2024/04/28 21:21

一、实验目的

二、使用仪器、器材

微机一台

操作系统:WinXP

编程软件:C++

三、实验内容及原理

1.校园导游咨询

【问题描述】

设计一个校园导游程序,为来访的客人提供各种信息查询服务。

【基本要求】

1)设计你的学校的校园平面图,所含景点不少于10个。以图中顶点表示学校各景点,存放景点名称、代号、简介等信息;以边表示路径,存放路径长度等相关信息。

2)为来访客人提供图中任意景点的问路查询,即查询任意两个景点之间的一条最短的简单路径。

3)为来访客人提供图中任意景点相关信息的查询。

【测试数据】

由读者根据实际情况指定。

【实现提示】

一般情况下,校园的道路是双向通行的,可设校园平面图是一个无向网。顶点和边均含有相关信息。

四、实验过程原始数据记录

实验源程序:

1、校园导游咨询

//graph.h

#define MVnum 10

#define NameLen 20

typedef char *VerTexType;//数据类型

typedef int ArcType;//边权类型

#include<iostream>

#include<queue>

#include<stack>

using namespace std;

typedef struct Closedge

{

int adjvex;

ArcType lowcost;

}Closedge;

typedef struct ArcNode

{

int adjvex;

ArcNode *nextarc;

ArcType info;

}ArcNode;

typedef struct VNode

{

VerTexType placename;

int  id;

char *info;

ArcNode *firstarc;

}VNode,AdjList;

typedef struct 

{

AdjList vertices[MVnum];

int vexnum,arcnum;

}ALGraph;

bool visited[MVnum];

int LocateVex(ALGraph G,char *name)

{

for(int i=0;i<G.vexnum;i++)

if(strcmp(name,G.vertices[i].placename)==0)

return i;

return -1;

}

int LocateVex(ALGraph G,int id)

{

for(int i=0;i<G.vexnum;i++)

if(G.vertices[i].id==id)

return i;

return -1;

}

int CreateUDG(ALGraph &G)

{

cout<<"input vexnum & arcnum"<<endl;

cin>>G.vexnum>>G.arcnum;

cout<<"input place information:like(id,placename,info)"<<endl;

for(int i=0;i<G.vexnum;i++)

{

cin>>G.vertices[i].id;

char *temp1=new char[NameLen];

cin>>temp1;G.vertices[i].placename=temp1;

char *temp2=new char[NameLen];

cin>>temp2;G.vertices[i].info=temp2;

G.vertices[i].firstarc=NULL;

}

char *v1,*v2;int i,j;ArcNode *p1,*p2;ArcType w;

cout<<"input road:(place1,place2,length)"<<endl;

for(int k=0;k<G.arcnum;k++)

{

v1=new char[NameLen];v2=new char[NameLen];

cin>>v1>>v2>>w;

i=LocateVex(G,v1);j=LocateVex(G,v2);

p1=new ArcNode;

p1->adjvex=j;p1->info=w;

p1->nextarc=G.vertices[i].firstarc;G.vertices[i].firstarc=p1;

//将新结点*p1插入顶点v1的边表头部

p2=new ArcNode;

p2->adjvex=i;p2->info=w;

p2->nextarc=G.vertices[j].firstarc;G.vertices[j].firstarc=p2;

}

return 1;

}

void DFS_AL(ALGraph G,VerTexType v1)

{

int v=LocateVex(G,v1);

cout<<v1<<" ";visited[v]=true;

ArcNode *p;int w;

p=G.vertices[v].firstarc;

while(p)

{

w=p->adjvex;

if(!visited[w])

DFS_AL(G,G.vertices[w].placename);

p=p->nextarc;

}

 

}

void DFSTraverse(ALGraph G)

{

for(int i=0;i<MVnum;i++)

visited[i]=false;

for(int i=0;i<G.vexnum;i++)

if(!visited[i])

DFS_AL(G,G.vertices[i].placename);

}

void BFS_AL(ALGraph G,VerTexType v1)

{

int v=LocateVex(G,v1);

cout<<v1<<" ";visited[v]=true;

queue<VerTexType> Q;

Q.push(v1);

VerTexType u1;

ArcNode *w;

int u;

while(!Q.empty())

{

u1=Q.front();

Q.pop();

u=LocateVex(G,u1);

for(w=G.vertices[u].firstarc;w!=NULL;w=w->nextarc)

{

if(!visited[w->adjvex])

{

cout<<G.vertices[w->adjvex].placename<<" ";visited[w->adjvex]=true;

Q.push(G.vertices[w->adjvex].placename);

}

}

}

}

void BFSTraverse(ALGraph G)

{

for(int i=0;i<MVnum;i++)

visited[i]=false;

for(int i=0;i<G.vexnum;i++)

if(!visited[i])

BFS_AL(G,G.vertices[i].placename);

}

int ShortPath_DIJ(ALGraph G,VerTexType v1,VerTexType v2)

{

bool S[MVnum];

int Path[MVnum];

ArcType D[MVnum];

int n=G.vexnum;

ArcNode *p;

int v0,vk,min,v;

v0=LocateVex(G,v1);vk=LocateVex(G,v2);

for(int i=0;i<n;i++)

{

S[i]=false;

Path[i]=-1;

D[i]=INT_MAX;

}

p=G.vertices[v0].firstarc;

while(p)

{

D[p->adjvex]=p->info;

Path[p->adjvex]=v0;

p=p->nextarc;

}

S[v0]=true;D[v0]=0;

/*------------初始化结束,开始主循环---------------*/

for(int i=1;i<G.vexnum;i++)

{

min=INT_MAX;

for(int w=0;w<n;w++)

{

if(!S[w]&&D[w]<min)

{

v=w;min=D[w];

}

}//for

S[v]=true;

p=G.vertices[v].firstarc;

while(p)

{

if(!S[p->adjvex]&&(D[v]+p->info)<D[p->adjvex])

{

D[p->adjvex]=D[v]+p->info;

Path[p->adjvex]=v;

}//if

p=p->nextarc;

}//while

 

}//for

cout<<v2<<"到"<<v1<<"的最短路径为:";

int k=vk;

while(k!=v0)

{

cout<<G.vertices[k].placename<<" ";

k=Path[k];

}

cout<<G.vertices[v0].placename<<endl;

return D[vk];

}

 

void Search(ALGraph G,VerTexType placename)

{

int i=LocateVex(G,placename);

if(i==-1)

{

cout<<"没有该景点"<<endl;

return ;

}

else

{

cout<<G.vertices[i].id<<'\t'<<G.vertices[i].placename<<'\t'<<G.vertices[i].info<<endl;

return ;

}

 

}

void Search(ALGraph G,int id)

{

int i=LocateVex(G,id);

if(i==-1)

{

cout<<"没有该景点"<<endl;

return ;

}

else

{

cout<<G.vertices[i].id<<'\t'<<G.vertices[i].placename<<'\t'<<G.vertices[i].info<<endl;

return ;

}

}

int SearchPlace(ALGraph G)

{

int flag;

cout<<"按(1.景点名;2.景点代码)查询";

cin>>flag;

VerTexType temp=new char[NameLen];

switch(flag)

{

case 1:

cout<<"input placename:";

cin>>temp;

Search(G,temp);

break;

case 2:

int id;

cout<<"input placeid:";cin>>id;

Search(G,id);

break;

default:

cout<<"wrong input!"<<endl;

break;

}

return 1;

}

int Traverse(ALGraph G)

{

int flag;

cout<<"遍历方式:1.深度;2.广度";

cin>>flag;

switch(flag)

{

case 1:DFSTraverse(G);break;

case 2:BFSTraverse(G);break;

default:

cout<<"wrong input!"<<endl;

break;

}

return 0;

}

//源.cpp

#include"graph.h"

int main()

{

ALGraph G;

while(1)

{

cout<<"----------校园导游程序----------"<<endl;

cout<<"--------1.初始化景点信息;------"<<endl;

cout<<"--------2.遍历景点;------------"<<endl;

cout<<"--------3.查询景点信息;--------"<<endl;

cout<<"--------4.查询最短路径;--------"<<endl;

cout<<"--------5.退出程序;------------"<<endl;

int work,flag=1;

cin>>work;

switch(work)

{

case 1:CreateUDG(G);break;

case 2:Traverse(G);cout<<endl;break;

case 3:SearchPlace(G);break;

case 4:

cout<<"input placename1&placename2:";

VerTexType v1,v2;

v1=new char[NameLen];v2=new char[NameLen];

cin>>v1>>v2;

ShortPath_DIJ(G,v2,v1);

break;

case 5:flag=0;break;

}

if(flag==0)

break;

}

cout<<"--------------------------------"<<endl;

return  0;

}