实验指导(C++面向对象部分) 联系客服

发布时间 : 星期五 文章实验指导(C++面向对象部分)更新完毕开始阅读

return DefWindowProc (hwnd, message, wParam, lParam) ; // 执行默认的消息处理 }

return 0; }

LRESULT CALLBACK MyDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {

switch (message) {

case WM_INITDIALOG: return TRUE;

case WM_COMMAND:

if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {

EndDialog(hDlg, LOWORD(wParam)); return TRUE; }

break; }

return FALSE; }

实验题2.参考程序 #include #include #include

// 求一元二次方程的根,函数返回根的个数 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) {

// 计算方程的根

float a = 2.0, b = 6.0, c = 3.0; double root[2];

int n = GetRoot(a, b, c, root); if (n<1)

strcpy(str, \方程无根!\ else

sprintf(str,\方程的解为:%f, %f\

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 = \ // 窗口类名 if (!RegisterClass (&wndclass)) // 注册窗口 {

MessageBox (NULL, \窗口注册失败!\ return 0 ; }

// 创建窗口

hwnd = CreateWindow (\ // 窗口类名

\我的窗口\ // 窗口标题

WS_OVERLAPPEDWINDOW, // 窗口样式

CW_USEDEFAULT, // 窗口最初的 x 位置

CW_USEDEFAULT, // 窗口最初的 y 位置

CW_USEDEFAULT, // 窗口最初的 x 大小

CW_USEDEFAULT, // 窗口最初的 y 大小

NULL, // 父窗口句柄 NULL, // 窗口菜单句柄

hInstance, // 应用程序实例句柄

NULL) ; // 创建窗口的参数

ShowWindow (hwnd, nCmdShow) ; // 显示窗口 UpdateWindow (hwnd) ; // 更新窗口,包括窗口的客户区 // 进入消息循环:当从应用程序消息队列中检取的消息是WM_QUIT时,则退出循环。

while (GetMessage (&msg, NULL, 0, 0)) {

TranslateMessage (&msg) ; // 转换某些键盘消息

DispatchMessage (&msg) ; // 将消息发送给窗口过程,这里是WndProc }

return msg.wParam ; }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

HDC hdc; PAINTSTRUCT ps; switch (message) {

case WM_CREATE: // 窗口创建产生的消息 return 0 ; case WM_PAINT:

hdc = BeginPaint(hwnd, &ps);

TextOut(hdc, 10, 10, str, strlen(str)); EndPaint(hwnd, &ps); return 0 ;

case WM_DESTROY: // 当窗口关闭时产生的消息 PostQuitMessage (0) ; return 0 ; }

return DefWindowProc (hwnd, message, wParam, lParam) ; // 执行默认的消息处理 }

实验题3参考程序

#include #include #include

// 求一元二次方程的根,函数返回根的个数 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 ;