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

📄 map.cpp

📁 这是帮那留学生做的第二个作业,里面有详细的文档资料,要求运用到 链表 图 和最短路径算法. Dijkstra
💻 CPP
字号:
// map.cpp: implementation of the map class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "map.h"
#include<fstream.h>
#include<iomanip.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

map::map()
{
	number=0;
	towns=NULL;
	start=0;
}

map::~map()
{
	if(towns!=NULL)
		delete[] towns;
}

bool map::loadfile(char *filename)
{
	int tempdist=0;
	fstream f;
	f.open(filename,ios::in|ios::nocreate);
	if(!f)
		return false;
	f>>number;
	towns=new town[number];
	char name[80];
	for(int i=0;i<number;i++)
	{
		f>>name;
		towns[i].SetName(name);
//		cout<<towns[i].name<<endl;//~~~~~~~~~~~~~~~~~~~~~~~~~~~
	}
	for(i=0;i<number-1;i++)
	{
 		for(int j=i+1;j<=number-1;j++)
		{
			f>>tempdist;
			if(tempdist==0){
				tempdist=Infinity;
			}
			connection *p=new connection;
			p->town=j;
			p->dist=tempdist;
			towns[i].connections.insert(p);
//			cout<<p->distance<<endl; //~~~~~~~~~~~~~~~~~~~~~~~~
			connection *p2;
			p2=new connection;
			p2->town=i;
			p2->dist=tempdist;
			towns[j].connections.insert(p2);						
		}
	}
	f.close();
	return true;
}

void map::printmap()
{
	int *a;
	Dijkstra path;
	a=path.findpath(towns,number,start);
	for(int i=0 ;i<number;i++)
		cout <<towns[i].name<<":"
		<<a[i]<<endl;


}

void map::SetStart(int m_start)
{
	start=m_start;
}

⌨️ 快捷键说明

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