⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 用VC++实现的一个简单校园导游系统
💻 CPP
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#define MAX_NAME 200
#define MAX_INFO 500
typedef int VRType;
typedef char VertexType[MAX_NAME];
#define INFINITY 65535
#define MAX_VERTEX_NUM 50

typedef struct 
{
	VRType adj;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

class MGraph
{
	public:
	VertexType vexs[MAX_VERTEX_NUM];
	AdjMatrix arcs;
	int vexnum;
	int arcnum;
    
};

typedef int DistancMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
 

//返回顶点在图中的序号
int LocateVex(MGraph G,VertexType u)
{
	int i;
	for(i =0; i< G.vexnum;i++)
	{
		if(strcmp(u,G.vexs[i]) == 0)
			return i;
	}
	return -1;
}



char *Find(char a[MAX_NAME])         //查找景点信息
{
	MGraph g;
	char Spot_name[MAX_NAME];
	static	char Spot_info[MAX_NAME];
	ifstream inf("景点信息.txt",ios::in);

	for(int p=0;p<MAX_VERTEX_NUM;++p)
	strcpy(g.vexs[p],"");


	for(int i=0;i<MAX_VERTEX_NUM;i++)
	{
		
		inf>>Spot_name>>Spot_info;

		if(!strcmp(Spot_name,a))
		{
			break;
		}

		
	}

	return Spot_info;

}

//read the file for creating the MGraph 
int CreateDN(MGraph &G)
{

	char start_Spot_name[MAX_NAME];
	char end_Spot_name[MAX_NAME];
	int  distance_of_Spot = 0;
	

	ifstream inf("最短路径.txt",ios::in);
	
	//init MGraph data

	//顶点数量
	G.vexnum = 0;

	//初始化两点之间的弧长信息,在这里即为两个城市间的距离
	//INFINITY在这里应当理解为无穷大的意思,65535只是一个参考值
	for(int p=0;p<MAX_VERTEX_NUM;++p)
		for(int k=0;k<MAX_VERTEX_NUM;++k)
			G.arcs[p][k].adj = INFINITY;

	for(p=0;p<MAX_VERTEX_NUM;++p)
		strcpy(G.vexs[p],"");

	int m = 0;
	for(int i=0;i<MAX_VERTEX_NUM;i++)
	{
		
		inf>>start_Spot_name;
		if(LocateVex(G,start_Spot_name) == -1)
		{
			strcpy(G.vexs[m++],start_Spot_name);
			G.vexnum++;
			
		}
	
		inf>>end_Spot_name;
		if(LocateVex(G,end_Spot_name) == -1)
		{
			strcpy(G.vexs[m++],end_Spot_name);
			G.vexnum++;

		}
	
		inf>>distance_of_Spot;
		int iStart = LocateVex(G,start_Spot_name);
		int iEnd   = LocateVex(G,end_Spot_name);

		G.arcs[iStart][iEnd].adj = distance_of_Spot;	
	}

	return 1;
}


//最短路径的floyd算法实现
void ShortestPathByFloyd(MGraph G,DistancMatrix &D)
{
	int u,v,w;
	for(v=0;v<G.vexnum;v++)
	{
		for(w=0;w<G.vexnum;w++)
		{
			D[v][w] = G.arcs[v][w].adj;
		}
		
	}
	for(u=0;u<G.vexnum;u++)
	{	
		for(v=0;v<G.vexnum;v++)
		{	
			for(w=0;w<G.vexnum;w++)
			{	
				if(D[v][u] + D[u][w] < D[v][w])
				{
					D[v][w] = D[v][u] + D[u][w];
				}
			}
		}
	}
}
int GetTheShortestDistance(char  start_Spot[],char end_Spot[])
{
	MGraph g;
	CreateDN(g);
	int i;
	
	for(i=0;i<g.vexnum;i++)
	{
		g.arcs[i][i].adj = 0;
	}
	DistancMatrix d;
	ShortestPathByFloyd(g,d);
	int nStart	= LocateVex(g,start_Spot);
	int nEnd	= LocateVex(g,end_Spot);
	if(nStart == -1)
	{
		cout<<"这个景点不存在:"<<endl<<start_Spot;
		return 0;
	}
	if(nEnd == -1)
	{
		cout<<"这个景点不存在:"<<endl<<end_Spot;
		return 0;
	}


	if(d[nStart][nEnd] == INFINITY)
	{
		return -1;
	}
	else
	{
		return d[nStart][nEnd];
	}
}
int main()
{

	int key,n;
	char start_Spot[MAX_NAME];
	char end_Spot[MAX_NAME];
	char Spot_name[MAX_NAME];
	cout<<"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  "<<endl;
	cout<<"以下是该校的所有景点:"<<endl;
	cout<<" 新校门	钟美林广场   明培体育馆   群贤楼   鲁迅塑像  大礼堂  建南楼群   体育场  清洁楼	海边沙滩  胡里山炮台  芙蓉楼  芙蓉湖	图书馆   南普陀寺  名山胜景  古刹春秋"<<endl;
	cout<<"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  "<<endl;
	while(1)
	{
		cout<<"1.查找景点信息"<<"2.寻找任两景点间最短路径"<<"3.退出查找"<<endl;
		cin>>n;
		switch(n)
		{
		case 1:
			do
			{	
				cout<<"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  "<<endl;
				cout<<"输入景点名字:"<<endl;
				cin>>Spot_name;
				cout<<"关于景点的信息:"<<endl;
				cout<<Find(Spot_name)<<endl;
				cout<<"如果你还想查景点信息,按'Y'或'y'继续查询,如果不想继续按任意键!"<<endl;
				cout<<"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "<<endl;
				cout<<endl;

			}
			while((key = getch()) == 'y' || key == 'Y');
			break;
		case 2:
			do
			{
				cout<<"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  "<<endl;
				cout<<"输入起始景点名字:"<<endl;
				cin>>start_Spot;
				cout<<"输入中止景点名字:"<<endl;
				cin>>end_Spot;

				int nDistance = GetTheShortestDistance(start_Spot,end_Spot);

				if(nDistance == -1)
				{
					cout<<"不能找到从"<<start_Spot <<"到" <<end_Spot<<endl;
				}
				else
				{
					cout<< start_Spot<<" 和 " <<end_Spot<< " 的 " << " 最短距离 " <<nDistance << " 米 " <<endl;
				}
				cout<<"如果你还想查最短距离,按'Y'或'y'继续查询,如果不想继续按任意键!"<<endl;
				cout<<"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  "<<endl;
			}
			while((key = getch()) == 'y' || key == 'Y');
			break;

		case 3:
			return 0;
		}

		
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -