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

📄 train3view.cpp

📁 在学习堆栈的应用时
💻 CPP
字号:
// train3View.cpp : implementation of the CTrain3View class
//

#include "stdafx.h"
#include "train3.h"

#include "train3Doc.h"
#include "train3View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTrain3View

IMPLEMENT_DYNCREATE(CTrain3View, CView)

BEGIN_MESSAGE_MAP(CTrain3View, CView)
	//{{AFX_MSG_MAP(CTrain3View)
	ON_COMMAND(ID_1, On1)
	ON_COMMAND(ID_3, On3)
	ON_COMMAND(ID_2, On2)
	ON_COMMAND(ID_4, On4)
	ON_COMMAND(ID_5, On5)
	ON_COMMAND(ID_6, On6)
	ON_COMMAND(ID_7, On7)
	ON_COMMAND(ID_8, On8)
	ON_COMMAND(ID_9, On9)
	ON_COMMAND(ID_START, OnStart)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CTrain3View construction/destruction

CTrain3View::CTrain3View()
{
	// TODO: add construction code here
	index=1;
	k=9;
	n=9;
	count=new int[k];
	count2=new int[k];
	ta=new int[n+1];
	Q=new queue[k];
	jj=8;
	for(int i=1;i<k;i++)
		count[i]=0;
	for(i=1;i<k;i++)
		count2[i]=0;
}

CTrain3View::~CTrain3View()
{
	delete[]Q;
	delete[]ta;
	delete[]count;
	delete[]count2;
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTrain3View drawing

void CTrain3View::OnDraw(CDC* pDC)
{
	CTrain3Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	for(int i=0;i<10;i++)
	{
		pDC->MoveTo(100+30*i,180);
		pDC->LineTo(100+30*i,210);
	}
	pDC->MoveTo(100,180);
	pDC->LineTo(370,180);
	pDC->MoveTo(100,210);
	pDC->LineTo(370,210);
		
}

/////////////////////////////////////////////////////////////////////////////
// CTrain3View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTrain3View diagnostics

#ifdef _DEBUG
void CTrain3View::AssertValid() const
{
	CView::AssertValid();
}

void CTrain3View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CTrain3View message handlers

void CTrain3View::On1() 
{
	// TODO: Add your command handler code here
	ta[index]=1;
	CString s;
	s.Format("%d",1);
	CDC* pDC=GetDC();
	pDC->TextOut(110+jj*30,190,s);
	index++;
	jj--;
	
}

void CTrain3View::On3() 
{
	// TODO: Add your command handler code here
	ta[index]=3;
	CString s;
	s.Format("%d",3);
	CDC* pDC=GetDC();
	pDC->TextOut(110+jj*30,190,s);
	index++;
	jj--;
	
}

void CTrain3View::On2() 
{
	// TODO: Add your command handler code here
	ta[index]=2;
	CString s;
	s.Format("%d",2);
	CDC* pDC=GetDC();
	pDC->TextOut(110+jj*30,190,s);
	index++;
	jj--;
}

void CTrain3View::On4() 
{
	// TODO: Add your command handler code here
	ta[index]=4;
	CString s;
	s.Format("%d",4);
	CDC* pDC=GetDC();
	pDC->TextOut(110+jj*30,190,s);
	index++;
	jj--;
	
}

void CTrain3View::On5() 
{
	// TODO: Add your command handler code here
	ta[index]=5;
	CString s;
	s.Format("%d",5);
	CDC* pDC=GetDC();
	pDC->TextOut(110+jj*30,190,s);
	index++;
	jj--;
	
}

void CTrain3View::On6() 
{
	// TODO: Add your command handler code here
	ta[index]=6;
	CString s;
	s.Format("%d",6);
	CDC* pDC=GetDC();
	pDC->TextOut(110+jj*30,190,s);
	index++;
	jj--;
	
}

void CTrain3View::On7() 
{
	// TODO: Add your command handler code here
	ta[index]=7;
	CString s;
	s.Format("%d",7);
	CDC* pDC=GetDC();
	pDC->TextOut(110+jj*30,190,s);
	index++;
	jj--;
	
}

void CTrain3View::On8() 
{
	// TODO: Add your command handler code here
	ta[index]=8;
	CString s;
	s.Format("%d",8);
	CDC* pDC=GetDC();
	pDC->TextOut(110+jj*30,190,s);
	index++;
	jj--;
	
}

void CTrain3View::On9() 
{
	// TODO: Add your command handler code here
	ta[index]=9;
	CString s;
	s.Format("%d",9);
	CDC* pDC=GetDC();
	pDC->TextOut(110+jj*30,190,s);
	index++;
	jj--;
	
}
bool CTrain3View::Railroad()
{
	CDC* pDC=GetDC();
	CString s;
	int m=0,tt=8;
	minH=n+2;
	int NowOut=1;
	for(int i=1;i<10;i++)
	{
		if(ta[i]==NowOut)
		{
			pDC->TextOut(110+30*tt,190,"  ");
			tt--;
			Sleep(800);
			s.Format("%d",ta[i]);
			pDC->TextOut(800-20*m,200,s);
			Sleep(800);
			m++;
			NowOut++;
			while(NowOut==minH)
			{
				pDC->TextOut(600-(count2[minQ]+1)*20,40+(minQ-1)*20,"  ");
				count2[minQ]++;
				Sleep(800);
				s.Format("%d",minH);
				pDC->TextOut(800-20*m,200,s);
				Sleep(800);
				m++;
				Output();
				NowOut++;
			}
		}
		else{
			if(!Hold(ta[i],tt))
				return false;
		}
	}
	return true;
}
void CTrain3View::Output()
{
	int x;
	Q[minQ].Delete(x);
	minH=n+2;
	for(int i=1;i<k;i++)
	{
		if(!Q[i].IsEmpty() && Q[i].get_first()<minH)
		{
			minH=Q[i].get_first();
			minQ=i;
		}
	}
}
bool CTrain3View::Hold(int c,int& tt)
{
	int BestTrack=0;
	int BestLast=0;
	int x;
	for(int i=1;i<k;i++)
	{
		if(!Q[i].IsEmpty())
		{
			x=Q[i].get_last();
			if(c>x && x>BestLast)
			{
				BestLast=x;
				BestTrack=i;
			}
		}
		else//为空轨的情况
			if(!BestTrack) BestTrack=i;
	}
	if(!BestTrack) return false;
	Q[BestTrack].add(c);
	count[BestTrack]++;
	CString s;
	CDC* pDC=GetDC();
	pDC->TextOut(110+30*tt,190,"  ");
	Sleep(800);
	tt--;
	s.Format("%d",c);
	pDC->TextOut(600-count[BestTrack]*20,40+(BestTrack-1)*20,s);
	Sleep(800);
		//移到定义的轨道的地方
	if(c<minH)
	{
		minH=c;
		minQ=BestTrack;
	}
	return true;
}

void CTrain3View::OnStart() 
{
	// TODO: Add your command handler code here
	Railroad();
	
}

⌨️ 快捷键说明

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