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

📄 汉诺塔dlg.cpp

📁 It s a C programming language cource from NUAA(Nanjing University of Aeronautics and Astronautics ),
💻 CPP
字号:
// 汉诺塔Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "汉诺塔.h"
#include "汉诺塔Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	number = 0;
	pn = 0;
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_R3, OnR3)
	ON_BN_CLICKED(IDC_R4, OnR4)
	ON_BN_CLICKED(IDC_R5, OnR5)
	ON_BN_CLICKED(IDC_R6, OnR6)
	ON_BN_CLICKED(IDC_R7, OnR7)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::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 CMyDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	if (IsIconic())
	{

		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();
	}
	// 显示
	ShowBg(&dc);
}

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

void CMyDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

/* 此函数为移动的递归函数,现已不用
// 将a最上面的n个盘子移动到b
void CMyDlg::MoveDish(int n, int a, int b) {

	if(1 == n){
		// 移动盘子
		int i = 1;
		for(; i <= number ; i++){
			if(a == dish[number - i]){
				break;
			}
		}
		if(i > number){
			return ;
		}
		dish[number - i] = b;
		Invalidate(FALSE); // 重绘
		return ; // 完成
	}
	else{
		MoveDish(n-1, a, (3-a-b));
		MoveDish(1, a, b);
		MoveDish(n-1, (3-a-b), b);
	}
}
*/

// 将a最上面的n个盘子移动到b,实际移动了盘子返回0,只是进栈返回1
int CMyDlg::MoveDish(int n, int a, int b) {

	if(1 == n){
		// 移动盘子
		int i = 1;
		for(; i <= number ; i++){
			if(a == dish[number - i]){
				break;
			}
		}
		if(i > number){
			return 0;
		}
		dish[number - i] = b;
		return 0; // 完成
	}
	else{
		// 将要进行的操作进栈
		opt[pn][0] = n-1;
		opt[pn][1] = 3-a-b;
		opt[pn][2] = b;
		pn ++;
		opt[pn][0] = 1;
		opt[pn][1] = a;
		opt[pn][2] = b;
		pn ++;
		opt[pn][0] = n-1;
		opt[pn][1] = a;
		opt[pn][2] = 3-a-b;
		pn ++;
		return 1;
	}
}

void CMyDlg::OnButton1() {

	// 开始演示
	if(0 == number){
		MessageBox("请选择盘子数!");
		return ;
	}
	SetDishNumber(number); // 数据重置
	MoveDish(number, 0, 2);
	SetTimer(1015, 400, NULL); // 开始计时,每400毫秒一步
}

void CMyDlg::OnR3() {

	// 设置三个盘子
	SetDishNumber(3);
}

void CMyDlg::OnR4() {

	// 设置四个盘子
	SetDishNumber(4);
}

void CMyDlg::OnR5() {
	// 设置五个盘子
	SetDishNumber(5);
}

void CMyDlg::OnR6() {
	// 设置六个盘子
	SetDishNumber(6);
}

void CMyDlg::OnR7() {
	// 设置七个盘子
	SetDishNumber(7);
}

// 设置盘子个数
void CMyDlg::SetDishNumber(int n){

	KillTimer(1015); // 停止计时
	pn = 0; // 清空栈
	number = n;
	for(int i = 0; i < n ; i++){
		dish[i] = 0; // 初始化每个盘子都在第一根柱子上
	}
	Invalidate(FALSE);//重绘
}

//显示
void CMyDlg::ShowBg(CDC * dc){

	//显示背景
	CDC pdc, ddc;
	pdc.CreateCompatibleDC(dc); // 创建一个临时显示设备
	ddc.CreateCompatibleDC(dc); // 创建一个加载盘子的临时显示设备

	CBitmap bmp, * obmp;
	bmp.LoadBitmap(IDB_BG); // 加载背景图片

	obmp = pdc.SelectObject(&bmp); // 将图片显示在设备pdc上。

	//显示盘子
	int n[] = {0, 0, 0}; // 用于存放每个柱子已显示多少盘子
	for(int i = 0; i < number ; i++){
		CBitmap dbmp, * odbmp;
		dbmp.LoadBitmap(IDB_B7 - i); // 加载从大到小第i个盘子图片
		odbmp = ddc.SelectObject(&dbmp); // 将盘子显示在设备ddc上。
		pdc.BitBlt(10 + 150*dish[i], 225 - n[dish[i]]*20, 140, 15, &ddc, 0, 0, SRCCOPY); // 将ddc拷贝到临时显示设备pdc对应位置上
		n[dish[i]] ++;
		ddc.SelectObject(odbmp); // 显示完毕,还原设备
	}

	dc->BitBlt(10, 10, 460, 260, &pdc, 0, 0, SRCCOPY); // 将pdc拷贝到程序显示设备dc上
	pdc.SelectObject(obmp); // 显示完毕,还原设备
}

void CMyDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(pn > 0){
		do{
			pn --; // 出栈
		}
		while(1 == MoveDish(opt[pn][0], opt[pn][1], opt[pn][2])); // 没有进行操作,则继续出栈
		Invalidate(FALSE); // 重绘
	}
	else{
		KillTimer(1015);
	}
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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