- •Лабораторная работа №4. Работа с элементом управления «Окно списка» (ListBox)
- •Лабораторная работа №6. Элементы управления «Шкала индикации» (Progress) и «Маркер» (Slider). Модальные диалоговые окна
- •Лабораторная работа №7. Sdi – приложение. Работа с данными в архитектуре «Документ/представление» («Document/view»)
Лабораторная работа №1. Мастера Сlassview и Сlasswizard в Visual C++ 6.0
Задание
14. .
Решение:
// x.cpp: implementation of the x class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "zadacha_1.h"
#include "x.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
x::x()
{}
x::~x()
{
}
/* обьявление методов */
void x::run() // метод для вычесления значений z1 и z2
{
z1=((m+2)/sqrt(2*m)+2/(m-sqrt(2*m))-m/(sqrt(2*m)+2))*(sqrt(m)-sqrt(2))/(m+2);
z2=1/(sqrt(m)+sqrt(2));
}
void x::print() // метод для вывода результатов
{
CString str;
/* объявили строковый объект класса библиотеки MFC, и при помощи его вызвали метод,
который работает как стандартная функция printf(…), но выводит значения объектов из
списка вывода в указанную строку*/
str.Format("Для m=%10.5f \n z1=%10.5f, z2=%10.5f",m, z1, z2);
AfxMessageBox(str);
}
void x::set(double a1) // метод для инициализации класса
{m=a1;
AfxMessageBox("Объект создан и проинициализирован!");
}
// zadacha_1Dlg.cpp : implementation file
#include "stdafx.h"
#include "zadacha_1.h"
#include "zadacha_1Dlg.h"
#include "x.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CZadacha_1Dlg dialog
CZadacha_1Dlg::CZadacha_1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CZadacha_1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CZadacha_1Dlg)
m_m = 0.0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CZadacha_1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CZadacha_1Dlg)
DDX_Text(pDX, IDC_EDIT1, m_m);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CZadacha_1Dlg, CDialog)
//{{AFX_MSG_MAP(CZadacha_1Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CZadacha_1Dlg message handlers
BOOL CZadacha_1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CZadacha_1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CZadacha_1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CZadacha_1Dlg::OnButton1()
{
UpdateData(TRUE); // данные считываються из элементов управления диалогового окна
//и обновляються переменные оконного объекта
x x1; // Конструктор класса x создает объект x1
x1.set(m_m); // инициализация данных
x1.run(); // вычисление
x1.print(); // вывод
UpdateData(FALSE); // обновляет содержимое элементов управления диалогового окна
}
Лабораторная работа №4. Работа с элементом управления «Окно списка» (ListBox)
Задание
14.
Решение:
// lab4Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "lab4.h"
#include "lab4Dlg.h"
#include "math.h"
#define pi 3.1415926535897932384626433832795
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLab4Dlg dialog
CLab4Dlg::CLab4Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CLab4Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLab4Dlg)
m_x1 = 0.0;
m_x2 = 0.0;
m_h = 0.0;
m_n = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLab4Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLab4Dlg)
DDX_Control(pDX, IDC_LIST1, m_l);
DDX_Text(pDX, IDC_EDIT1, m_x1);
DDV_MinMaxDouble(pDX, m_x1, 0.1, 0.3);
DDX_Text(pDX, IDC_EDIT2, m_x2);
DDV_MinMaxDouble(pDX, m_x2, 0.7, 0.9);
DDX_Text(pDX, IDC_EDIT3, m_h);
DDV_MinMaxDouble(pDX, m_h, 0.1, 0.2);
DDX_Text(pDX, IDC_EDIT4, m_n);
DDV_MinMaxInt(pDX, m_n, 5, 20);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLab4Dlg, CDialog)
//{{AFX_MSG_MAP(CLab4Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLab4Dlg message handlers
BOOL CLab4Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLab4Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLab4Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int i;
double x,s,y;
CString str;
void CLab4Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(true);
str.Format(" x1=%.3lf, x2=%.3lf, h=%.3lf, n=%3d ", m_x1, m_x2, m_h, m_n);
m_l.AddString(str);
UpdateData(false);
for (x=m_x1;x<=m_x2;x=x+m_h)
{
s=0;
for(i=1;i<=m_n;i++)
{
s+=pow(-1,i)*cos(i*x)/(i*i);
}
y=(x*x-pi*pi/3)/4;
str.Format("Для x= %.3lf, сумма= %.3lf, y= %.3lf", x,s,y);
m_l.AddString(str);
UpdateData(FALSE);
}
}
void CLab4Dlg::OnButton2()
{
// TODO: Add your control notification handler code here
m_l.ResetContent();
}