15,979
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include "core.h"
#include "resource.h"
LRESULT CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
char source [MAX_SIZE];
char dest [40];
double cal_result;
HINSTANCE hInst;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
static TCHAR szAppName[] = TEXT ("HexCalc") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
HACCEL hAccel ;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = DialogProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = DLGWINDOWEXTRA ; // Note!
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL,IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = MAKEINTRESOURCE(IDD_DIALOG) ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateDialog (hInstance, MAKEINTRESOURCE(IDD_DIALOG), 0, NULL) ;
ShowWindow (hwnd, nShowCmd) ;
hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE(ID_ACC)) ;
while (GetMessage (&msg, NULL, 0, 0))
{
if (!TranslateAccelerator (hwnd, hAccel, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return msg.wParam ;
}
LRESULT CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hInput = GetDlgItem (hDlg, IDC_INPUT);
HWND hOut = GetDlgItem (hDlg, IDC_OUT);
switch (message)
{
case WM_COMMAND:
SetFocus (hDlg) ;
switch (LOWORD (wParam))
{
case IDM_HELP_ABOUT:
if (DialogBox (hInst, TEXT ("AboutBox"), hDlg, AboutDlgProc))
InvalidateRect (hDlg, NULL, TRUE) ;
return 0 ;
case IDC_0:
strcat (source, "0");
SetWindowTextA (hInput, source);
break;
case IDC_1:printf("1\n");
strcat (source, "1");
SetWindowTextA (hInput, source);
break;
case IDC_2:
strcat (source, "2");
SetWindowTextA (hInput, source);
break;
case IDC_3:
strcat (source, "3");
SetWindowTextA (hInput, source);
break;
case IDC_4:
strcat (source, "4");
SetWindowTextA (hInput, source);
break;
case IDC_5:
strcat (source, "5");
SetWindowTextA (hInput, source);
break;
case IDC_6:
strcat (source, "6");
SetWindowTextA (hInput, source);
break;
case IDC_7:
strcat (source, "7");
SetWindowTextA (hInput, source);
break;
case IDC_8:
strcat (source, "8");
SetWindowTextA (hInput, source);
break;
case IDC_9:
strcat (source, "9");
SetWindowTextA (hInput, source);
break;
case IDC_ADD:
strcat (source, "+");
SetWindowTextA (hInput, source);
break;
case IDC_SUB:
strcat (source, "-");
SetWindowTextA (hInput, source);
break;
case IDC_MUL:
strcat (source, "*");
SetWindowTextA (hInput, source);
break;
case IDC_DIV:
strcat (source, "/");
SetWindowTextA (hInput, source);
break;
case IDC_DOT:
strcat (source, ".");
SetWindowTextA (hInput, source);
break;
case IDC_DEL:
source [strlen(source)-1] = '\0';
SetWindowTextA (hInput, source);
break;
case IDC_CLEAR:
strcpy (source, "\0");
SetWindowTextA (hInput, "0");
SetWindowTextA (hOut, "0");
break;
case IDC_LEFT_BRACKET:
strcat (source, "(");
SetWindowTextA (hInput, source);
break;
case IDC_RIGHT_BRACKET:
strcat (source, ")");
SetWindowTextA (hInput, source);
break;
case IDC_VALUE:
if (head != NULL)
{
destroy(head);//销毁表达式树
head = NULL;
}
if (!check1(source))
{
MessageBox (hDlg, TEXT("括号不匹配"), TEXT("提示"), 0);
break;
}
if (!check2(source))
{
MessageBox (hDlg, TEXT("表达式错误"), TEXT("提示"), 0);
break;
}
convert(source);
postfix(source);
head = Create_Tree (&head, source);
cal_result = cal(head);
sprintf (dest,"%.4lf",cal_result);
SetWindowTextA (hOut, dest);
break;
case IDCANCEL:
if (head != NULL)
{
destroy(head);
head = NULL;
}
DestroyWindow (hDlg);
// hwnd = NULL;
break;
}
return true;
case WM_CLOSE:
DestroyWindow (hDlg);
// hwnd = NULL;
break;
}
return DefWindowProc (hDlg, message, wParam, lParam) ;
}
while (GetMessage (&msg, NULL, 0, 0))
{
if (!TranslateAccelerator (hwnd, hAccel, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
while (GetMessage(&msg, NULL, NULL, NULL))
{
if (!IsWindow(hwnd) || !IsDialogMessage(hwnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}