#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include <math.h>
TCHAR text[50];
TCHAR opt='c';
BOOL have=FALSE,enable=TRUE;
double num1=0,num2=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)
{
memset(text,'\0',sizeof(TCHAR)*50);
SetDlgItemText(hwnd,IDC_EDIT,TEXT("0."));
return TRUE;
}
void trim()
{
int i=0,j=0;
for (i=0;i<lstrlen(text);i++)
{
if (text[i]!='\0'&&text[i]!='0')
{
j=i;
}
}
for (j=j+1;j<lstrlen(text);j++)
{
text[j]='\0';
}
}
void AppendText(HWND hwnd,TCHAR c)
{
if (lstrlen(text)==1&&text[0]=='0'&&c!='.')
{
text[0]=c;
}
else
{
text[lstrlen(text)]=c;
}
SetDlgItemText(hwnd,IDC_EDIT,text);
}
void result(HWND hwnd)
{
switch(opt)
{
case '+':
num2=num2+num1;
break;
case '-':
num2=num2-num1;
break;
case '*':
num2=num1*num2;
break;
case '/':
if (num1==0)
{
SetDlgItemText(hwnd,IDC_EDIT,TEXT("除数不能为零."));/*除数不能为零*/
enable=FALSE;
return;
}
else
{
num2=num2/num1;
}
break;
default:
num2=num2+num1;
break;
}
num1=0;
sprintf(text,TEXT("%f"),num2);
trim();
SetDlgItemText(hwnd,IDC_EDIT,text);
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
TCHAR c[2];
switch(id)
{
case IDC_0:
case IDC_1:
case IDC_2:
case IDC_3:
case IDC_4:
case IDC_5:
case IDC_6:
case IDC_7:
case IDC_8:
case IDC_9:
{
if (enable)
{
GetDlgItemText(hwnd,id,c,2);
AppendText(hwnd,c[0]);
}
}
break;
case IDC_DOT:
{
if (!have&&enable)
{
have=TRUE;
if (lstrlen(text)==0)
{
AppendText(hwnd,'0');
}
AppendText(hwnd,'.');
}
}
break;
case IDC_PLUSS:
case IDC_SUB:
case IDC_MUL:
case IDC_DIV:
{
if(enable)
{
if (lstrlen(text)!=0)
{
num1=atof(text);
result(hwnd);
GetDlgItemText(hwnd,id,c,2);
opt=c[0];
memset(text,'\0',sizeof(TCHAR)*50);
have=FALSE;
}
else
{
GetDlgItemText(hwnd,id,c,2);
opt=c[0];
}
}
}
break;
case IDC_EQUAL:
{
if (lstrlen(text)!=0&&enable)
{
num1=atof(text);
result(hwnd);
memset(text,'\0',sizeof(TCHAR)*50);
have=FALSE;
}
}
break;
case IDC_C:
{
memset(text,'\0',sizeof(TCHAR)*50);
num1=0;
num2=0;
have=FALSE;
enable=TRUE;
opt='c';
SetDlgItemText(hwnd,IDC_EDIT,TEXT("0."));
}
break;
case IDC_CE:
{
memset(text,'\0',sizeof(TCHAR)*50);
num1=0;
have=FALSE;
enable=TRUE;
SetDlgItemText(hwnd,IDC_EDIT,TEXT("0."));
}
break;
case IDC_CHANGE:
{
if (strlen(text)!=0&&enable)
{
num1=atof(text);
num1=-num1;
sprintf(text,TEXT("%f"),num1);
trim();
SetDlgItemText(hwnd,IDC_EDIT,text);
}
}
break;
case IDC_1CX:
{
if (enable)
{
num1=atof(text);
if (num1==0)
{
SetDlgItemText(hwnd,IDC_EDIT,TEXT("除数不能为零."));/*除数不能为零*/
enable=FALSE;
}
else
{
num1=1.0/num1;
sprintf(text,TEXT("%f"),num1);
trim();
SetDlgItemText(hwnd,IDC_EDIT,text);
}
}
}
break;
case IDC_BFH:
{
}
break;
case IDC_SQRT:
{
if (enable)
{
num1=atof(text);
if (num1<0)
{
SetDlgItemText(hwnd,IDC_EDIT,TEXT("被开方数不能为负数."));/*被开方数不能为负数*/
enable=FALSE;
}
else
{
num1=sqrt(num1);
sprintf(text,TEXT("%f"),num1);
trim();
SetDlgItemText(hwnd,IDC_EDIT,text);
}
}
}
break;
case IDC_BACKSPACE:
{
if (lstrlen(text)>0&&enable)
{
text[lstrlen(text)-1]='\0';
if (lstrlen(text)==0)
{
SetDlgItemText(hwnd,IDC_EDIT,TEXT("0."));
}
else
{
SetDlgItemText(hwnd,IDC_EDIT,text);
}
}
}
break;
case IDC_MC:
{
}
break;
case IDC_MR:
{
}
break;
case IDC_MS:
{
}
break;
case IDC_MPLUSS:
{
}
break;
case ID_ABOUT:
{
MessageBox(hwnd,TEXT("制作人:张辉军\t\n学号:B12041316\t\nQQ:1021294347\t\n版本:V1.0"),TEXT("关于计算器"),MB_OK);
/*关于计算器制作人,版本学号联系方式*/
}
break;
case ID_HP:
{
system("start C:\\windows\\help\\calc.chm");
}
break;
default:
break;
}
}
void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include <math.h>
TCHAR text[50];
TCHAR opt='c';
BOOL have=FALSE,enable=TRUE;
double num1=0,num2=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)
{
memset(text,'\0',sizeof(TCHAR)*50);
SetDlgItemText(hwnd,IDC_EDIT,TEXT("0."));
return TRUE;
}
void trim()
{
int i=0,j=0;
for (i=0;i<lstrlen(text);i++)
{
if (text[i]!='\0'&&text[i]!='0')
{
j=i;
}
}
for (j=j+1;j<lstrlen(text);j++)
{
text[j]='\0';
}
}
void AppendText(HWND hwnd,TCHAR c)
{
if (lstrlen(text)==1&&text[0]=='0'&&c!='.')
{
text[0]=c;
}
else
{
text[lstrlen(text)]=c;
}
SetDlgItemText(hwnd,IDC_EDIT,text);
}
void result(HWND hwnd)
{
switch(opt)
{
case '+':
num2=num2+num1;
break;
case '-':
num2=num2-num1;
break;
case '*':
num2=num1*num2;
break;
case '/':
if (num1==0)
{
SetDlgItemText(hwnd,IDC_EDIT,TEXT("除数不能为零."));/*除数不能为零*/
enable=FALSE;
return;
}
else
{
num2=num2/num1;
}
break;
default:
num2=num2+num1;
break;
}
num1=0;
sprintf(text,TEXT("%f"),num2);
trim();
SetDlgItemText(hwnd,IDC_EDIT,text);
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
TCHAR c[2];
switch(id)
{
case IDC_0:
case IDC_1:
case IDC_2:
case IDC_3:
case IDC_4:
case IDC_5:
case IDC_6:
case IDC_7:
case IDC_8:
case IDC_9:
{
if (enable)
{
GetDlgItemText(hwnd,id,c,2);
AppendText(hwnd,c[0]);
}
}
break;
case IDC_DOT:
{
if (!have&&enable)
{
have=TRUE;
if (lstrlen(text)==0)
{
AppendText(hwnd,'0');
}
AppendText(hwnd,'.');
}
}
break;
case IDC_PLUSS:
case IDC_SUB:
case IDC_MUL:
case IDC_DIV:
{
if(enable)
{
if (lstrlen(text)!=0)
{
num1=atof(text);
result(hwnd);
GetDlgItemText(hwnd,id,c,2);
opt=c[0];
memset(text,'\0',sizeof(TCHAR)*50);
have=FALSE;
}
else
{
GetDlgItemText(hwnd,id,c,2);
opt=c[0];
}
}
}
break;
case IDC_EQUAL:
{
if (lstrlen(text)!=0&&enable)
{
num1=atof(text);
result(hwnd);
memset(text,'\0',sizeof(TCHAR)*50);
have=FALSE;
}
}
break;
case IDC_C:
{
memset(text,'\0',sizeof(TCHAR)*50);
num1=0;
num2=0;
have=FALSE;
enable=TRUE;
opt='c';
SetDlgItemText(hwnd,IDC_EDIT,TEXT("0."));
}
break;
case IDC_CE:
{
memset(text,'\0',sizeof(TCHAR)*50);
num1=0;
have=FALSE;
enable=TRUE;
SetDlgItemText(hwnd,IDC_EDIT,TEXT("0."));
}
break;
case IDC_CHANGE:
{
if (strlen(text)!=0&&enable)
{
num1=atof(text);
num1=-num1;
sprintf(text,TEXT("%f"),num1);
trim();
SetDlgItemText(hwnd,IDC_EDIT,text);
}
}
break;
case IDC_1CX:
{
if (enable)
{
num1=atof(text);
if (num1==0)
{
SetDlgItemText(hwnd,IDC_EDIT,TEXT("除数不能为零."));/*除数不能为零*/
enable=FALSE;
}
else
{
num1=1.0/num1;
sprintf(text,TEXT("%f"),num1);
trim();
SetDlgItemText(hwnd,IDC_EDIT,text);
}
}
}
break;
case IDC_BFH:
{
}
break;
case IDC_SQRT:
{
if (enable)
{
num1=atof(text);
if (num1<0)
{
SetDlgItemText(hwnd,IDC_EDIT,TEXT("被开方数不能为负数."));/*被开方数不能为负数*/
enable=FALSE;
}
else
{
num1=sqrt(num1);
sprintf(text,TEXT("%f"),num1);
trim();
SetDlgItemText(hwnd,IDC_EDIT,text);
}
}
}
break;
case IDC_BACKSPACE:
{
if (lstrlen(text)>0&&enable)
{
text[lstrlen(text)-1]='\0';
if (lstrlen(text)==0)
{
SetDlgItemText(hwnd,IDC_EDIT,TEXT("0."));
}
else
{
SetDlgItemText(hwnd,IDC_EDIT,text);
}
}
}
break;
case IDC_MC:
{
}
break;
case IDC_MR:
{
}
break;
case IDC_MS:
{
}
break;
case IDC_MPLUSS:
{
}
break;
case ID_ABOUT:
{
MessageBox(hwnd,TEXT("制作人:张辉军\t\n学号:B12041316\t\nQQ:1021294347\t\n版本:V1.0"),TEXT("关于计算器"),MB_OK);
/*关于计算器制作人,版本学号联系方式*/
}
break;
case ID_HP:
{
system("start C:\\windows\\help\\calc.chm");
}
break;
default:
break;
}
}
void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}