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

📄 hanluotadlg.cpp

📁 一个汉诺塔示例程序
💻 CPP
字号:
// hanluotaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "hanluota.h"
#include "hanluotaDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define WM_THREADFINISHED WM_USER + 100
UINT ComputeThreadProc( LPVOID pParam );
struct struct_Node
{
   int object;
   char taget;
   char source;
};

DWORD gCurTimes;

int gTiers;


struct_Node gOutInfo[20000000];

struct_Tag_Node gA[40];
struct_Tag_Node gB[40];
struct_Tag_Node gC[40];

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
	
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHanluotaDlg dialog

CHanluotaDlg::CHanluotaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHanluotaDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHanluotaDlg)
	m_tiers = 0;
	m_times = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHanluotaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHanluotaDlg)
	DDX_Control(pDX, IDC_LIST_OUT, m_list_out);
	DDX_Text(pDX, IDC_EDIT_TIER, m_tiers);
	DDV_MinMaxInt(pDX, m_tiers, 1, 24);
	DDX_Text(pDX, IDC_EDIT_TIMES, m_times);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHanluotaDlg, CDialog)
	//{{AFX_MSG_MAP(CHanluotaDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONFIRM, OnConfirm)
	ON_WM_TCARD()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_THREADFINISHED, OnFinishedThread)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHanluotaDlg message handlers

BOOL CHanluotaDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_list_out.InsertColumn( 0, "源", LVCFMT_LEFT, 70 );
	m_list_out.InsertColumn( 1, "目标", LVCFMT_LEFT, 70 );
	m_list_out.InsertColumn( 2, "移动对象", LVCFMT_LEFT, 80 );
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CHanluotaDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CHanluotaDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CHanluotaDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CHanluotaDlg::OnConfirm() 
{
	// TODO: Add your control notification handler code here
	UpdateData( );
	//struct_Node node[10000];
	if( m_tiers >24 )
		return;
	m_list_out.DeleteAllItems();
	gCurTimes = 0;
    gTiers = m_tiers;
	for( int i=0;i<40;i++ )
	{
		gA[i].cTag = 'A';
		gB[i].cTag = 'B';
		gC[i].cTag = 'C';

		gA[i].nValude = 0;
		gB[i].nValude = 0;
		gC[i].nValude = 0;

	}

	for( i=m_tiers-1;i>=0;i-- )
	{
		gA[i].nValude = m_tiers-i;
	}


	AfxBeginThread(ComputeThreadProc, GetSafeHwnd(), THREAD_PRIORITY_NORMAL );
	//moveTo( gA, gC, m_tiers,gB );

    char tmpch[100];
    for( i = 0; i<gCurTimes && i<1000;i++)
	{
	  strcpy( tmpch, "3" );
      tmpch[0] = gOutInfo[i].source;
      m_list_out.InsertItem( i, tmpch );
	  tmpch[0] = gOutInfo[i].taget;
	  m_list_out.SetItemText( i,1, tmpch );
	  m_list_out.SetItemText( i,2, itoa(gOutInfo[i].object,tmpch,10) );
	}
	
    m_times = gCurTimes;

	UpdateData( FALSE );
}

short CHanluotaDlg::moveTo( struct_Tag_Node *A, struct_Tag_Node *C, int nTiers,struct_Tag_Node *B )
{
  if( nTiers == 1)
  {
	 int nFindPos1 = 0;
	 int nFindPos2 = 0;
	 for( int i = 0;i<40;i++)
	 {
		 if( A[i].nValude == 0 )
		 {
			 nFindPos1 = i-1;
			 break;
		 }
	 }

	 for( i = 0;i<40;i++)
	 {
		 if( C[i].nValude == 0 )
		 {
			 nFindPos2 = i;
			 break;
		 }
	 }

	 C[nFindPos2].nValude = A[nFindPos1].nValude;
	 A[nFindPos1].nValude = 0;

	 gOutInfo[gCurTimes].object = C[nFindPos2].nValude;
     gOutInfo[gCurTimes].source = A[nFindPos1].cTag;
     gOutInfo[gCurTimes].taget = C[nFindPos1].cTag;
     gCurTimes++;
	 return TRUE;
  }
  else
  {
	  moveTo( A, B, nTiers-1, C );
	  moveTo( A, C, 1, B );
	  moveTo( B, C, nTiers-1, A );
  }
  return TRUE;
}

UINT ComputeThreadProc( LPVOID pParam )
{
	CHanluotaDlg aa;
	aa.moveTo( gA, gC, 22,gB );
    ::PostMessage( (HWND)pParam, WM_THREADFINISHED, 0, 0);
	return TRUE;
}

void CHanluotaDlg::OnTCard(UINT idAction, DWORD dwActionData) 
{
	// TODO: Add your message handler code here
	
}

long CHanluotaDlg::OnFinishedThread(WPARAM wParam, LPARAM lParam)
{
  char tmpch[100];
    for( int i = 0; i<gCurTimes && i<1000;i++)
	{
	  strcpy( tmpch, "3" );
      tmpch[0] = gOutInfo[i].source;
      m_list_out.InsertItem( i, tmpch );
	  tmpch[0] = gOutInfo[i].taget;
	  m_list_out.SetItemText( i,1, tmpch );
	  m_list_out.SetItemText( i,2, itoa(gOutInfo[i].object,tmpch,10) );
	}
	
    m_times = gCurTimes;
	return TRUE;
}

⌨️ 快捷键说明

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