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

📄 airportdepview.cpp

📁 使飞机进离港航班的时间在一时间段内达到最小.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AirportDepView.cpp : implementation of the CAirportDepView class
//

#include "stdafx.h"
#include "AirportDep.h"

#include "AirportDepDoc.h"
#include "AirportDepView.h"

#include "MainFrm.h"
#include "Glbs.h"  //包含全局变量

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

/////////////////////////////////////////////////////////////////////////////
// CAirportDepView

IMPLEMENT_DYNCREATE(CAirportDepView, CScrollView)

BEGIN_MESSAGE_MAP(CAirportDepView, CScrollView)
	//{{AFX_MSG_MAP(CAirportDepView)
	ON_COMMAND(IDM_AIRPORTDEP, OnAirportdep)
	ON_COMMAND(IDM_GA, OnGA)
	ON_COMMAND(IDM_AIRPORTARR, OnAirportArr)
	ON_COMMAND(IDM_CIA, OnCIA)
	ON_COMMAND(IDM_FCFS, OnFCFS)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAirportDepView construction/destruction

CAirportDepView::CAirportDepView()
{
	// TODO: add construction code here
    m_ptOrigin=0;  //光标位置初始化
	population=200;
	CROSS_FACTOR=30;
	MUTE_FACTOR=20;
	airport.erase(airport.begin(),airport.end());
}

CAirportDepView::~CAirportDepView()
{
}

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

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAirportDepView drawing

void CAirportDepView::OnDraw(CDC* pDC)
{
	CAirportDepDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	int y,n;
	CPoint pt=0;
    TEXTMETRIC tm;
	
	pDC->GetTextMetrics(&tm);	
    y=tm.tmHeight;
	
	for(int i=0;i<m_strArray.GetSize();i++) //当窗口滚动时数据位置保持不变
	{	
		pDC->TextOut(pt.x,pt.y,m_strArray.GetAt(i));
		pt.y+=y;
		n=3*i;
		for(int k=n;k<(n+3);k++)	
		{
			pDC->TextOut(pt.x,pt.y,m_strPath.GetAt(k));
			pt.x+=2*tm.tmMaxCharWidth+tm.tmAveCharWidth;
			if(((k+1)%3)!=0)
			{
				pDC->TextOut(pt.x,pt.y,',');
				pt.x+=tm.tmAveCharWidth;
			}
		}
		pt.y+=y; 
		pt.x=0;

	} 
}


void CAirportDepView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = 600;
	sizeTotal.cy = 8000;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CAirportDepView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CAirportDepView diagnostics

#ifdef _DEBUG
void CAirportDepView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CAirportDepView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CAirportDepView message handlers

void CAirportDepView::OnAirportdep()    //生成机场离港航班信息
{
	// TODO: Add your command handler code here
    char f_num[10];		
	char f_time[10];
	Flight ff;
	FILE *in;

	if(!Flight_Dep.empty())
	{
		flight_DepNumber=0;
		Flight_Dep.erase(Flight_Dep.begin(),Flight_Dep.end());  //清空容器		
	}
	if((in=fopen("Dep100.txt","r"))==NULL)       
	{  
		MessageBox("不能读取文件信息!");
		return;
	}
	else
	{
		do
		{			
			fscanf(in,"%s",&f_time);
			fscanf(in,"%s",&f_num);

			ff.Set_Flight_PlanTime(f_time);
			ff.Set_Flight_Num(f_num);
			ff.Set_Flight_Plan();
			ff.Set_Flight_DelayTime();
           
			Flight_Dep.push_back(ff);          //生成总的离港航班信息		  
					
			flight_DepNumber++;           //生成总的离港航班数
		}while(!feof(in));
	}
	fclose(in);	

    CString str;
	str.Format("Flight_DepNumber:%d",flight_DepNumber);
	//str.Format("DepFlight[0].flightnumber:%s",Flight_Dep[26].Get_Flight_Num());
	//str.Format("DepFlight[0].flightnumber:%d",DepFlight[23].Get_Flight_DepPlan());
		
	((CMainFrame *)GetParent())->GetMessageBar()->SetWindowText(str); //第二种方法    
}


void CAirportDepView::Generate_Airport()   //初始化所有代的机场种群
{  	
	Airport air;

	airport.erase(airport.begin(),airport.end());

	for(int i=0;i<population;i++)
	{
		air.SetFlightArr(true);
	    air.SetFlightDep(true);
		air.InitialAirport(true);

		air.SetAirport_ArrFlight();
	    air.SetAirport_DepFlight();
		air.CountDelay_Arr();
		air.CountDelay_Dep();
		air.CountDelay();
		//air.CountDelayFlight_Arr();
		//air.CountDelayFlight_Dep();
		//air.CountDelayFlight();
		air.SetOptDone(false); 

		airport.push_back(air);    
	}
	sort();
}  

void CAirportDepView::OnGA() 
{
	// TODO: Add your command handler code here
    m_ptOrigin=0;
    CClientDC dc(this);	
	ReleaseDC(&dc); //使窗口重绘
    
	m_strArray.RemoveAll(); //清空数组

	Generate_Airport();
	
	for(int m=0;m<10;m++)
		doOneGenerationModified();
	sort();
    for(int i=0;i<10;i++)
		OutAirport(airport[i]);    
	
}

void CAirportDepView::OutAirport(Airport air)    //输出机场信息
{
	CClientDC dc(this);	
	char buffer[10];	
	int delay,num1;
	CPoint point=m_ptOrigin;
	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);	
	
	/*delay=air.GetDelayTime();	
    
	itoa(delay,buffer,10);
	
	//窗口滚动时,图形的位置保持不变
    OnPrepareDC(&dc);

	m_strArray.Add(buffer); 
	dc.TextOut(point.x,point.y,buffer);  //输出延误时间段数  */

	for(int t=0;t<3;t++)
	{
		if(t==0)
			delay=air.GetDelayTime_Arr();
		if(t==1)
			delay=air.GetDelayTime_Dep();
		if(t==2)
			delay=air.GetDelayTime();

		itoa(delay,buffer,10);
		m_strArray.Add(buffer); 
	    dc.TextOut(point.x,point.y,buffer);
		point.x+=2*tm.tmMaxCharWidth+tm.tmAveCharWidth;
		if(((t+1)%3)!=0)
		{
			dc.TextOut(point.x,point.y,',');
			point.x+=tm.tmAveCharWidth;
		}
	}
	point.y+=tm.tmHeight; 
	point.x=0;

	//以下代码使光标随着输出移动	
   /* TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);	
	point.y+=tm.tmHeight;  */

	for(int k=0;k<3;k++)  //输出延误航班数
	{		
		if(k==0)
			delay=air.GetDelayFlight_Arr();
		if(k==1)
			delay=air.GetDelayFlight_Dep();
		if(k==2)
			delay=air.GetDelayFlight();

		itoa(delay,buffer,10);
		m_strPath.Add(buffer);
		dc.TextOut(point.x,point.y,buffer);
		point.x+=2*tm.tmMaxCharWidth+tm.tmAveCharWidth;
		if(((k+1)%3)!=0)
		{
			dc.TextOut(point.x,point.y,',');
			point.x+=tm.tmAveCharWidth;
		}
	}
	point.x=0;
    point.y+=tm.tmHeight;    
	m_ptOrigin.y=point.y;
	for(k=0;k<T;k++)   //输出各个时间段的到达容量
	{
		num1=air.GetAirport_Arr(k);
		itoa(num1,buffer,10);
		m_strPath.Add(buffer);
		dc.TextOut(point.x,point.y,buffer);
		point.x+=tm.tmMaxCharWidth+tm.tmAveCharWidth;
        if(((k+1)%T)!=0)
		{
			dc.TextOut(point.x,point.y,',');
			point.x+=tm.tmAveCharWidth;
		}
	}
	point.x=0;
	point.y+=tm.tmHeight;    
	m_ptOrigin.y=point.y;

	for(k=0;k<T;k++)   //输出各个时间段的出发容量
	{
		num1=air.GetAirport_Dep(k);
		itoa(num1,buffer,10);
		m_strPath.Add(buffer);
		dc.TextOut(point.x,point.y,buffer);
		point.x+=tm.tmMaxCharWidth+tm.tmAveCharWidth;
        if(((k+1)%T)!=0)
		{
			dc.TextOut(point.x,point.y,',');
			point.x+=tm.tmAveCharWidth;
		}
	}
	point.y+=tm.tmHeight;    
	m_ptOrigin.y=point.y;
} 


void CAirportDepView::OnAirportArr()   //生成机场到港航班信息
{
	// TODO: Add your command handler code here
	char f_num[10];		
	char f_time[10];
	Flight ff;
	FILE *in;

	if(!Flight_Arr.empty())
	{
		flight_ArrNumber=0;
		Flight_Arr.erase(Flight_Arr.begin(),Flight_Arr.end());  //清空容器		
	}
	if((in=fopen("Arr100.txt","r"))==NULL)       
	{  
		MessageBox("不能读取文件信息!");
		return;
	}
	else
	{
		do
		{			
			fscanf(in,"%s",&f_time);
			fscanf(in,"%s",&f_num);

			ff.Set_Flight_PlanTime(f_time);
			ff.Set_Flight_Num(f_num);
			ff.Set_Flight_Plan();
			ff.Set_Flight_DelayTime();
           
			Flight_Arr.push_back(ff);          //生成总的进港航班信息		  
					
			flight_ArrNumber++;           //生成总的进港航班数
		}while(!feof(in));
	}
	fclose(in);	

    CString str;
	str.Format("Flight_ArrNumber:%d",flight_ArrNumber);
	//str.Format("DepFlight[0].flightnumber:%s",DepFlight[26].Get_Flight_PlanTime());
	//str.Format("DepFlight[0].flightnumber:%d",DepFlight[23].Get_Flight_DepPlan());
		
	((CMainFrame *)GetParent())->GetMessageBar()->SetWindowText(str); //第二种方法	
}

void CAirportDepView::sort()  //由小到大排序
{
	Airport temp;
	 	 
	for(int i=0;i<=(population-2);i++)
		for(int j=i+1;j<=(population-1);j++)
		{
			if(airport[i].GetDelayTime()>airport[j].GetDelayTime())
			{
				 temp=airport[i];
				 airport[i]=airport[j];
				 airport[j]=temp;
			}
		}
}

void CAirportDepView::eliminate(int toCross, int toMute)   //保证种群中的相邻代机场航班延误时间段数不同
{
	int t=airport[0].GetDelayTime();

	for(int i = 1; i < population-toMute-toCross; i++)
	{
        if(airport[i].GetDelayTime()==t)
		{			
			airport[i].SetFlightArr(true);
			airport[i].SetFlightDep(true);
			airport[i].InitialAirport(true);			
			airport[i].SetAirport_ArrFlight();
			airport[i].SetAirport_DepFlight();
			airport[i].CountDelay_Arr();
			airport[i].CountDelay_Dep();
			airport[i].CountDelay();
			//airport[i].CountDelayFlight_Arr();
			//airport[i].CountDelayFlight_Dep();
			//airport[i].CountDelayFlight();
			airport[i].SetOptDone(false);						
		}
		else			
			t=airport[i].GetDelayTime();
	}

⌨️ 快捷键说明

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