数据结构.docx
《数据结构.docx》由会员分享,可在线阅读,更多相关《数据结构.docx(16页珍藏版)》请在冰豆网上搜索。
![数据结构.docx](https://file1.bdocx.com/fileroot1/2022-12/17/6860b80f-16c0-4f17-9129-7a67ba199d0b/6860b80f-16c0-4f17-9129-7a67ba199d0b1.gif)
数据结构
——数据结构(C语言)课程设计
题目:
弗洛伊德算法
计算最短路径
班级:
计算机12-1班
姓名:
学号:
日期:
2014年1月16日
一.实习目的
通过实习,了解并初步掌握设计、实现较大系统的完整过程,包括系统分析、编码设计、系统集成、以及调试分析,熟练掌握数据结构的选择、设计、实现以及操作方法,为进一步的应用开发打好基础。
二.问题描述
设计、实现一个计算有向图各顶点之间最短路径的程序,用弗洛伊德算法,结果用矩阵输出。
三.需求分析
该程序所做的工作的是求最短路径问题。
此程序规定:
(1)在程序中建立有向图,为边输入权值。
(2)程序的输出信息主要是:
图中各个顶点到其它顶点之间的最短路径及其长度。
结果用矩阵表示。
(3)程序的功能包括:
随机建立有向图、手动建立有向图、弗洛伊德算法。
四.概要设计
∙系统用到的抽象数据类型定义:
∙classCDiGraph
∙{
∙public:
∙CDiGraph();
∙virtual~CDiGraph();
∙public:
∙基本数据:
∙voidDrawFloyd(CDC*pDC);
∙voidFloyd();
∙voidTransform();
∙voidInitHand();
∙//有向图的当前顶点数目
∙intvexnum;
∙//有向图的当前边数目
∙intarcnum;
∙//有向图深度优先已经遍历顶点数目
∙intm_nDFSnum;
∙//有向图存储链表
∙CTypedPtrListm_DigraphList;
∙CStringArrayvex[MAX];
∙intArrayweight[MAX][MAX];
∙CStringPath[MAX][MAX];
基本操作:
voidCreateDGRandom(CPointvCenterPoint);
//自动创建有向图
voidCreateDGHand(CPointvCenterPoint);
//手动创建有向图
///////////////////////////////////////////////////////
//有向图基本函数//
///////////////////////////////////////////////////////
intLocateInList(CStringvName);
//判断顶点vPoint是否在有向图存储链表
CGraphVertex*IsPointInList(CPointvPoint);
//判断顶点名vName是否在有向图存储链表
CGraphVertex*IsNameInList(CStringvName);
//判断边(pBeginVex,pEndVex)是否在有向图中
BOOLIsEdgeExist(CGraphVertex*pBeginVex,CGraphVertex*pEndVex);
//判断名为vName的顶点是否在有向图中存储链表中
CGraphVertex*IsVexNameInList(CStringvName);
//查找名为vName的顶点在有向图中存储链表中的地址
CGraphVertex*FindVexNameInList(CStringvName);
//设置边(vBeginVex,vEndVex)的权值
voidSetEdgeWeight(CStringvBeginVex,CStringvEndVex,intvWeight);
voidDeleteVex(CPointvPoint);
//删除显示位置为vPoint的顶点
voidDeleteEdge(CGraphVertex*pBeginVex,CGraphVertex*pEndVex);
//删除边(pBeginVex,pEndVex)
voidInsertEdge(CGraphVertex*pBeginVex,CGraphVertex*pEndVex,intweight);
//插入顶点(pBeginVex,pEndVex)之间的边
///////////////////////////////////////////////////////
//有向图显示函数//
///////////////////////////////////////////////////////
//有向图可视化显示
voidDrawDiGraph(CDC*pDC);
//显示有向图边
voidDrawEdges(CDC*pDC);
//显示有向图顶点
voidDrawVexs(CDC*pDC);
∙};
画图类:
∙classCDiGraphDraw:
publicCFormView
∙{
∙protected:
∙CDiGraphDraw();//protectedconstructorusedbydynamiccreation
∙DECLARE_DYNCREATE(CDiGraphDraw)
∙
∙//FormData
∙public:
∙//{{AFX_DATA(CDiGraphDraw)
∙enum{IDD=IDD_DIGRHDRAW_FORMVIEW};
∙//NOTE:
theClassWizardwilladddatamembershere
∙//}}AFX_DATA
∙
∙//Attributes
∙public:
∙
∙//Operations
∙public:
∙voidDrawFloyd(CDC*pDC);
∙//画出弗洛伊德算法的结果
∙voidFloyd();
∙voidSetEdgeWeightHand();
∙//手动设置权值
∙voidDelEdgesHand();
∙//手动删除边
∙voidAddEdgesHand();
∙//手动添边
∙voidMovVertsHand();
∙BOOLm_Capture;
∙voidDelVertsHand();
∙//手动删除顶点
∙voidAddVertsHand();
∙//手动添加顶点
∙voidCreateDGHand();
∙//手动创建有向图
∙voidCreateDGRandom();
∙//自动创建有向图
∙voidComputeFloyd();
∙CGraphVertex*m_pEndNode;
∙CGraphVertex*m_pBeginNode;
∙CPointm_StartPoint;
∙intm_FunType;
∙voidDrawDGHand(CDC*pDC);
∙voidDrawDGRandom(CPointvCenterPoint,CDC*pDC);
∙staticDWORDWINAPIDiGraphproc(LPVOIDlpParameter);
∙CDataStructVisualDoc*GetDocument();
∙CDataStructVisualDoc*pDoc;
∙boolm_StartFlag;
∙HANDLEhEventDiGraph;
∙HANDLEhThreadDiGraph;
∙intm_flag;
∙有向图边的类:
∙classCGraphEdge:
publicCObject
∙{
∙public:
∙CGraphEdge();
∙virtual~CGraphEdge();
∙public:
∙boolEdgeDraw(CGraphVertex*pBeginVex,CDC*pDC);
∙intinfo;
∙intm_weight;//边的权值
∙COLORREFm_color;//图边颜色
∙CGraphVertex*m_pAdjVertex;
∙CGraphEdge*m_pNextEdge;
∙}
∙有向图顶点的类:
∙classCGraphVertex:
publicCObject
∙{
∙public:
∙CGraphVertex();
∙virtual~CGraphVertex();
∙public:
∙boolVexDraw(CDC*pDC);
∙CPointm_point;
∙COLORREFm_color;//图顶点颜色
∙CGraphEdge*m_pFirstEdge;
∙charm_strname;
∙BOOLm_bvisit;
∙intm_nvisit;
∙intm_pos;
∙
∙};
∙弗洛伊德算法及其画图的代码:
∙voidCDiGraph:
:
Floyd()
∙{
∙Transform();
∙intA[MAX][MAX];
∙inti,j,k;
∙for(i=0;i∙{
∙for(j=0;j∙{
∙A[i][j]=Arrayweight[i][j];
∙if(A[i][j]!
=0&&A[i][j]∙{
∙Path[i][j]=Arrayvex[i]+Arrayvex[j];
∙}
∙}
∙}
∙for(k=0;k∙{
∙for(i=0;i∙{
∙for(j=0;j∙{
∙if(A[i][j]>(A[i][k]+A[k][j]))
∙{
∙if(A[i][k]∙{
∙A[i][j]=A[i][k]+A[k][j];
∙if(Path[i][k]!
='0'&&Path[k][j]!
='0')
∙{
∙Path[i][j]=Path[i][k]+Arrayvex[j];
∙}
∙
∙}
∙}
∙}
∙}
∙}
∙for(i=0;i∙{
∙for(j=0;j∙{
∙Arrayweight[i][j]=A[i][j];
∙}
∙}
∙}
∙
∙voidCDiGraph:
:
DrawFloyd(CDC*pDC)
∙{
∙CStringstr;
∙CPointm_point;
∙m_point.y=50;
∙m_point.x=700;
∙pDC->MoveTo(m_point.x,m_point.y);
∙pDC->LineTo(m_point.x-10,m_point.y+10);
∙pDC->MoveTo(m_point.x-10,m_point.y+10);
∙pDC->LineTo(m_point.x-10,m_point.y+vexnum*20-10);
∙pDC->MoveTo(m_point.x-10,m_point.y+vexnum*20-10);
∙pDC->LineTo(m_point.x,m_point.y+vexnum*20);
∙for(inti=0;i∙{
∙m_point.x=700;
∙for(intj=0;j∙{
∙if(Arrayweight[i][j]∙{
∙str.Format("%d",Arrayweight[i][j]);
∙pDC->TextOut(m_point.x,m_point.y,str);
∙m_point.x+=20;
∙}
∙else
∙{
∙str.Format("%d",Arrayweight[0][0]);
∙pDC->TextOut(m_point.x,m_point.y,str);
∙m_point.x+=20;
∙}
∙}
∙m_point.y+=20;
∙}
∙pDC->MoveTo(m_point.x-10,m_point.y);
∙pDC->LineTo(m_point.x,m_point.y-10);
∙pDC->MoveTo(m_point.x,m_point.y-10);
∙pDC->LineTo(m_point.x,m_point.y-vexnum*20+10);
∙pDC->MoveTo(m_point.x,m_point.y-vexnum*20+10);
∙pDC->LineTo(m_point.x-10,m_point.y-vexnum*20);
∙
∙m_point.y=250;
∙m_point.x=700;
∙pDC->MoveTo(m_point.x,m_point.y);
∙pDC->LineTo(m_point.x-10,m_point.y+10);
∙pDC->MoveTo(m_point.x-10,m_point.y+10);
∙pDC->LineTo(m_point.x-10,m_point.y+vexnum*20-10);
∙pDC->MoveTo(m_point.x-10,m_point.y+vexnum*20-10);
∙pDC->LineTo(m_point.x,m_point.y+vexnum*20);
∙for(i=0;i∙{
∙m_point.x=700;
∙for(intj=0;j∙{
∙str.Format("%s",Path[i][j]);
∙pDC->TextOut(m_point.x,m_point.y,str);
∙m_point.x+=45;
∙}
∙m_point.y+=20;
∙}
∙pDC->MoveTo(m_point.x-10,m_point.y);
∙pDC->LineTo(m_point.x,m_point.y-10);
∙pDC->MoveTo(m_point.x,m_point.y-10);
∙pDC->LineTo(m_point.x,m_point.y-vexnum*20+10);
∙pDC->MoveTo(m_point.x,m_point.y-vexnum*20+10);
∙pDC->LineTo(m_point.x-10,m_point.y-vexnum*20);
∙}
∙界面显示:
∙classCLeftPane:
publicCFormView
∙{
∙protected:
∙CLeftPane();//protectedconstructorusedbydynamiccreation
∙DECLARE_DYNCREATE(CLeftPane)
∙
∙//FormData
∙public:
∙//{{AFX_DATA(CLeftPane)
∙enum{IDD=IDD_LEFTPANE_FORMVIEW};
∙CTreeCtrlm_LeftTree;
∙//}}AFX_DATA
∙
∙//Attributes
∙public:
∙
∙//Operations
∙public:
∙CRightFrame*m_pRightSwitchFrame;
∙
∙//Overrides
∙//ClassWizardgeneratedvirtualfunctionoverrides
∙//{{AFX_VIRTUAL(CLeftPane)
∙public:
∙virtualvoidOnInitialUpdate();
∙protected:
∙virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
∙virtualvoidCalcWindowRect(LPRECTlpClientRect,UINTnAdjustType=adjustBorder);
∙//}}AFX_VIRTUAL
∙
∙//Implementation
∙protected:
∙virtual~CLeftPane();
∙#ifdef_DEBUG
∙virtualvoidAssertValid()const;
∙virtualvoidDump(CDumpContext&dc)const;
∙#endif
∙
∙//Generatedmessagemapfunctions
∙//{{AFX_MSG(CLeftPane)
∙afx_msgvoidOnSize(UINTnType,intcx,intcy);
∙afx_msgvoidOnCancelMode();
∙afx_msgvoidOnSelchangedLeftpaneTree(NMHDR*pNMHDR,LRESULT*pResult);
∙//}}AFX_MSG
∙DECLARE_MESSAGE_MAP()
∙private:
∙voidInitTree();
∙HTREEITEMm_Root;
∙CImageListm_TreeImageList;
∙CRectm_sRect;
∙};
树的显示:
∙voidCLeftPane:
:
InitTree()
∙{
∙LPSTRpszText;
∙m_TreeImageList.Create(16,16,TRUE,6,1);
∙HICONhIcon;
∙hIcon=:
:
LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON1));
∙m_TreeImageList.Add(hIcon);
∙hIcon=:
:
LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON2));
∙m_TreeImageList.Add(hIcon);
∙hIcon=:
:
LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON3));
∙m_TreeImageList.Add(hIcon);
∙hIcon=:
:
LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON4));
∙m_TreeImageList.Add(hIcon);
∙hIcon=:
:
LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON5));
∙m_TreeImageList.Add(hIcon);
∙m_LeftTree.SetImageList(&m_TreeImageList,TVSIL_NORMAL);
∙//////////////////////在树视图控件添加信息/////////////////////////////////////
∙m_LeftTree.DeleteAllItems();//清空当前书控件所有节点
∙m_Root=m_LeftTree.InsertItem("动态切换视图");//插入根节点
∙TV_INSERTSTRUCTTCItem;//设屏蔽
∙TCItem.item.mask=TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
∙TCItem.hInsertAfter=TVI_LAST;//在最后项之后
∙CStringstrTreeNodeName="测试一";
∙pszText=strTreeNodeName.LockBuffer();
∙TCItem.hParent=m_Root;
∙TCItem.item.pszText=pszText;
∙TCItem.item.iImage=1;
∙TCItem.item.iSelectedImage=2;
∙HTREEITEMhCurrent=m_LeftTree.InsertItem(&TCItem);
∙m_LeftTree.SetItemData(hCurrent,1);
∙strTreeNodeName="测试二";
∙pszText=strTreeNodeName.LockBuffer();
∙TCItem.hParent=m_Root;
∙TCItem.item.pszText=pszText;
∙TCItem.item.iImage=1;
∙TCItem.item.iSelectedImage=2;
∙hCurrent=m_LeftTree.InsertItem(&TCItem);
∙m_LeftTree.SetItemData(hCurrent,2);
∙m_LeftTree.Expand(m_Root,TVE_EXPAND);//展开根节点
∙strTreeNodeName="单链表";
∙pszText=strTreeNodeName.LockBuffer();
∙TCItem.hParent=m_Root;
∙TCItem.item.pszText=pszText;
∙TCItem.item.iImage=1;
∙TCItem.item.iSelected