这里是我的源代码,请各位斧正。只因电脑不在手边,回贴晚了
#include <windows.h>
#include<time.h>
#define IDB_OK 101
/* This is where all the input to the window goes to */
HWND txtFrame,txtUpper,txtKey,lblUpper,btnOK,lblNum,txtNum,lblLower,txtLower;
char str_upper[3],str_lower[3];short upper,lower;
class Question
{
private:
short add_one;
short add_two;
short sum;
short add;//运算符标记。标识做加法1还是减法0
short blank;//标识第几个数作为问题,10,20,30
short answer;
public:
short GetAnswer(){return answer;}
char str[37];
char str1[12];
char str2[12];
char str3[12];
Question()
{
while(!Reset());
}
int Reset()
{
strcpy(str1,"");strcpy(str2,"");strcpy(str3,"");
GetWindowText(txtUpper,str_upper,3);upper=atoi(str_upper);
GetWindowText(txtLower,str_lower,3);lower=atoi(str_lower);
short span;
/****************
以下两行为问题代码**********************/
//if(upper-lower==10) span=10;else span=40; //若执行这一行可以对span取余
span=upper-lower;
//若执行这一行不能对span取余
/********************************************************/
sum=(((unsigned short)rand())%span)+lower+1;//为什么不能对span取模???
add_one=((unsigned short)rand())%(sum-1)+1;
add_two=sum-add_one;
add=((unsigned short)rand())%2;
blank=(((unsigned short)rand())%3+1)*10;
switch(blank+add)
{
case 11:
answer=add_one;
strcpy(str1,TEXT("(
) + "));
itoa(add_two,str2,10);
strcat(str2," = ");
itoa(sum,str3,10);
break;
case 10:
answer=sum;
strcpy(str1,TEXT("(
) - "));
itoa(add_one,str2,10);
strcat(str2," = ");
itoa(add_two,str3,10);
break;
case 21:
answer=add_two;
itoa(add_one,str1,10);
strcat(str1," + ");
strcpy(str2,TEXT("(
) = "));
itoa(sum,str3,10);
break;
case 20:
answer=add_one;
itoa(sum,str1,10);
strcat(str1," - ");
strcpy(str2,"(
) = ");
itoa(add_two,str3,10);
break;
case 31:
answer=sum;
itoa(add_one,str1,10);
strcat(str1," + ");
itoa(add_two,str2,10);
strcat(str2," = ");
strcpy(str3,TEXT("(
)"));
break;
case 30:
answer=add_two;
itoa(sum,str1,10);
strcat(str1," - ");
itoa(add_one,str2,10);
strcat(str2," = ");
strcpy(str3,TEXT("(
)"));
break;
default:return 0;
}
memset(str,0,30);
strcat(str,str1);
strcat(str,str2);
strcat(str,str3);
return 1;
}
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
Question q;short num;char strnum[3];char strkey[2];short i;char oldq[5000];char oldk[256];
switch(Message) {
/* Upon destruction, tell the main thread to stop */
case WM_CREATE:
txtFrame=CreateWindow(TEXT("EDIT"),TEXT(""),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_VSCROLL|WS_BORDER|SS_LEFT,20,20,580,360,hwnd,NULL,NULL,NULL);
txtKey=CreateWindow(TEXT("EDIT"),TEXT(""),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_BORDER|SS_LEFT,20,400,580,40,hwnd,NULL,NULL,NULL);
txtUpper=CreateWindow(TEXT("EDIT"),TEXT("20"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_BORDER|SS_LEFT,100,470,22,22,hwnd,NULL,NULL,NULL);
lblUpper=CreateWindow(TEXT("STATIC"),TEXT("数字上限"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|SS_CENTER,20,470,80,40,hwnd,NULL,NULL,NULL);
lblLower=CreateWindow(TEXT("STATIC"),TEXT("数字下限"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|SS_CENTER,140,470,80,40,hwnd,NULL,NULL,NULL);
txtLower=CreateWindow(TEXT("EDIT"),TEXT("10"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER,220,470,22,22,hwnd,NULL,NULL,NULL);
lblNum=CreateWindow(TEXT("STATIC"),TEXT("题目数"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|SS_CENTER,300,470,69,40,hwnd,NULL,NULL,NULL);
txtNum=CreateWindow(TEXT("EDIT"),TEXT("10"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_BORDER|SS_LEFT,370,470,40,22,hwnd,NULL,NULL,NULL);
btnOK=CreateWindow(TEXT("BUTTON"),TEXT("确定"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER|BS_PUSHBUTTON,500,470,80,40,hwnd,(HMENU)IDB_OK,NULL,NULL);
break;
case WM_COMMAND:
switch(HIWORD(wParam))
{
case BN_CLICKED:
switch(LOWORD(wParam))
{
case IDB_OK:
srand((unsigned int)time(0));
SetWindowText(txtFrame,"");SetWindowText(txtKey,"");
GetWindowText(txtNum,strnum,3);num=atoi(strnum);
for(i=0;i<num;i++)
{
GetWindowText(txtFrame,oldq,5000);
strcat(oldq,q.str);
if(i%3!=2)
strcat(oldq,"\t\t");
else
{
strcat(oldq,"\r\n");
}
SetWindowText(txtFrame,oldq);
GetWindowText(txtKey,oldk,256);
itoa(q.GetAnswer(),strkey,10);
strcat(oldk,strkey);strcat(oldk,"
");
SetWindowText(txtKey,oldk);
q.Reset();
}
char siz[4];itoa(i,siz,10);SetWindowText(hwnd,siz);
break;
}
break;
}
break;
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG Msg; /* A temporary location for all messages */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize
= sizeof(WNDCLASSEX);
wc.lpfnWndProc
= WndProc; /* This is where we will send messages to */
wc.hInstance
= hInstance;
wc.hCursor
= LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon
= LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm
= LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","幼儿数学",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
640, /* width */
580, /* height */
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&Msg); /* Translate key codes to chars if present */
DispatchMessage(&Msg); /* Send it to WndProc */
}
return Msg.wParam;
}