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

📄 24dlg.cpp

📁 24游戏
💻 CPP
字号:
// 24Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "24.h"
#include "24Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMy24Dlg dialog

CMy24Dlg::CMy24Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy24Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy24Dlg)
	m_int1 = 0;
	m_int2 = 0;
	m_int3 = 0;
	m_int4 = 0;
	m_String = _T("");
	m_bool = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy24Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy24Dlg)
	DDX_Control(pDX, IDC_EDIT6, m_Edit6);
	DDX_Control(pDX, IDC_EDIT5, m_Edit5);
	DDX_Control(pDX, IDC_EDIT4, m_Edit4);
	DDX_Control(pDX, IDC_EDIT3, m_Edit3);
	DDX_Control(pDX, IDC_EDIT2, m_Edit2);
	DDX_Control(pDX, IDC_EDIT1, m_Edit1);
	DDX_Control(pDX, IDC_EXIT_BUTTON, m_Exit);
	DDX_Control(pDX, IDC_CALCULATE_BUTTON, m_Calculate);
	DDX_Control(pDX, IDC_ABOUT_BUTTON, m_About);
	DDX_Text(pDX, IDC_EDIT1, m_int1);
	DDX_Text(pDX, IDC_EDIT2, m_int2);
	DDX_Text(pDX, IDC_EDIT3, m_int3);
	DDX_Text(pDX, IDC_EDIT4, m_int4);
	DDX_Text(pDX, IDC_EDIT5, m_String);
	DDX_Text(pDX, IDC_EDIT6, m_bool);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy24Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy24Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
	ON_BN_CLICKED(IDC_ABOUT_BUTTON, OnAboutButton)
	ON_BN_CLICKED(IDC_CALCULATE_BUTTON, OnCalculateButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy24Dlg message handlers

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

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

void CMy24Dlg::OnAboutButton() 
{
	// TODO: Add your control notification handler code here
	MessageBox("This software help you find how to form 24 by given integers.\nThose who first find out mistakes of this software will be reward.\nEnjoy it!");
}

void CMy24Dlg::OnCalculateButton() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	int aiCardArrange[24][4]= 
	{ 
	{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1}, 
	{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0}, 
	{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0}, 
	{3,0,1,2},{3,0,2,1},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0} 
	}; 
	int aiCard[4]; 
	int aiAnswer[4]; 
	int aiInput[4]; 
	int aiOperator[3];
	int i,j,k,iSoluted,iNumber; 
	int dAnswer; 
	char achOperator[4]={'+','-','*','/'}; 
	aiInput[0] = m_int1;
	aiInput[1] = m_int2;
	aiInput[2] = m_int3;
	aiInput[3] = m_int4;
	iSoluted = 0;
	iNumber = -1;
	
	for(i=0;i<24;i++) 
	{	
		for(k=0;k<64;k++) 
		{ 
			for(j=0;j<4;j++) 
				aiCard[j]=aiInput[aiCardArrange[i][j]]; 
			aiOperator[0]=k%4; 
			aiOperator[1]=(k/4)%4; 
			aiOperator[2]=(k/4/4)%4; 
			dAnswer= Compute ( Compute ( Compute ( aiCard[0], aiCard[1], aiOperator[0] ), aiCard[2], aiOperator[1] ), aiCard[3], aiOperator[2] ); 
			if(dAnswer == 24)
			{
				for(j=0;j<4;j++) 
					aiAnswer[j]=aiInput[aiCardArrange[i][j]];
				iSoluted = 1;
				iNumber = 1;
				break;
			}
			dAnswer = Compute(Compute(aiCard[0],aiCard[1],aiOperator[0]),Compute(aiCard[2],aiCard[3],aiOperator[2]),aiOperator[1]);
			if(dAnswer == 24)
			{
				for(j=0;j<4;j++) 
					aiAnswer[j]=aiInput[aiCardArrange[i][j]];
				iSoluted = 1;
				iNumber = 2;
				break;
			}
			dAnswer = Compute(Compute(aiCard[0], Compute(aiCard[1],aiCard[2],aiOperator[1]),aiOperator[0]),aiCard[3],aiOperator[2]);
            if(dAnswer == 24)
			{
				for(j=0;j<4;j++) 
					aiAnswer[j]=aiInput[aiCardArrange[i][j]];
				iSoluted = 1;
				iNumber = 3;
				break;
			}
//			dAnswer = Compute((double)aiCard[0],Compute(Compute((double)aiCard[1],(double)aiCard[2],aiOperator[1]),(double)aiCard[3],aiOperator[2])),aiOperator[0]);
			int c = Compute(aiCard[1],aiCard[2],aiOperator[1]);
			int d = Compute(c,aiCard[3],aiOperator[2]);
			dAnswer = Compute(aiCard[0],d,aiOperator[0]);
			if(dAnswer == 24)
			{
				for(j=0;j<4;j++) 
					aiAnswer[j]=aiInput[aiCardArrange[i][j]];
				iSoluted = 1;
				iNumber = 4;
				break;
			}
//			dAnswer = Compute((double)aiCard[0],Compute((double)aiCard[1],Compute((double)aiCard[2],(double)aiCard[3],aiOperator[2]),aiOperator[1])),aiOperator[0]);
			int a = Compute(aiCard[2],aiCard[3],aiOperator[2]);
			int b = Compute(aiCard[1],a,aiOperator[1]);
			dAnswer = Compute(aiCard[0],b,aiOperator[0]);
			if(dAnswer == 24)
			{
				for(j=0;j<4;j++) 
					aiAnswer[j]=aiInput[aiCardArrange[i][j]];
				iSoluted = 1;
				iNumber = 5;
				break;
			}

		} 
		if(iSoluted==1) 
			break; 
	}

	if(iSoluted == 1)
	{
		m_bool = "Yes";
		int s, t = 0;
		char ch1 = '(';
		char ch2 = ')';
		char ch[10];
		char str[100];
//		char sign[4][2] = {"+","-","*","/"};
		switch(iNumber)
		{
		case 1:
			{
//				((a#b)#c)#d;
				str[t++] = ch1;
				str[t++] = ch1;
				itoa(aiAnswer[0],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = achOperator[aiOperator[0]];
				itoa(aiAnswer[1],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = ch2;
				str[t++] = achOperator[aiOperator[1]];
				itoa(aiAnswer[2],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = ch2;
				str[t++] = achOperator[aiOperator[2]];
				itoa(aiAnswer[3],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t] = '\0';

				m_String = str;
				break;
			}
		case 2:
			{
				//(a#b)#(c#d)
				str[t++] = ch1;
				itoa(aiAnswer[0],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = achOperator[aiOperator[0]];
				itoa(aiAnswer[1],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = ch2;
				str[t++] = achOperator[aiOperator[1]];
				str[t++] = ch1;
				itoa(aiAnswer[2],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = achOperator[aiOperator[2]];
				itoa(aiAnswer[3],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = ch2;
				str[t] = '\0';

				m_String = str;
				break;
			}
		case 3:
			{
				//(a#(b#c))#d
				str[t++] = ch1;
				itoa(aiAnswer[0],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = achOperator[aiOperator[0]];
				str[t++] = ch1;
				itoa(aiAnswer[1],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = achOperator[aiOperator[1]];
				itoa(aiAnswer[2],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = ch2;
				str[t++] = ch2;
				str[t++] = achOperator[aiOperator[2]];
				itoa(aiAnswer[3],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t] = '\0';

				m_String = str;
				break;
			}
		case 4:
			{
				//a#((b#c)#d)
				itoa(aiAnswer[0],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = achOperator[aiOperator[0]];
				str[t++] = ch1;
				str[t++] = ch1;
				itoa(aiAnswer[1],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = achOperator[aiOperator[1]];
				itoa(aiAnswer[2],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = ch2;
				str[t++] = achOperator[aiOperator[2]];
				itoa(aiAnswer[3],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = ch2;
				str[t] = '\0';

				m_String = str;
				break;
			}
		case 5:
			{
				//a#b(#(c#d))
				itoa(aiAnswer[0],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = achOperator[aiOperator[0]];
				itoa(aiAnswer[1],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = ch1;
				str[t++] = achOperator[aiOperator[1]];
				str[t++] = ch1;
				itoa(aiAnswer[2],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = achOperator[aiOperator[2]];
				itoa(aiAnswer[3],ch,10);
				for(s = 0; s < strlen(ch) ; s++)
					str[t++] = ch[s];
				str[t++] = ch2;
				str[t++] = ch2;
				str[t] = '\0';

				m_String = str;
				break;
			}
		}
	}
	else
	{
		m_bool = "No";
		m_String = "No Solution!";
	}
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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