📄 1724dlg.cpp
字号:
// 1724Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "1724.h"
#include "1724Dlg.h"
#include "time.h"
#include <cstring>
#include <cmath>
#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()
/////////////////////////////////////////////////////////////////////////////
// CMy1724Dlg dialog
CMy1724Dlg::CMy1724Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy1724Dlg::IDD, pParent),PRECISION(1E-6)
{
numm = 0;
//{{AFX_DATA_INIT(CMy1724Dlg)
m_answer = _T("");
m_noans = FALSE;
m_get = _T("");
m_count = _T("");
m_decide = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMy1724Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy1724Dlg)
DDX_Control(pDX, IDC_num4, m_num4);
DDX_Control(pDX, IDC_num3, m_num3);
DDX_Control(pDX, IDC_num2, m_num2);
DDX_Control(pDX, IDC_num1, m_num1);
DDX_Control(pDX, IDC_away, m_away);
DDX_Control(pDX, IDC_submit, m_submit);
DDX_Control(pDX, IDC_next, m_next);
DDX_Control(pDX, IDC_look, m_look);
DDX_Control(pDX, IDC_close, m_close);
DDX_Control(pDX, IDC_beg, m_beg);
DDX_Text(pDX, IDC_ans, m_answer);
DDV_MaxChars(pDX, m_answer, 20);
DDX_Check(pDX, IDC_CHECK1, m_noans);
DDX_Text(pDX, IDC_EDIT2, m_get);
DDX_Text(pDX, IDC_count, m_count);
DDX_Text(pDX, IDC_decide, m_decide);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMy1724Dlg, CDialog)
//{{AFX_MSG_MAP(CMy1724Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_beg, Onbeg)
ON_BN_CLICKED(IDC_next, Onnext)
ON_BN_CLICKED(IDC_look, Onlook)
ON_BN_CLICKED(IDC_close, Onclosed)
ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
ON_BN_CLICKED(IDC_submit, Onsubmit)
ON_BN_CLICKED(IDC_away, Onaway)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy1724Dlg message handlers
BOOL CMy1724Dlg::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
m_num1.ModifyStyle(0,SS_BITMAP);
m_num1.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP11)));
m_num2.ModifyStyle(0,SS_BITMAP);
m_num2.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP11)));
m_num3.ModifyStyle(0,SS_BITMAP);
m_num3.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP11)));
m_num4.ModifyStyle(0,SS_BITMAP);
m_num4.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP11)));
return TRUE; // return TRUE unless you set the focus to a control
}
void CMy1724Dlg::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 CMy1724Dlg::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 CMy1724Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMy1724Dlg::Onbeg()
{
// TODO: Add your control notification handler code here
//129~139
srand((unsigned)time(NULL));
for(int i = 0; i < 4; i++)
{
getnum[i] = 129+rand()%11;
if(getnum[i] == 130)
{
getnum[i] = 131 +rand()%9;
}
}
m_num1.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(getnum[0])));
m_num2.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(getnum[1])));
m_num3.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(getnum[2])));
m_num4.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(getnum[3])));
m_next.EnableWindow(true);
m_submit.EnableWindow(true);
m_beg.EnableWindow(false);
}
void CMy1724Dlg::Onnext()
{
// TODO: Add your control notification handler code here
srand((unsigned)time(NULL));
for(int i = 0; i < 4; i++)
{
getnum[i] = 129+rand()%11;
if(getnum[i] == 130)
{
getnum[i] = 131 +rand()%9;
}
}
m_num1.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(getnum[0])));
m_num2.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(getnum[1])));
m_num3.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(getnum[2])));
m_num4.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(getnum[3])));
if(m_noans)
{
m_noans = false;
((CEdit*)GetDlgItem(IDC_EDIT2))->EnableWindow(true);
}
m_get = "";
((CEdit*)GetDlgItem(IDC_EDIT2))->SetFocus();
m_submit.EnableWindow(true);
UpdateData(false);
}
void CMy1724Dlg::Onlook()
{
// TODO: Add your control notification handler code here
for(int i = 0; i < 4; i++)
{
char buffer[20];
number[i] = getnum[i]-129;
number[i] = (number[i]==0)?1:number[i];
number_copy[i] = number[i];
itoa(number[i],buffer,10);
expression[i] = buffer;
}
if(!Search(COUNT_OF_NUMBER))
{
m_answer = "无解!";
}
UpdateData(false);
}
bool CMy1724Dlg::Search(int n)
{
if (n == 1) {
if ( fabs(number[0] - NUMBER_TO_BE_CAL) < PRECISION )
{
m_answer.Format(_T("%s"), (LPCTSTR)expression[0].c_str());
return true;
}
else
{
return false;
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double a, b;
string expa, expb;
a = number[i];
b = number[j];
number[j] = number[n - 1];
expa = expression[i];
expb = expression[j];
expression[j] = expression[n - 1];
expression[i] = '(' + expa + '+' + expb + ')';
number[i] = a + b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '-' + expb + ')';
number[i] = a - b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expb + '-' + expa + ')';
number[i] = b - a;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '*' + expb + ')';
number[i] = a * b;
if ( Search(n - 1) ) return true;
if (b != 0) {
expression[i] = '(' + expa + '/' + expb + ')';
number[i] = a / b;
if ( Search(n - 1) ) return true;
}
if (a != 0) {
expression[i] = '(' + expb + '/' + expa + ')';
number[i] = b / a;
if ( Search(n - 1) ) return true;
}
number[i] = a;
number[j] = b;
expression[i] = expa;
expression[j] = expb;
}
}
return false;
}
void CMy1724Dlg::Onclosed()
{
// TODO: Add your control notification handler code here
m_num1.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP11)));
m_num2.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP11)));
m_num3.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP11)));
m_num4.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP11)));
m_beg.EnableWindow(true);
m_next.EnableWindow(false);
m_decide = "";
m_count = "";
m_answer = "";
m_get = "";
numm = 0;
UpdateData(false);
}
void CMy1724Dlg::OnCheck1()
{
// TODO: Add your control notification handler code here
if(m_noans)
{
m_noans = false;
((CEdit*)GetDlgItem(IDC_EDIT2))->EnableWindow(true);
}
else
{
m_noans = true;
((CEdit*)GetDlgItem(IDC_EDIT2))->EnableWindow(false);
}
}
void CMy1724Dlg::Onsubmit()
{
// TODO: Add your control notification handler code here
char edit_temp[30];
char stack[30];
double para[5];
UpdateData(true);
strcpy(edit_temp,m_get);
if(m_noans)
{
bool Search_temp = Search(COUNT_OF_NUMBER);
if(!Search_temp)
{
char temp[30];
m_decide = "正确!";
numm++;
m_count = itoa(numm,temp,10);
m_submit.EnableWindow(false);
}
else
{
MessageBox("数据或符号错误,请重新输入!","出错",MB_OK);
m_answer = "";
}
}
else if(!m_noans && num_check(edit_temp,stack,para))
{
MessageBox("数据或符号错误,请重新输入!","出错",MB_OK);
m_get = "";
((CEdit*)GetDlgItem(IDC_EDIT2))->SetFocus();
}
else
{
if(calcul(edit_temp,para))
{
char temp[30];
m_decide = "正确!";
numm++;
m_count = itoa(numm,temp,10);
m_submit.EnableWindow(false);
}
else
{
m_decide = "错误!";
}
}
UpdateData(false);
}
bool CMy1724Dlg::calcul(char *ed,double *pa)
{
char st[30];
int st_top = -1;
int res_top = -1;
double para_a;
double para_b;
char sign;
int temp;
for(int i = 0; i < strlen(ed); i++)
{
if(ed[i] > 48 && ed[i] <= 57)
{
if(ed[i] == '1' && ed[i+1] == '0')
{
pa[++res_top] = 10.0;
i++;
}
else if(ed[i] == '1')
{
pa[++res_top] = 1.0;
}
else
{
temp = ed[i] -48;
pa[++res_top] = temp*1.0;
}
}
else if(ed[i] !=')')
{
st[++st_top] = ed[i];
}
else
{
para_b = pa[res_top--];
para_a = pa[res_top--];
sign = st[st_top--];
st_top--;
switch(sign)
{
case '+' : pa[++res_top] = para_a + para_b; break;
case '-' : pa[++res_top] = para_a - para_b; break;
case '*' : pa[++res_top] = para_a * para_b; break;
case '/' : pa[++res_top] = para_a / para_b; break;
default : break;
}
}
}
if(fabs(pa[0] - 24) < 1e-6)
return true;
return false;
}
///////////////////////////////////////////////////////////////
bool CMy1724Dlg::num_check(char *a, char *b, double *c)
{
if(a[0] == ')')
{
return true;
}
if(a[strlen(a)-1] == '(')
{
return true;
}
int bi = 0;
int ci = 0;
int qua_check = 0;
int sym_check = 0;
int int_temp;
for(int i = 0; i < (int)strlen(a); i++)
{
if(a[i] == ' ')
{
return true;
}
if(a[i] == '(' || a[i] == ')' || a[i] == '+' ||
a[i] == '-' || a[i] == '*' || a[i] == '/')
{
if((i+1) >= (int)strlen(a) && a[i] != ')')
{
return true;
}
if((a[i] == '+'||a[i] == '-'||a[i] =='*' || a[i] == '/') &&
(a[i+1] == '+'||a[i+1] == '-'||a[i+1] =='*'||a[i+1] == '/'))
{
return true;
}
if(a[i] == '(')
{
sym_check++;
}
if(a[i] == ')')
{
sym_check--;
}
b[bi++] = a[i];
}
else if(a[i] > 48 && a[i] <=57)
{
if((i+1) <strlen(a) && a[i] == '1' && a[i+1] == '0')
{
c[ci++] = 10.0;
i++;
}
else if((i+1) <strlen(a) && a[i] == '1')
{
c[ci++] = 1.0;
}
else
{
int_temp = a[i]-48;
c[ci++] = int_temp*1.0;
}
}
}
if(sym_check != 0)
{
return true;
}
b[bi] = '\0';
for(int j = 0; j < 4; j++)
{
for(int k = 0; k < 4; k++)
{
if(fabs(number_copy[j] - c[k]) <= 1e-6 )
{
qua_check++;
break;
}
}
}
if(qua_check < 4)
{
return true;
}
return false;
}
void CMy1724Dlg::Onaway()
{
// TODO: Add your control notification handler code here
MessageBox("新年快乐",": )",MB_OK);
PostQuitMessage(0);
}
void CMy1724Dlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
MessageBox("新年快乐!",":)",MB_OK);
CDialog::OnClose();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -