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

📄 collegewizarddoc.cpp

📁 这是我写的数据结构的课设
💻 CPP
字号:
// CollegeWizardDoc.cpp : implementation of the CCollegeWizardDoc class
//

#include "stdafx.h"
#include "CollegeWizard.h"

#include "CollegeWizardDoc.h"
#include "CollegeWizardView.h"
#include "CollegeMap.h"
#include "Vertex.h"
#include "Path.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCollegeWizardDoc

IMPLEMENT_DYNCREATE(CCollegeWizardDoc, CDocument)

BEGIN_MESSAGE_MAP(CCollegeWizardDoc, CDocument)
	//{{AFX_MSG_MAP(CCollegeWizardDoc)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCollegeWizardDoc construction/destruction

CCollegeWizardDoc::CCollegeWizardDoc()
{
	// TODO: add one-time construction code here
	g_pDoc = this;
	m_selected1 = NULL;
	m_selected2 = NULL;
	m_path	    = NULL;
}

CCollegeWizardDoc::~CCollegeWizardDoc()
{
}

BOOL CCollegeWizardDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CCollegeWizardDoc serialization

void CCollegeWizardDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CCollegeWizardDoc diagnostics

#ifdef _DEBUG
void CCollegeWizardDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CCollegeWizardDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CCollegeWizardDoc commands
//文档绘图函数
void CCollegeWizardDoc::Draw(CDC *pDC)
{
	CVertex* vertex = nuaaWizard.GetVertexList();
	CPath* path = NULL;
	while(vertex)
	{
		//调用CVertex类的Draw()函数绘制该景点或建筑
		vertex->Draw(pDC);
		path = vertex->GetPaths();
		while(path)
		{
			//调用CPath类的Draw()函数绘制该道路
			path->Draw(pDC);
		//	path->SetIsVisited();
	
			if(vertex->GetLocation() == path->GetLocation1())
				path = path->GetPath1();
			else
				path = path->GetPath2();	
		}
		vertex = vertex->GetNext();
	}
//	AfxMessageBox("wandahuatu!");//调试用
}

//DEL void CCollegeWizardDoc::OnFileSave() 
//DEL {
//DEL 	// TODO: Add your command handler code here
//DEL 
//DEL }
/////////////////////////////////////////////////////////////////////////////
//文档的保存
void CCollegeWizardDoc::OnFileSave() 
{
	// TODO: Add your command handler code here
	//文件过滤器
	CString strFilter = "我的文件 (*.pnt)|*.pnt||";
	CFileDialog saveDlg(FALSE,"pnt",NULL,NULL,strFilter);
 	saveDlg.DoModal();
	
	CString strName = saveDlg.GetPathName();
 	CFile saveFile;
 	if(!saveFile.Open(strName,CFile::modeCreate|CFile::modeReadWrite,NULL))
 	{
	//	MessageBox("打开文件失败!");
		AfxMessageBox("打开文件失败");
 	}
 	else
 	{
		//清空
		int totalOfVertexs = nuaaWizard.GetTotalOfVertexs();
		int totalOfPaths = nuaaWizard.GetTotalOfPaths();
		BOOL visitedTag = !nuaaWizard.GetVisitedTag();
		//存储总点数 
 		saveFile.Write(&totalOfVertexs,sizeof(totalOfVertexs));
		//存储总道路数 
		saveFile.Write(&totalOfPaths,sizeof(totalOfPaths));
		//存储访问标志
		saveFile.Write(&visitedTag,sizeof(visitedTag));
		//存储景点或建筑
		CVertex* vertex = nuaaWizard.GetVertexList();
		while(vertex)
		{
			saveFile.Write(vertex,sizeof(class CVertex));
			vertex = vertex->GetNext();
		}
		//存储道路
		vertex = nuaaWizard.GetVertexList();
		CPath* path = NULL;
		nuaaWizard.SetVisitedTag();
		while(vertex)
		{
			path = vertex->GetPaths();
			while(path)
			{
				if(path->GetIsVisited() != nuaaWizard.GetVisitedTag())
				{
					saveFile.Write(path,sizeof(class CPath));//存储道路
					path->SetIsVisited();
				}
				if(vertex->GetLocation() == path->GetLocation1())
					path = path->GetPath1();
				else
					path = path->GetPath2();				
			}
			vertex = vertex->GetNext();
		}

 		saveFile.Close();
//		AfxMessageBox("文件存储完成!");	//调式用 
 	}
}
/////////////////////////////////////////////////////////////////////////////
//文档打开函数
void CCollegeWizardDoc::OnFileOpen() 
{
	// TODO: Add your command handler code here
	//文件过滤器
	CString strFilter = "我的文件 (*.pnt)|*.pnt|所有文件 (*.*)|*.*||";
	CFileDialog openDlg(TRUE,"pnt",NULL,NULL,strFilter);
 	openDlg.DoModal();
	
	CString strName = openDlg.GetPathName();
 	CFile openFile;
 	if(!openFile.Open(strName,CFile::modeReadWrite,NULL))
	
		AfxMessageBox("打开文件失败");
 	else
 	{
		//此处还没有释放内存,应当将VertexList先析构!(只要反复调用deleteVertex()就行了)
		nuaaWizard.SetVertexList(NULL);
		nuaaWizard.SetTotalOfVertexs(0);
		nuaaWizard.SetTotalOfPaths(0);
 		int totalOfVertexs;
		int totalOfPaths;
		BOOL  visitedTag;
 		openFile.Read(&totalOfVertexs,sizeof(totalOfVertexs));
		openFile.Read(&totalOfPaths,sizeof(totalOfPaths));
		openFile.Read(&visitedTag,sizeof(visitedTag));
		if(nuaaWizard.GetVisitedTag() != visitedTag)
			nuaaWizard.SetVisitedTag();
		//读取景点或建筑
		CVertex* vertex = NULL;
		for(int i = 0; i < totalOfVertexs; i++)
		{
			vertex = new CVertex;
			openFile.Read(vertex,sizeof(class CVertex));
			vertex->SetNext(NULL);
			vertex->SetPaths(NULL);
			nuaaWizard.InsertVertex(vertex);
		}
		//读取道路
		CPath* path = NULL;
		for(i = 0; i < totalOfPaths; i++)
		{
			path = new CPath;
			openFile.Read(path,sizeof(class CPath));
			path->SetPath1(NULL);
			path->SetPath2(NULL);
			nuaaWizard.InsertPath(path);
		}

 		openFile.Close();
		
		g_pView->m_cmdType = ctUnknown;//将命令清零
		UpdateAllViews(0);
	//	AfxMessageBox("文件打开完成!");	//调式用
 	}
}

⌨️ 快捷键说明

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