第一范文网 - 专业文章范例文档资料分享平台

实验指导(C++面向对象部分)

来源:用户分享 时间:2025/5/23 21:29:01 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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 ;

搜索更多关于: 实验指导(C++面向对象部分) 的文档
实验指导(C++面向对象部分).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c9v0k89sirp7yogl1iu0k_9.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top