📄 窗口.cpp
字号:
#include<windows.h>
#include"8_1.h"
#define FALSE 0
#define Max 10000
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
//- - - - - - - - - 以下初始化窗口类 - - - - - - - - - - - - - - - - -//
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInst,
LPSTR lpszCmdLine,
int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszMenuName[]="Menu";
char lpszClassName[]="窗口";
char lpszTitle[]="24点计算器";
//窗口类的定义
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(hInstance,"ABC");
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=lpszMenuName;
wndclass.lpszClassName=lpszClassName;
//----------------以下进行窗口类的注册------------------------------------//
if(!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
//创建窗口
hwnd=CreateWindow(lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
390,
220,
NULL,
NULL,
hInstance,
NULL);
hInst=hInstance;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
//窗口函数
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
char lpsz_enter[]="输入4个大于0的数:";
char lpsz_Show[]="答案:";
char lpsz_Key[]="输入你的表达式:";
switch(message)
{
case WM_PAINT:
hDC=BeginPaint(hwnd,&ps);
SetTextColor(hDC,RGB(0,0,0));
TextOut(hDC,10,8,lpsz_enter,strlen(lpsz_enter));
TextOut(hDC,10,53,lpsz_Show,strlen(lpsz_Show));
TextOut(hDC,200,8,lpsz_Key,strlen(lpsz_Key));
EndPaint(hwnd,&ps);
break;
case WM_CREATE:
hEditBox0=CreateWindow("EDIT",
NULL,
WS_CHILD|WS_VISIBLE|ES_LEFT|WS_BORDER,
10,25,
40,20,
hwnd,
(HMENU)IDE_INPUT0,
hInst,
NULL
);
hEditBox1=CreateWindow("EDIT",
NULL,
WS_CHILD|WS_VISIBLE|ES_LEFT|WS_BORDER,
55,25,
40,20,
hwnd,
(HMENU)IDE_INPUT1,
hInst,
NULL
);
hEditBox2=CreateWindow("EDIT",
NULL,
WS_CHILD|WS_VISIBLE|ES_LEFT|WS_BORDER,
100,25,
40,20,
hwnd,
(HMENU)IDE_INPUT2,
hInst,
NULL
);
hEditBox3=CreateWindow("EDIT",
NULL,
WS_CHILD|WS_VISIBLE|ES_LEFT|WS_BORDER,
145,25,
40,20,
hwnd,
(HMENU)IDE_INPUT3,
hInst,
NULL
);
hResultBox=CreateWindow("EDIT",
NULL,
WS_CHILD|WS_VISIBLE|ES_LEFT|WS_BORDER|ES_READONLY|ES_AUTOVSCROLL,
10,70,
170,60,
hwnd,
(HMENU)IDE_RESULT,
hInst,
NULL
);
hInputBox=CreateWindow("EDIT",
NULL,
WS_CHILD|WS_VISIBLE|ES_LEFT|WS_BORDER,
200,25,
170,50,
hwnd,
(HMENU)IDE_INPUTRESULT,
hInst,
NULL
);
hwndPush=CreateWindow("BUTTON",
"显示结果",
BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,
10,135,
170,40,
hwnd,
(HMENU)IDB_RESULTBUTTON,
hInst,
NULL
);
hwndPush2=CreateWindow("BUTTON",
"提交",
BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,
200,90,
170,40,
hwnd,
(HMENU)IDB_KEYBUTTON,
hInst,
NULL
);
hwndExit=CreateWindow("BUTTON",
"退出",
BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,
200,135,
170,40,
hwnd,
(HMENU)IDB_EXITBUTTON,
hInst,
NULL
);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_EXITBUTTON:
SendMessage(hwnd,WM_DESTROY,0,0);
case IDB_KEYBUTTON:
SendMessage(hInputBox,EM_GETSEL,0,0);
nMax=GetWindowTextLength(hInputBox)+1;
GetWindowText(hInputBox,lpsz_5,nMax);
SetWindowText(hResultBox,lpsz_5);
Read();
break;
case IDB_RESULTBUTTON:
SendMessage(hEditBox0,EM_GETSEL,0,0);
nMax=GetWindowTextLength(hEditBox0)+1;
GetWindowText(hEditBox0,lpsz_4,nMax);
Num[0]=strtol(lpsz_4,&stop,10);
SendMessage(hEditBox1,EM_GETSEL,0,0);
nMax=GetWindowTextLength(hEditBox1)+1;
GetWindowText(hEditBox1,lpsz_4,nMax);
Num[1]=strtol(lpsz_4,&stop,10);
SendMessage(hEditBox0,EM_GETSEL,0,0);
nMax=GetWindowTextLength(hEditBox2)+1;
GetWindowText(hEditBox2,lpsz_4,nMax);
Num[2]=strtol(lpsz_4,&stop,10);
SendMessage(hEditBox0,EM_GETSEL,0,0);
nMax=GetWindowTextLength(hEditBox3)+1;
GetWindowText(hEditBox3,lpsz_4,nMax);
Num[3]=strtol(lpsz_4,&stop,10);
caculate3();
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
void print(int ch)
{
switch(ch)
{
case 0:strcpy(lpsz,"+");
break;
case 1:strcpy(lpsz,"-");
break;
case 2:strcpy(lpsz,"*");
break;
case 3:strcpy(lpsz,"/");
break;
}
}
void print1(int num[],int ch[])
{
char lpsz_2[80]="";
strcpy(lpsz_2,":");
for(int i=0;i<3;i++)
{
_ltoa(num[i],lpsz,10);
strcat(lpsz_2,lpsz);
print(ch[i]);
strcat(lpsz_2,lpsz);
}
_ltoa(num[3],lpsz,10);
strcat(lpsz_2,lpsz);
SetWindowText(hResultBox,lpsz_2);
}
void print2(int num[],int ch[],int change[])
{
char lpsz_3[80]="";
int a=0;
strcpy(lpsz_3,":");
for(int i=0;i<3;i++)
a=a*10+change[i];
switch(a)
{
case 123:
strcat(lpsz_3,"((");
_ltoa(num[0],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[0]);
strcat(lpsz_3,lpsz);
_ltoa(num[1],lpsz,10);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,")");
print(ch[1]);
strcat(lpsz_3,lpsz);
_ltoa(num[2],lpsz,10);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,")");
print(ch[2]);
strcat(lpsz_3,lpsz);
_ltoa(num[3],lpsz,10);
strcat(lpsz_3,lpsz);
SetWindowText(hResultBox,lpsz_3);
break;
case 132:
strcat(lpsz_3,"(");
_ltoa(num[0],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[0]);
strcat(lpsz_3,lpsz);
_ltoa(num[1],lpsz,10);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,")");
print(ch[1]);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,"(");
_ltoa(num[2],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[2]);
strcat(lpsz_3,lpsz);
_ltoa(num[3],lpsz,10);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,")");
SetWindowText(hResultBox,lpsz_3);
break;
case 213:
strcat(lpsz_3,"(");
_ltoa(num[0],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[0]);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,"(");
_ltoa(num[1],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[1]);
strcat(lpsz_3,lpsz);
_ltoa(num[2],lpsz,10);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,"))");
print(ch[2]);
strcat(lpsz_3,lpsz);
_ltoa(num[3],lpsz,10);
strcat(lpsz_3,lpsz);
SetWindowText(hResultBox,lpsz_3);
break;
case 321:
_ltoa(num[0],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[0]);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,"(");
_ltoa(num[1],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[1]);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,"(");
_ltoa(num[2],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[2]);
strcat(lpsz_3,lpsz);
_ltoa(num[3],lpsz,10);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,"))");
SetWindowText(hResultBox,lpsz_3);
break;
case 312:
_ltoa(num[0],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[0]);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,"((");
_ltoa(num[1],lpsz,10);
strcat(lpsz_3,lpsz);
print(ch[1]);
strcat(lpsz_3,lpsz);
_ltoa(num[2],lpsz,10);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,")");
print(ch[2]);
strcat(lpsz_3,lpsz);
_ltoa(num[3],lpsz,10);
strcat(lpsz_3,lpsz);
strcat(lpsz_3,")");
SetWindowText(hResultBox,lpsz_3);
break;
}
}
int caculate1(int num[],int ch[])
{
int i;
int a=num[0];
for(i=0;i<3;i++)
{
switch(ch[i])
{
case 0:
a+=num[i+1];
break;
case 1:
a-=num[i+1];
break;
case 2:
a*=num[i+1];
break;
case 3:
if(num[i+1]==0)return 0;
else
if(a%num[i+1]!=0)
return 0;
else
a/=num[i+1];
break;
}
}
return a;
}
void Read()
{
int num[4]={0},tokens[3],change[3]={3,3,3};
int i,n,j=0,k=0,m=0,m2=0,cm;
int am[2]={0},bm[2]={0};
n=strlen(lpsz_5);
for(i=0;i<n;i++)
{
if(lpsz_5[i]>='0'&&lpsz_5[i]<='9')
{
num[k]=num[k]*10+lpsz_5[i]-'0';
}
if(lpsz_5[i]=='+'){tokens[j]=0;j++;k++;}
if(lpsz_5[i]=='-'){tokens[j]=1;j++;k++;}
if(lpsz_5[i]=='*'){tokens[j]=2;j++;k++;}
if(lpsz_5[i]=='/'){tokens[j]=3;j++;k++;}
if(lpsz_5[i]=='('){am[m]=k;m++;}
if(lpsz_5[i]==')'&&m>m2){bm[m2]=k-1;m2++;}
}
if(m==m2)
{
if(m==2)
{
if(am[0]==am[1])
{
m2=am[0];
change[m2]=1;
m2=bm[1];
change[m2]=2;
}
else if(bm[0]==bm[1])
{
m2=bm[0];
change[m2]=1;
m2=am[0];
change[m2]=2;
}
else
{
m2=am[0];
change[m2]=1;
m2=am[1];
change[m2]=2;
}
if(caculate2(num,tokens,change)==24)SetWindowText(hResultBox,"正确!");
else SetWindowText(hResultBox,"错误!");
}
else if(m==1)
{
change[0]=1;
change[1]=2;
change[2]=3;
m2=am[0];
if(m2!=0)
for(i=0;i<3;i++)
{
cm=change[m2-1];
change[m2-1]=change[m2];
change[m2]=cm;
}
if(caculate2(num,tokens,change)==24)SetWindowText(hResultBox,"正确!");
else SetWindowText(hResultBox,"错误!");
}
else
{
if(caculate1(num,tokens)==24)SetWindowText(hResultBox,"正确!");
else SetWindowText(hResultBox,"错误!");
}
}
}
void caculate3()
{
int array[24][5]={0},change[5][3]={1,2,3,1,3,2,2,1,3,3,2,1,3,1,2},tokens[3]={0};
int i,j,k,m,n=0,sum=0;
int num2[4]={0},change2[3]={0},jud[24],num3[4]={0};
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(j!=i)
for(k=0;k<4;k++)
if(k!=i&&k!=j)
for(m=0;m<4;m++)
if(m!=i&&m!=j&&m!=k)
{
array[n][0]=Num[i];
array[n][1]=Num[j];
array[n][2]=Num[k];
array[n][3]=Num[m];
jud[n]=Num[i]*1000+Num[j]*100+Num[k]*10+Num[m];
n++;
}
for(i=0;i<24;i++)
for(j=0;j<24;j++)
if(i!=j&&jud[j]==jud[i])array[i][4]=1;
n=0;
while(sum!=4)
{
sum=1;
for(i=0;i<24;i++)
{
for(j=0;j<4;j++)
num3[j]=num2[j]=array[i][j];
if(n==0||n==42)
{
if(caculate1(num2,tokens)==24)
{ print1(num3,tokens);return ;}
}
else
{
for(k=0;k<5;k++)
{
for(m=0;m<3;m++)
change2[m]=change[k][m];
if(caculate2(num2,tokens,change2)==24)
{print2(num3,tokens,change2);return ;}
}
}
}
for(i=0;i<sum;i++)
{
tokens[i]+=1;
if(tokens[i]==4)
{
tokens[i]=0;
sum++;
}
}
n++;
}
SetWindowText(hResultBox,"没有答案!");
}
int cacul(int a,int b,int ch)
{
switch(ch)
{
case 0:
a+=b;
break;
case 1:
a-=b;
break;
case 2:
a*=b;
break;
case 3:
if(b==0)
return Max;
else if(a%b!=0)
return Max;
a/=b;
break;
}
return a;
}
int caculate2(int num[],int ch[],int change[])
{
int a=0,b,c;
bool jud=false;
for(int i=0;i<3;i++)
a=a*10+change[i];
switch(a)
{
case 123:
b=cacul(num[0],num[1],ch[0]);
if(b!=Max)
{
b=cacul(b,num[2],ch[1]);
if(b!=Max)
{
b=cacul(b,num[3],ch[2]);
jud=true;
}
}
break;
case 132:
b=cacul(num[0],num[1],ch[0]);
if(b!=Max)
{
c=cacul(num[2],num[3],ch[2]);
if(c!=Max)
{
b=cacul(b,c,ch[1]);
jud=true;
}
}
break;
case 231:
b=cacul(num[0],num[1],ch[0]);
if(b!=Max)
{
c=cacul(num[2],num[3],ch[2]);
if(c!=Max)
{
b=cacul(b,c,ch[1]);
jud=true;
}
}
break;
case 213:
b=cacul(num[1],num[2],ch[1]);
if(b!=Max)
{
b=cacul(num[0],b,ch[0]);
if(b!=Max)
{
b=cacul(b ,num[3],ch[2]);
jud=true;
}
}
break;
case 321:
b=cacul(num[3],num[2],ch[2]);
if(b!=Max)
{
b=cacul(num[1],b,ch[1]);
if(b!=Max)
{
b=cacul(num[0],b,ch[0]);
jud=true;
}
}
break;
case 312:
b=cacul(num[1],num[2],ch[1]);
if(b!=Max)
{
b=cacul(b,num[3],ch[2]);
if(b!=Max)
{
b=cacul(num[0],b,ch[0]);
jud=true;
}
}
break;
}
if(jud==true)
return b;
else return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -