求助一个用MFC来编写的计算器程序代码
2个回答
展开全部
主代码如下。。这个是我自己做的。就怕你看不懂了。。。
不过敢上来问,应该是比较精通的了。。
#include
"stdafx.h"
#include
<stdio.h>
#include
<stdlib.h>
#include
<windows.h>
#include
<windowsx.h>
#include
"resource.h"
#include
"MainDlg.h"
#define
TURE
1
#define
FALSE
0
BOOL
WINAPI
Main_Proc(HWND
hWnd,
UINT
uMsg,
WPARAM
wParam,
LPARAM
lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd,
WM_INITDIALOG,
Main_OnInitDialog);
HANDLE_MSG(hWnd,
WM_COMMAND,
Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE,
Main_OnClose);
}
return
FALSE;
}
BOOL
Main_OnInitDialog(HWND
hwnd,
HWND
hwndFocus,
LPARAM
lParam)
{
return
TRUE;
}
BOOL
IsInt(TCHAR*
str)
{
int
i=atoi(str);
TCHAR
strtemp[256];
wsprintf(strtemp,"%i",i);
if(strcmp(str,strtemp)==0)
{
return
TURE;
}
else
{
return
FALSE;
}
}
TCHAR
str1[256],str2[256],str3[256];
int
i1,i2,i3;
void
Main_OnCommand(HWND
hwnd,
int
id,
HWND
hwndCtl,
UINT
codeNotify)
{
switch(id)
{
case
IDC_OK:
{
GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
int
i1,i2,i3;
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);i2=atoi(str2);i3=i1+i2;
wsprintf(str3,"%i",i3);
SetDlgItemText(hwnd,IDC_EDIT3,str3);
}
break;
case
IDC_OK2:
{
GetDlgItemText(hwnd,IDC_EDIT4,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT5,str2,sizeof(str2));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);
i2=atoi(str2);
i3=i1-i2;
wsprintf(str3,"%i",i3);
SetDlgItemText(hwnd,IDC_EDIT6,str3);
}
break;
case
IDC_OK3:
{
GetDlgItemText(hwnd,IDC_EDIT7,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT8,str2,sizeof(str2));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);i2=atoi(str2);
i3=i1*i2;
itoa(i3,str3,10);
SetDlgItemText(hwnd,IDC_EDIT9,str3);
}
break;
case
IDC_OK4:
{
GetDlgItemText(hwnd,IDC_EDIT10,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT11,str2,sizeof(str2));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);
i2=atoi(str2);
i3=i1/i2;
itoa(i3,str3,10);
//else
str3="除不掉";这里仍然有个bug!
SetDlgItemText(hwnd,IDC_EDIT12,str3);
}
break;
case
IDC_OK5:
{
GetDlgItemText(hwnd,IDC_EDIT13,str1,sizeof(str1));
i1=atoi(str1);
itoa(i1,str2,2);
SetDlgItemText(hwnd,IDC_EDIT14,str2);
}
break;
case
IDC_OK6:
{
GetDlgItemText(hwnd,IDC_EDIT15,str1,sizeof(str1));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);
itoa(i1,str2,16);
SetDlgItemText(hwnd,IDC_EDIT16,str2);
}
break;
default:
break;
}
}
void
Main_OnClose(HWND
hwnd)
{
EndDialog(hwnd,
0);
}
不过敢上来问,应该是比较精通的了。。
#include
"stdafx.h"
#include
<stdio.h>
#include
<stdlib.h>
#include
<windows.h>
#include
<windowsx.h>
#include
"resource.h"
#include
"MainDlg.h"
#define
TURE
1
#define
FALSE
0
BOOL
WINAPI
Main_Proc(HWND
hWnd,
UINT
uMsg,
WPARAM
wParam,
LPARAM
lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd,
WM_INITDIALOG,
Main_OnInitDialog);
HANDLE_MSG(hWnd,
WM_COMMAND,
Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE,
Main_OnClose);
}
return
FALSE;
}
BOOL
Main_OnInitDialog(HWND
hwnd,
HWND
hwndFocus,
LPARAM
lParam)
{
return
TRUE;
}
BOOL
IsInt(TCHAR*
str)
{
int
i=atoi(str);
TCHAR
strtemp[256];
wsprintf(strtemp,"%i",i);
if(strcmp(str,strtemp)==0)
{
return
TURE;
}
else
{
return
FALSE;
}
}
TCHAR
str1[256],str2[256],str3[256];
int
i1,i2,i3;
void
Main_OnCommand(HWND
hwnd,
int
id,
HWND
hwndCtl,
UINT
codeNotify)
{
switch(id)
{
case
IDC_OK:
{
GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
int
i1,i2,i3;
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);i2=atoi(str2);i3=i1+i2;
wsprintf(str3,"%i",i3);
SetDlgItemText(hwnd,IDC_EDIT3,str3);
}
break;
case
IDC_OK2:
{
GetDlgItemText(hwnd,IDC_EDIT4,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT5,str2,sizeof(str2));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);
i2=atoi(str2);
i3=i1-i2;
wsprintf(str3,"%i",i3);
SetDlgItemText(hwnd,IDC_EDIT6,str3);
}
break;
case
IDC_OK3:
{
GetDlgItemText(hwnd,IDC_EDIT7,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT8,str2,sizeof(str2));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);i2=atoi(str2);
i3=i1*i2;
itoa(i3,str3,10);
SetDlgItemText(hwnd,IDC_EDIT9,str3);
}
break;
case
IDC_OK4:
{
GetDlgItemText(hwnd,IDC_EDIT10,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT11,str2,sizeof(str2));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);
i2=atoi(str2);
i3=i1/i2;
itoa(i3,str3,10);
//else
str3="除不掉";这里仍然有个bug!
SetDlgItemText(hwnd,IDC_EDIT12,str3);
}
break;
case
IDC_OK5:
{
GetDlgItemText(hwnd,IDC_EDIT13,str1,sizeof(str1));
i1=atoi(str1);
itoa(i1,str2,2);
SetDlgItemText(hwnd,IDC_EDIT14,str2);
}
break;
case
IDC_OK6:
{
GetDlgItemText(hwnd,IDC_EDIT15,str1,sizeof(str1));
if(!IsInt(str1))
{
MessageBox(hwnd,TEXT("第一个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
if(!IsInt(str2))
{
MessageBox(hwnd,TEXT("第二个数不是整数!"),TEXT("错误"),MB_OK|MB_ICONERROR);
return
;
}
i1=atoi(str1);
itoa(i1,str2,16);
SetDlgItemText(hwnd,IDC_EDIT16,str2);
}
break;
default:
break;
}
}
void
Main_OnClose(HWND
hwnd)
{
EndDialog(hwnd,
0);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询