c语言练习
- 格式:doc
- 大小:34.50 KB
- 文档页数:3
#include <windows.h>
#include <math.h>
#include <stdio.h>
int GetRoot(float a, float b, float c, double *root)
{
double delta, deltasqrt;
delta = b*b - 4.0 * a * c;
if (delta<0.0) return 0;
deltasqrt = sqrt(delta);
if (a!=0.0) {
root[0] = (-b + deltasqrt)/(2.0 * a);
root[1] = (-b - deltasqrt)/(2.0 * a);
} else
if (b!=0.0) root[0] = root[1] = -c/b;
else return 0;
if (root[0] == root[1]) return 1;
else return 2;
}
char str[80];
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = "SDKWin";
if (!RegisterClass (&wndclass))
MessageBox (NULL, "窗口注册失败!", "HelloWin", 0) ; return 0 ;
}
hwnd = CreateWindow ("SDKWin",
"实验1——Windows编程基础",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL) ;
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static HWND hwndButton, hwndEdit;
char strEdit[80], strA[3][80], strHint[80];
float a[3];
double root[2];
int i, j, k, m;
switch (message)
{
case WM_CREATE:
hwndEdit = CreateWindow("edit", NULL, WS_CHILD|WS_VISIBLE|WS_ BORDER,
10, 60, 200, 25, hwnd, NULL, NULL, NULL );
hwndButton = CreateWindow("button", "计算",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
240, 60, 80, 25, hwnd, NULL, NULL, NULL );
return 0 ;
case WM_COMMAND:
if (((HWND)lParam == hwndButton )&&( HIWORD(wParam) == BN_CLICKED )){ GetWindowText( hwndEdit, strEdit, 80);
k = 0; m = 0;
for (j=0; j<80; j++){
if (strEdit[j] == ','){
k++; m = 0;
}
else {
strA[k][m] = strEdit[j]; m++;
}
}
for (i=0; i<3; i++)
a[i] = (float)atof(strA[i]);
int n = GetRoot(a[0], a[1], a[2], root);
if (n<1) strcpy(str, "方程无根!") ;
else sprintf(str,"方程的解为:%f, %f", root[0], root[1]); InvalidateRect(hwnd, NULL, TRUE);
}
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
strcpy(strHint, "请输入一元二次方程的3个系数,中间用逗号分隔"); TextOut(hdc, 10, 40, strHint, strlen(strHint));
TextOut(hdc, 10, 90, str, strlen(str));
EndPaint(hwnd, &ps);
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}。