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

📄 hanoi.cpp

📁 汉诺塔的VC演示程序
💻 CPP
字号:
// Hanoi.cpp : Defines the entry point for the application.

#include "stdafx.h"
#include <windows.h>
#include "Message.h"
#include "resource.h"
#include "Brick.h"
#include "List.h"

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

HINSTANCE hInst;	//程序实例句柄
HWND hMainWnd;		//主窗口句柄

CList x, y, z;//Hanoi Tower的3根柱子

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

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	MSG msg;

	if(!InitApplication(hInstance))
		return FALSE;

	if(!InitInstance(hInstance, nCmdShow))
		return FALSE;

	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

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

BOOL InitApplication(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbClsExtra = 0;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.cbWndExtra = 0;
	wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_HANOI));
	wcex.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_HANOI));
	wcex.hInstance = hInstance;
	wcex.lpfnWndProc = (WNDPROC)MainWndProc;
	wcex.lpszClassName = szMainWndClass;
	wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
	wcex.style = NULL;

	hBmp1  = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B01));
	hBmp2  = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B02));
	hBmp3  = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B03));
	hBmp4  = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B04));
	hBmp5  = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B05));
	hBmp6  = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B06));
	hBmp7  = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B07));
	hBmp8  = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B08));
	hBmp9  = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B09));
	hBmp10 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B10));
	hBmp11 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B11));
	hBmp12 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B12));
	hBmp13 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B13));
	hBmp14 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B14));
	hBmp15 = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_B15));
	
	return RegisterClassEx(&wcex);
}

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

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	hInst = hInstance;

	hMainWnd = CreateWindowEx(0l,
							  szMainWndClass,
							  szTitle,
							  WS_OVERLAPPEDWINDOW,
							  CW_USEDEFAULT,
							  CW_USEDEFAULT,
							  CW_USEDEFAULT,
							  CW_USEDEFAULT,
							  NULL,
							  NULL,
							  hInstance,
							  NULL);

	if(!hMainWnd)
		return FALSE;

	ShowWindow(hMainWnd, SW_SHOWMAXIMIZED);
	UpdateWindow(hMainWnd);

	return TRUE;
}

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

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam , LPARAM lParam)
{
	int i;

	for(i = 0; i<dim(_messageEntry); i++)
	{
		if(message == _messageEntry[i].nMessage)
			return ((*_messageEntry[i].pfn)(hWnd, message, wParam, lParam));
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}

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

void move(CList* a, int n, CList* b, HWND hWnd)
{
	b->AddBrick(a->GetListTail());
	a->DelBrick();
	::OnPaint(hWnd, 0, 0, 0);
	::InvalidateRect(hWnd,NULL, 1);
}

void hanoi(int n, CList* pList_x, CList* pList_y, CList* pList_z, HWND hWnd)
{
	if(n==1)
		move(pList_x, 1, pList_z, hWnd);
	else
	{
		hanoi(n-1, pList_x, pList_z, pList_y, hWnd);
		move(pList_x, n, pList_z, hWnd);
		hanoi(n-1, pList_y, pList_x, pList_z, hWnd);
	}
}

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

⌨️ 快捷键说明

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