一、實驗目的
二、使用儀器、器材
微機一臺
操作系統: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;
}