最近看了个视频,做了个小计算器,只能做整数运算,想做兼容小数的运算现在自己改了下,还是不行,大家帮我改改!
#include "stdafx.h"#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
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)
{
HWND hwndComboOp=GetDlgItem(hwnd,IDC_COMBOOP);
ComboBox_InsertString(hwndComboOp,-1,TEXT("+"));
ComboBox_InsertString(hwndComboOp,-1,TEXT("-"));
ComboBox_InsertString(hwndComboOp,-1,TEXT("*"));
ComboBox_InsertString(hwndComboOp,-1,TEXT("/"));
return TRUE;
}
BOOL IsInt(TCHAR* str)
{
double i=atoi(str);
TCHAR strtemp[256];
wsprintf(strtemp,"%f",i);
if(strcmp(str,strtemp)!=0)
{
return FALSE;
}
else
{
return TRUE;
}
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case IDC_OK:
{
TCHAR str1[256];
TCHAR str2[256];
TCHAR str3[256];
GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
if(IsInt(str1)==FALSE&&IsInt(str2)==FALSE)
{
MessageBox(hwnd,TEXT("第一、二个数都不是合法整数"),TEXT("错误"),MB_OK|MB_ICONERROR);
return;
}
if(IsInt(str1)==FALSE)
{
MessageBox(hwnd,TEXT("第一个数不是合法的整数"),TEXT("错误"),
MB_OK|MB_ICONERROR);
return;
}
if(IsInt(str2)==FALSE)
{
MessageBox(hwnd,TEXT("第二个数不是合法的整数"),TEXT("错误"),
MB_OK|MB_ICONERROR);
return;
}
double i1=atoi(str1);
double i2=atoi(str2);
double i3=0;
HWND hwndComboOp=GetDlgItem(hwnd,IDC_COMBOOP);
int curIndex = ComboBox_GetCurSel(hwndComboOp);
switch(curIndex)
{
case 0:
{
i3=i1+i2;
}
break;
case 1:
{
i3=i1-i2;
}
break;
case 2:
{
i3=i1*i2;
}
break;
case 3:
{
i3=i1/i2;
}
break;
default: MessageBox(hwnd,TEXT("未选择运算"),TEXT("错误"),MB_OK|MB_ICONERROR);
break;
}
itoa(i3,str3,10);
SetDlgItemText(hwnd,IDC_EDIT3,str3);
}
break;
case IDC_GUILING:
{
SetDlgItemText(hwnd,IDC_EDIT1,0);
SetDlgItemText(hwnd,IDC_EDIT2,0);
SetDlgItemText(hwnd,IDC_EDIT3,0);
}
default:
break;
}
}
void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}