- 注册时间
- 2010-10-1
- 最后登录
- 2010-10-6
- 在线时间
- 2 小时
- 鹏币
- 0
- 阅读权限
- 10
- 积分
- 21
- 帖子
- 1
- 主题
- 0
- 精华
- 0
- UID
- 61281

|
发表于 2010-10-5 16:47:57
|显示全部楼层
其实老师的计算器(在《C语言也能干大事第四节》)不仅可以利用函数的调用来简化代码。。还可以用SWITCH来得到更简化的代码。。。。
case IDC_OK:
{
HWND hwndCombo1=GetDlgItem(hwnd,IDC_COMBO1);
int p=ComboBox_GetCurSel(hwndCombo1);
TCHAR s1[50];
TCHAR s2[50];
GetDlgItemText(hwnd,IDC_EDIT1,s1,sizeof(s1));
GetDlgItemText(hwnd,IDC_EDIT2,s2,sizeof(s2));
int a=atoi(s1);
int b=atoi(s2);
TCHAR s4[50];
TCHAR s5[50];
itoa(a,s4,10);
itoa(b,s5,10);
if(strcmp(s1,s4)!=0||strcmp(s2,s5)!=0)
{
MessageBox(hwnd,TEXT("输入必须是整数"),TEXT("警告"),MB_OK|MB_ICONERROR);
}
else
{
int c;
TCHAR s3[50];
switch (p)
{
case 3: wsprintf(s3,"%i",(a+b));break;
case 0: wsprintf(s3,"%i",(a-b));break;
case 1: wsprintf(s3,"%i",(a*b));break;
default:;
}
SetDlgItemText(hwnd,IDC_EDIT3,s3);
}
} |
|