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

📄 repcombdlg.cpp

📁 穷举法进行三八二十四的计算。列举所有可能的表达式进行计算筛选。
💻 CPP
字号:
// RepCombDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RepComb.h"
#include "RepCombDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char CRepCombDlg::m_strOperaters[4]={'+','-','*','/'};
double CRepCombDlg::m_dblOperandTable[4]={3,3,8,8};
int CRepCombDlg::m_nScheme[8]={1,0,1,0,1,0,0};
char CRepCombDlg::m_cOperatorTable[3]={'/','-','/'};
CPrefixCalculator CRepCombDlg::m_PrefixCalculator;
int CRepCombDlg::m_a[4];
int CRepCombDlg::m_b[4];
int CRepCombDlg::m_comp[4];
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CRepCombDlg dialog

CRepCombDlg::CRepCombDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRepCombDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRepCombDlg)
	m_n1 = 10;
	m_n2 = 10;
	m_n3 = 4;
	m_n4 = 4;
	m_strInfo = _T("试试看,谁快?");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRepCombDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRepCombDlg)
	DDX_Control(pDX, IDC_KEY, m_listKey);
	DDX_Text(pDX, IDC_EDIT1, m_n1);
	DDV_MinMaxInt(pDX, m_n1, 1, 13);
	DDX_Text(pDX, IDC_EDIT2, m_n2);
	DDV_MinMaxInt(pDX, m_n2, 1, 13);
	DDX_Text(pDX, IDC_EDIT3, m_n3);
	DDV_MinMaxInt(pDX, m_n3, 1, 13);
	DDX_Text(pDX, IDC_EDIT4, m_n4);
	DDV_MinMaxInt(pDX, m_n4, 1, 13);
	DDX_Text(pDX, IDC_INFORMATION, m_strInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRepCombDlg, CDialog)
	//{{AFX_MSG_MAP(CRepCombDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRepCombDlg message handlers

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

void CRepCombDlg::OnOK() 
{
	// TODO: Add extra validation here
	CString strText;
	int n;
	UpdateData();
	GetWindowText(strText);
	SetWindowText(strText+"(请稍候,正在计算中…… )");
	m_listKey.ResetContent();
	UpdateData(false);
	Input();
	n=go();
	SetWindowText(strText);
	m_strInfo.Format("共有%d个解",n);
	UpdateData(false);
}

//功能:生成指定的前缀表达式,用正数表示操作数在操作数表中的位置
//用负数表示操作符在操作符表中的位置,0表示结束
//nLength前缀表达式的长度
//scheme前缀表达式模式
//op运算符下标的一个排列
//operand操作数下标的一个排列
void CRepCombDlg::ExpressionGenerator(int nLength,int scheme[],int op[],int operand[])
{
  if(nLength<=0) return ;
  int nIndex=0,nOpIndex=0,nOperandIndex=0;
  while(nIndex<nLength)
  {
   if(scheme[nIndex]==1)scheme[nIndex]=-(op[nOpIndex++]+1);
   else scheme[nIndex]=operand[nOperandIndex++]+1;
   nIndex++;
  }
  scheme[nIndex]=0;
}

void CRepCombDlg::Input()
{
  int  nIndex=0;
  m_count=0;
  for(nIndex=0;nIndex<4;nIndex++)
  {
	  m_a[nIndex]=0;
	  m_b[nIndex]=3;
  }
  m_dblOperandTable[0]=m_n1;
  m_dblOperandTable[1]=m_n2;
  m_dblOperandTable[2]=m_n3;
  m_dblOperandTable[3]=m_n4;
  for(int i=0;i<4;i++)
  {
	for(int j=0;j<m_count;j++)
	{
	 if((int)m_dblOperandTable[i]==m_comp[j])
	 {
		 m_a[j]++;
	     break;
	 }
	}
	if(j==m_count)
	{
	 m_comp[j]=(int)m_dblOperandTable[i];
	 m_a[m_count]++;
	 m_count++;
	}
  }
 }

int CRepCombDlg::go()
{
  int s[8],i; 
  double val;
  int n=0;
  double dblData[4];
  CString strExpression;
  for(i=0;i<4;i++)
	  dblData[i]=(double)m_comp[i];
  m_PrefixCalculator.initiate(7);
  m_ss.initiate(7);
  do
  {
	  m_ss.get(m_nScheme);
	  m_rcOperand.initiate(m_a,m_count,4);
	  do
	  { 
		  m_rcOperand.get(m_operand);
		  m_rcOperator.initiate(m_b,4,3);
		  do
		  {	
			  m_rcOperator.get(m_op);
			  for(i=0;i<7;i++)
				s[i]=m_nScheme[i];
              ExpressionGenerator(7,s,m_op,m_operand);
			  val=m_PrefixCalculator.Calculate(s,m_strOperaters,
				  dblData,strExpression);
			  if(fabs(val-24)<1e-6)
			  {
				  
				  strExpression+="=24";
				  m_listKey.AddString(strExpression);
				  n++;
			  }
		  
		  }while(m_rcOperator.next());
	  }while(m_rcOperand.next());
  }while(m_ss.next());
  return n;
}

⌨️ 快捷键说明

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