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

📄 magicsquaredlg.cpp

📁 当年
💻 CPP
字号:
// MagicSquareDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MagicSquare.h"
#include "MagicSquareDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMagicSquareDlg dialog

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

void CMagicSquareDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMagicSquareDlg)
	DDX_Text(pDX, IDC_EDIT_N, m_nN);
	DDV_MinMaxInt(pDX, m_nN, 3, 100);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMagicSquareDlg, CDialog)
	//{{AFX_MSG_MAP(CMagicSquareDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CALC, OnButtonCalc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMagicSquareDlg message handlers

BOOL CMagicSquareDlg::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
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMagicSquareDlg::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 CMagicSquareDlg::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 CMagicSquareDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMagicSquareDlg::OnButtonCalc() 
{
	// TODO: Add your control notification handler code here
	UpdateData();

	MagicSquare(m_nN);
}

int CMagicSquareDlg::MagicSquare(int n)
{
	int n2 = n * n;
	int *N = new int[n2];

	int *Used = new int[n2+1];

	ZeroMemory(N   , sizeof(int) * n2  );
	ZeroMemory(Used, sizeof(int) * (n2+1));

	CStdioFile File;
	BOOL bLog = File.Open("e:\\MagicSquare.Txt", CFile::modeCreate|CFile::modeWrite);

	CWaitCursor WaitCursor;

	int i = 0, nTotal = 0;

	while ( i >= 0/*N[0] <= n2*/ )
	{
		// 清除上一个标志
		//if ( N[i] > 0 )
			Used[N[i]] = 0;

		// 获得下一个未使用数
		N[i] = GetNextNoneUsed(Used, N[i], n2+1);

		// 已经没有未使用数,则回溯
		if ( N[i] == 0 )
		{
			// 清除标志,回溯
			//Used[N[i]] = 0;
			i --;
			continue;
		}

		// 置使用标志
		Used[N[i]] = 1;

		// 检查是否满足条件
		if ( !CheckMagicSum(N, i, n) )
		{
			// 在大于的数都用完的情况下回溯
			if ( N[i] >= n2 )
			{
				// 清除标志,回溯
				Used[N[i]] = 0;
				N[i] = 0;
				i --;
			}
			continue;
		}

		// 是否到达最后点
		if ( i == n2 - 1 )
		{
			nTotal ++;

			if ( bLog )
			{
				CString strMsg;
				// 满足条件,输出
				File.WriteString("// 满足条件,输出:\n");
				for ( int j = 0; j < n2; j ++ )
				{
					strMsg.Format("%2d ", N[j]);
					File.WriteString(strMsg);
					if ( j % n == n - 1 )
						File.WriteString("\n");
				}
			}
			
			if ( n >= 5 )  // 不列举所有情况
				break;

			Used[N[i]] = 0;
			i --;
		}
		else
		{
			i ++;
		}
	}


	CString strMsg;
	strMsg.Format("// 满足条件,共输出%d组\n", nTotal);

	File.WriteString(strMsg);
	File.Close();

	AfxMessageBox(strMsg);

	return nTotal;
}

int CMagicSquareDlg::GetNextNoneUsed(int *pUsed, int i, int nSize)
{

	// 从当前使用数组中查找未使用的数
	for ( int j = i + 1; j < nSize; j ++ )
	{
		if ( pUsed[j] == 0 )
			return (j);
	}

	return 0;
}

// 检查行列和是否满足条件
BOOL CMagicSquareDlg::CheckMagicSum(int *pN, int i, int nSize)
{
	int j;
	int n2 = nSize * nSize;
	int nSums = nSize*(n2+1)/2;
	int nRows = (i + 1) / nSize;

	// 检查行和
	for ( j = 0; j < nRows; j ++ )
	{
		int nSum = 0, nBase = j*nSize;
		for ( int k = 0; k < nSize; k ++ )
		{
			nSum += pN[nBase + k];
		}
		if ( nSum != nSums )
			return FALSE;
	}

	if ( i < n2 - nSize  )	// nSize * (nSize -1)
		return TRUE;

	// 检查列和
	int nCols = i % 3; 
	for ( j = 0; j <= nCols; j ++ )
	{
		int nSum = 0;
		for ( int k = 0; k < nSize; k ++ )
		{
			nSum += pN[k*nSize + j];
		}
		if ( nSum != nSums )
			return FALSE;
	}

	// 检查对角线和
	if ( i == n2 - nSize )
	{
		int nSum = 0;
		for ( j = 0; j < nSize; j ++ )
		{
			nSum += pN[(j + 1) * nSize - j - 1];
		}
		if ( nSum != nSums )
			return FALSE;
	}

	// 检查反对角线和
	if ( i == n2 - 1 )
	{
		int nSum = 0;
		for ( j = 0; j < nSize; j ++ )
		{
			nSum += pN[j * nSize + j];
		}
		if ( nSum != nSums )
			return FALSE;
	}

	return TRUE;
}

⌨️ 快捷键说明

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