Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы2 / MOLab2 / MOLab2 / MOLab2Dlg
.cpp// MOLab1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "MOLab2.h"
#include "MOLab2Dlg.h"
#include "GraphicDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CMODlg dialog
CMODlg::CMODlg(CWnd* pParent /*=NULL*/)
: CDialog(CMODlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bThinking = FALSE;
m_pThinkDlg = NULL;
}
void CMODlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_FUNCTION, m_Function);
DDX_Control(pDX, IDC_METHOD, m_Method);
}
BEGIN_MESSAGE_MAP(CMODlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_CBN_SELCHANGE(IDC_FUNCTION, OnCbnSelchangeFunction)
ON_MESSAGE(WM_CALCULATE_END, OnCalculateEnd)
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
ON_BN_CLICKED(IDC_GRAPHIC, OnBnClickedGraphic)
END_MESSAGE_MAP()
// CMODlg message handlers
BOOL CMODlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
GetDlgItem(IDC_FUNCTION_EDIT)->SetWindowText("f(x1, x2) = a*x1+b*x2+exp(c*x1+d*x2)");
CString str;
for (int i = 1; i <= 10; i++)
{
str.Format("%d", i);
m_Function.AddString(str);
}
m_Function.SetCurSel(0);
m_Method.AddString("Градиентный метод с постоянным шагом");
m_Method.AddString("Градиентный метод с дроблением шага");
m_Method.AddString("Метод наискорейшего спуска");
m_Method.AddString("Метод покоординатного спуска с постоянным шагом");
m_Method.AddString("Метод Гауса - Зейделя");
m_Method.AddString("Эвристический алгоритм");
m_Method.AddString("Овражный метод I");
m_Method.AddString("Овражный метод II");
m_Method.AddString("Метод конфигураций");
m_Method.AddString("Метод конфигураций (модифицированный)");
m_Method.AddString("Метод симплекса");
// m_Method.AddString("Метод деформируемого симплекса");
m_Method.SetCurSel(0);
SetVariant(1);
m_pFindThread = (CFindThread*)AfxBeginThread(RUNTIME_CLASS(CFindThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
if (!m_pFindThread)
{
TRACE("Could not create thread\n");
SendMessage(WM_CLOSE);
return TRUE;
}
m_pFindThread->m_hParentWnd = this->m_hWnd;
m_pFindThread->m_bAutoDelete = TRUE;
m_pFindThread->ResumeThread();
return TRUE; // return TRUE unless you set the focus to a control
}
void CMODlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CMODlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMODlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMODlg::OnBnClickedOk()
{
if (m_bThinking)
return;
m_bThinking = TRUE;
GetDlgItem(IDOK)->EnableWindow( FALSE );
GetDlgItem(IDC_GRAPHIC)->EnableWindow( FALSE );
CDPoint pt;
m_nLastMethod = m_Method.GetCurSel() + 1;
m_nLastFunction = m_Function.GetCurSel() + 1;
m_pFindThread->PostThreadMessage(WM_CALCULATE_START,
m_Method.GetCurSel() + 1,
m_Function.GetCurSel() + 1);
if (NULL != m_pThinkDlg)
return;
m_pThinkDlg = new CThinkingDialog(this);
m_pThinkDlg->Create(IDD_THINKING_DIALOG, this);
m_pThinkDlg->ShowWindow(SW_SHOW);
}
void CMODlg::SetVariant(int nVariant)
{
CString str;
int pos = nVariant;
str.Format("%0.0f", X_DEFAULTS[pos][0]);
GetDlgItem(IDC_A_EDIT)->SetWindowText(str);
str.Format("%0.1f", X_DEFAULTS[pos][1]);
GetDlgItem(IDC_B_EDIT)->SetWindowText(str);
str.Format("%0.2f", X_DEFAULTS[pos][2]);
GetDlgItem(IDC_C_EDIT)->SetWindowText(str);
str.Format("%0.2f", X_DEFAULTS[pos][3]);
GetDlgItem(IDC_D_EDIT)->SetWindowText(str);
str.Format("(%0.1f, %0.1f)", X_DEFAULTS[pos][4], X_DEFAULTS[pos][5]);
GetDlgItem(IDC_START_EDIT)->SetWindowText(str);
str.Format("%0.1e", X_DEFAULTS[pos][6]);
GetDlgItem(IDC_EPS_EDIT)->SetWindowText(str);
}
void CMODlg::OnCbnSelchangeFunction()
{
int pos = m_Function.GetCurSel() + 1;
SetVariant(pos);
}
LRESULT CMODlg::OnCalculateEnd(WPARAM wParam, LPARAM lParam)
{
CString str;
CDPoint pt = m_pFindThread->GetResult ();
int nF = m_pFindThread->GetFSteps ();
int nF1 = m_pFindThread->GetF1Steps();
int nK = m_pFindThread->GetSteps ();
str.Format("(%f, %f)", pt.x[0], pt.x[1]);
GetDlgItem(IDC_X_EDIT)->SetWindowText(str);
str.Format("%f", pt.y);
GetDlgItem(IDC_Y_EDIT)->SetWindowText(str);
str.Format("%d", nF);
GetDlgItem(IDC_NF_EDIT)->SetWindowText(str);
str.Format("%d", nF1);
GetDlgItem(IDC_NF1_EDIT)->SetWindowText(str);
str.Format("%d", nF + nF1);
GetDlgItem(IDC_SUM_EDIT)->SetWindowText(str);
str.Format("%d", nK);
GetDlgItem(IDC_ITER_EDIT)->SetWindowText(str);
GetDlgItem(IDOK)->EnableWindow( TRUE );
GetDlgItem(IDC_GRAPHIC)->EnableWindow( TRUE );
if (NULL != m_pThinkDlg)
{
m_pThinkDlg->Exit();
m_pThinkDlg->DestroyWindow();
delete m_pThinkDlg;
m_pThinkDlg = NULL;
}
m_bThinking = FALSE;
return NULL;
}
void CMODlg::OnBnClickedCancel()
{
if (NULL != m_pThinkDlg)
{
m_pThinkDlg->Exit();
m_pThinkDlg->DestroyWindow();
delete m_pThinkDlg;
m_pThinkDlg = NULL;
}
m_pFindThread->PostThreadMessage(WM_QUIT, NULL, NULL);
::WaitForSingleObject(*m_pFindThread, INFINITE);
OnCancel();
}
void CMODlg::OnBnClickedGraphic()
{
CGraphicDlg dlg;
dlg.m_Path = m_pFindThread->GetPath();
dlg.m_Lines = m_pFindThread->GetLines();
CString str;
CString strMethodName;
m_Method.GetLBText(m_nLastMethod - 1, strMethodName);
str.Format("Вариант %d - ", m_nLastFunction);
str += strMethodName;
dlg.DoModal(str);
}
Соседние файлы в папке MOLab2