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

📄 shortview.cpp

📁 一个路由器最短路径算法的源程序
💻 CPP
字号:
// shortView.cpp : implementation of the CShortView class
//

#include "stdafx.h"
#include "short.h"

#include "shortDoc.h"
#include "shortView.h"
#include "dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CShortView

static luyou   R1[10];	
static CSemaphore g_Sp(1,1);
IMPLEMENT_DYNCREATE(CShortView, CView)

BEGIN_MESSAGE_MAP(CShortView, CView)
	//{{AFX_MSG_MAP(CShortView)
	ON_COMMAND(ID_SHORT_SHOW, OnShortShow)
	ON_COMMAND(ID_INIT, OnInit)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShortView construction/destruction

CShortView::CShortView()
{
	// TODO: add construction code here

}

CShortView::~CShortView()
{
}

BOOL CShortView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CShortView drawing

void CShortView::OnDraw(CDC* pDC)
{
	dc = GetDocument();
	ASSERT_VALID(dc);

	CPen mypen1,mypen2,mypen3,*oldpen;
    mypen1.CreatePen(PS_SOLID,2,RGB(0,0,255));
    mypen2.CreatePen(PS_SOLID,2,RGB(255,0,0));
    mypen3.CreatePen(PS_SOLID,2,RGB(0,255,0));
	// TODO: add draw code for native data here
	CString name1,name2,name3;
	name1="路由器--主机的路径图";
	name2="-------- 路由器";
    name3="-------- 主机";
	pDC->TextOut(300,40,name1); 
	pDC->TextOut(50,390,name2);
	pDC->TextOut(50,440,name3);
	pDC->Rectangle(20,390,40,410);
	pDC->Ellipse(20,440,40,460);
	// 路径图的标题  

     int m,n;
                
	 for(m=0;m<10;m++)
	 {  
		 for(n=0;n<10;n++) 
		 {  if(dc->ly_paint[m][n]==1)
		       {
			 oldpen=pDC->SelectObject(&mypen1);
			    pDC->MoveTo(dc->ly[m].x+10,dc->ly[m].y+10);
		        pDC->LineTo(dc->ly[n].x+10,dc->ly[n].y+10);
		 pDC->SelectObject(oldpen);
		 }
		   
			  if(dc->ly_paint[m][n]==2)
			  {
				  oldpen=pDC->SelectObject(&mypen2);
			    pDC->MoveTo(dc->ly[m].x+10,dc->ly[m].y+10);
		        pDC->LineTo(dc->ly[n].x+10,dc->ly[n].y+10);
			    dc->ly_paint[m][n]=1;
				pDC->SelectObject(oldpen);
			  }
			  
		 }
	 }
	 //路由器之间的连线的绘制

     int j,k;
	 for(j=0;j<10;j++)
	 {  
		 for(k=0;k<10;k++) 
		 { 
			 if(dc->zhj_paint[j][k]==1)
			 {
				 oldpen=pDC->SelectObject(&mypen1);
				 pDC->MoveTo(dc->zhj[j].x+10,dc->zhj[j].y+10);
				 pDC->LineTo(dc->ly[k].x+10,dc->ly[k].y+10);
				 pDC->SelectObject(oldpen);
			 }
			 
			 if(dc->zhj_paint[j][k]==2)
			 {
				 oldpen=pDC->SelectObject(&mypen2);
				 pDC->MoveTo(dc->zhj[j].x+10,dc->zhj[j].y+10);
				 pDC->LineTo(dc->ly[k].x+10,dc->ly[k].y+10);
				 dc->zhj_paint[j][k]=1;
				 pDC->SelectObject(oldpen);
			 }
		 }
	 }
	 //主机与路由器之间的连线的绘制

    pDC->SelectObject(&mypen3);
	int i;
	for(i=0;i<10;i++)
	{  pDC->Rectangle(dc->ly[i].x,dc->ly[i].y,dc->ly[i].x+20,dc->ly[i].y+20);
	   pDC->TextOut(dc->ly[i].x+5,dc->ly[i].y+2,i+48);}
	//路由器节点的绘制
	 

	 int w;
	 for(w=0;w<9;w++)
	 {  pDC->Ellipse(dc->zhj[w].x,dc->zhj[w].y,dc->zhj[w].x+20,dc->zhj[w].y+20);
	 pDC->TextOut(dc->zhj[w].x+6,dc->zhj[w].y+2,w+97);}
     //主机节点的绘制

     
     pDC->Rectangle(20,390,40,410);
	 pDC->Ellipse(20,440,40,460);


	 

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CShortView printing

BOOL CShortView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CShortView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CShortView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CShortView diagnostics

#ifdef _DEBUG

CShortDoc* CShortView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShortDoc)));
	return (CShortDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CShortView message handlers

void CShortView::OnShortShow() 
{
	// TODO: Add your command handler code here
	dc = GetDocument();
	ASSERT_VALID(dc);
	dlg newdlg;
	int result=newdlg.DoModal();
	   if(result==IDOK)
	   {   
		   CShortDoc* dc = GetDocument();
		   ASSERT_VALID(dc);
		   dc->start=newdlg.m_start;
		   dc->end=newdlg.m_end;
		   char x=(char)dc->start[0];
		   char y=(char)dc->end[0];		 
		   backpatch(x,y);
		  // SetTimer(1,100,NULL);
		   Invalidate();
	   }
}

void CShortView::snull(int x[10])
{
	int o=0;
	while(o<10)
	{
		x[o]=0;
		o++;
	}
}

void CShortView::scat1(int x[10], int y)
{
	int i=0;
	while(x[i]!=0)
	{
		i++;
	}
	
	x[i]=y;
	i++;
}

void CShortView::scat2(int x[10], int y[10])
{
	int j=0;
	int i=0;
	while(x[i]!=0)
	{
		i++;
	}
	while(y[j]!=0)
	{
		x[i]=y[j];
		i++;
		j++;
	}
}

void CShortView::backpatch(char x, char y)
{
	dc = GetDocument();
	ASSERT_VALID(dc);
	int i=0;
	int j=0;
	int r=0;r=x-97;int f=0; f=y-97;
	int temp1=0;
	int temp2=0;
	int k=0;	
	for(i=0;i<=9;i++)
		for(j=0;j<20;j++)
		{ 
			if((R1[i].computer[j]==x))
				temp1=i;
			
		}
 dc->zhj_paint[r][temp1]=2;				

		///////////
		for(int i1=0;i1<=9;i1++)
			for(int j1=0;j1<20;j1++)
			{ 
				if((R1[i1].computer[j1]==y))
					temp2=i1;
			}

     dc->zhj_paint[f][temp2]=2;				

			//////////////////////////////////

		      if(R1[temp1].biao[temp2].path[0]==10)
			{
				dc->ly_paint[0][temp1]=dc->ly_paint[temp1][0]=2; 
			}
			  else if(R1[temp1].biao[temp2].path[0]!=0)
			{
				dc->ly_paint[R1[temp1].biao[temp2].path[0]][temp1]= dc->ly_paint[temp1][R1[temp1].biao[temp2].path[0]]=2; 
			}
			
			while(R1[temp1].biao[temp2].path[k]!=0)
			{  
				if(R1[temp1].biao[temp2].path[k]==10)
				{ 
					dc->ly_paint[0][R1[temp1].biao[temp2].path[k+1]]=2;
					dc->ly_paint[R1[temp1].biao[temp2].path[k+1]][0]=2; 
				}
				else if(R1[temp1].biao[temp2].path[k+1]==10)
				{ 
					dc->ly_paint[R1[temp1].biao[temp2].path[k]][0]=2;
					dc->ly_paint[0][R1[temp1].biao[temp2].path[k]]=2; 
				}
				else if(R1[temp1].biao[temp2].path[k+1]!=0)
				{
					dc->ly_paint[R1[temp1].biao[temp2].path[k]][R1[temp1].biao[temp2].path[k+1]]=2;
					dc->ly_paint[R1[temp1].biao[temp2].path[k+1]][R1[temp1].biao[temp2].path[k]]=2;    
				}
				k++;
			}
}

void CShortView::OnInit() 
{
	// TODO: Add your command handler code here

	dc=GetDocument();
	ASSERT_VALID(dc);
	int i1=0;
	int j1=0;	
	for(i1=0;i1<=9;i1++)
		for(j1=0;j1<=9;j1++)
			R1[i1].biao[j1].distance=1000;		           
		R1[0].biao[0].distance=0;  
		R1[0].computer[0]='a';
		
		R1[1].biao[1].distance=0;
		R1[1].computer[0]='\0';
		
		R1[2].biao[2].distance=0;  
		R1[2].computer[0]='i';
		
		R1[3].biao[3].distance=0;  
		R1[3].computer[0]='h';
		
		R1[4].biao[4].distance=0;  
		R1[4].computer[0]='b';
		R1[4].computer[1]='c';
		
		R1[5].biao[5].distance=0;  
		R1[5].computer[0]='\0';
		
		R1[6].biao[6].distance=0;  
		R1[6].computer[0]='\0';
		
		R1[7].biao[7].distance=0;  
		R1[7].computer[0]='g';
		
		R1[8].biao[8].distance=0;  
		R1[8].computer[0]='d';
		
		R1[9].biao[9].distance=0;  
		R1[9].computer[0]='e';
		R1[9].computer[1]='f';
		
		for(int k=0;k<10;k++)
			R1[k].p=0;

		for(int i3=1;i3<=9;i3++)
			if(dc->ly_paint[i3][0]==1)
			{    
//				R1[i3].nearly[R1[i3].p]=10;
				R1[i3].p++;
				R1[i3].biao[0].distance=1;
				R1[i3].biao[0].path[0]=10;
			}	
			int mark=0;
		for(int i2=0;i2<=9;i2++) 
		{
			for(int j2=1;j2<=9;j2++)
			{
				if(dc->ly_paint[i2][j2]==1)
				{  
				//	R1[i2].nearly[R1[i2].p]=j2;
					R1[i2].biao[j2].distance=1;
					if(R1[i2].biao[j2].path[mark]!=0)
						mark++;
				    R1[i2].biao[j2].path[mark]=j2;
					mark=0;
					R1[i2].p++;
				} 
			}
		}
		mutiThread();
		//shortPath();					
}


void CShortView::mutiThread()
{
	for(int i=0;i<10;i++)
	{
		canshu *cs=new canshu;
		//canshu cs;
		cs->k=i;
		::AfxBeginThread(ThreadProc,cs);
	}
}

UINT CShortView::ThreadProc(LPVOID pPar)
{
	CSingleLock sl(&g_Sp);
	sl.Lock();

	canshu *p=(canshu*)pPar;
	int k=p->k;
	if(k>9||k<0)
		return 0;
	int tag=0;
	while(tag<10000)
	{
		tag++;
	
				for(int i=0;i<10;i++)
					for(int j=0;j<10;j++)
						if(R1[j].biao[i].distance+R1[k].biao[j].distance<R1[k].biao[i].distance)
						{
							R1[k].biao[i].distance=R1[j].biao[i].distance+R1[k].biao[j].distance;
							snull(R1[k].biao[i].path);
							scat2(R1[k].biao[i].path,R1[k].biao[j].path);
		    	if(R1[i].biao[j].distance==1)
							{
								if(i==0)
									scat1(R1[k].biao[i].path,10);
								else
									scat1(R1[k].biao[i].path,i);
							}
							else
								scat2(R1[k].biao[i].path,R1[j].biao[i].path);
						}
		}

return 0;

			  
  
} 		

⌨️ 快捷键说明

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