Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

1-курс / aaa / Kursak 33+ / Kursak / KursakDlg

.cpp
Скачиваний:
31
Добавлен:
23.03.2015
Размер:
22.86 Кб
Скачать
яю// KursakDlg.cpp : implementation file

//



#include "stdafx.h"

#include "Kursak.h"

#include "KursakDlg.h"

#include <math.h>

#include <stdio.h>







#ifdef _DEBUG

#define new DEBUG_NEW

#endif





// CKursakDlg dialog



	CEdit* pEdit_min_x;			// <0AA82 C:070B5;59 =0 Edit Control 4;O <8=8<0;L=>3> 7=0G5=8O X

	CEdit* pEdit_min_y;			// <0AA82 C:070B5;59 =0 Edit Control 4;O <8=8<0;L=>3> 7=0G5=8O X

	CEdit* pEdit_max_x;			// <0AA82 C:070B5;59 =0 Edit Control 4;O <0:A8<0;L=>3> 7=0G5=8O X

	CEdit* pEdit_max_y;			// <0AA82 C:070B5;59 =0 Edit Control 4;O <0:A8<0;L=>3> 7=0G5=8O Y

	CEdit* pEdit_grid;			// <0AA82 C:070B5;59 =0 Edit Control 4;O 7=0G5=8O A5B:8

	CEdit* pEdit_num;			// <0AA82 C:070B5;59 =0 Edit Control 4;O 7=0G5=8O G8A;0 48A:@5B=KE 7=0G5=89 3@0D8:0

	CEdit* pEdit_ei;

	CEdit* pEdit_n;

	

	double* x;					// <0AA82 A> 7=0G5=8O<8 x

	double* y_integral;			// <0AA82 A> 7=0G5=8O<8 DC=:F88 ?>AG8B0=K<8 ?> 8=B53@0;L=><C ?@54AB02;5=8N

	double* y_ryad;				// <0AA82 A> 7=0G5=8O<8 DC=:F88 ?>AG8B0=K<8 ?> @07;>65=8N 2 @O4

	float xMin=1, xMax=10, yMin=0, yMax=3;	// 7=0G5=8O, >3@0=8G820NI85 >1;0ABL

	int Num = 100;				// G8A;> 48A:@5B=KE 7=0G5=89 3@0D8:0

	char strBuffer[20];			// 2A?><030B5;L=K9 B5:AB>2K9 1CD5@

	

	float ei = 0.001;			 // B>G=>ABL 28G8A;5=89 4;O 8=B53@0;L=>3> ?@54AB02;5=8O

	int N = 0;



	#define X_EXT 1000			// @>BO65==>ABL >B>1@065=8O ?> X

	#define Y_EXT 1000			// @>BO65==>ABL >B>1@065=8O ?> Y

	int GRIDNUM=10;				//  07<5@ A5B:8

	#define PI       3.14159265358979323846 



	double X;



long double pows(long double, long double);				

long double r(long double);					// $C=:F8O @07;>65=8O 2 @O4

long double fact(int);			// $C=:F8O D0:B>@80;0

	// $C=:F8O 8=B53@0;L=>3> ?@54AB02;5=8O

long double f(long double);			// $C=:F8O DC=:F88 2 8=B53@0;5

/* @>B>B8? DC=   F88, 2KG8A;ONI59 8=B53@0; */

float integral(float a, float b, float e);





CKursakDlg::CKursakDlg(CWnd* pParent /*=NULL*/)

	: CDialog(CKursakDlg::IDD, pParent)



{

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}



void CKursakDlg::DoDataExchange(CDataExchange* pDX)

{

	CDialog::DoDataExchange(pDX);

}



BEGIN_MESSAGE_MAP(CKursakDlg, CDialog)

	ON_WM_PAINT()

	ON_WM_QUERYDRAGICON()

	//}}AFX_MSG_MAP

	ON_BN_CLICKED(IDC_BUTTON1, &CKursakDlg::OnBnClickedButton1)

//	ON_WM_SHOWWINDOW()

//ON_WM_UPDATEUISTATE()

END_MESSAGE_MAP()





// CKursakDlg message handlers



BOOL CKursakDlg::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



	// 2K45;5=8O ?0<OB8 ?>4 C:070B5;8

	x = new double[Num]; 

	y_integral = new double[Num];

	y_ryad = new double[Num];



	pEdit_min_x = new CEdit[2];

	pEdit_max_x = new CEdit[2];

	pEdit_min_y = new CEdit[2];

	pEdit_max_y = new CEdit[2];

	



	



	// =8F80;878@>20=85 <0AA82>2 C:070B5;59 =0 A>>B25BAB2CNI85 M;5<5=BK C?@02;5=8O 

	pEdit_min_x = (CEdit*)GetDlgItem(IDC_MINX);

	pEdit_max_x = (CEdit*)GetDlgItem(IDC_MAXX);

	pEdit_min_y = (CEdit*)GetDlgItem(IDC_MINY);

	pEdit_max_y = (CEdit*)GetDlgItem(IDC_MAXY);

	pEdit_grid = (CEdit*)GetDlgItem(IDC_GRID);

	pEdit_num = (CEdit*)GetDlgItem(IDC_NUM);



	pEdit_ei = (CEdit*)GetDlgItem(IDC_EI);







	// 0?>;=5=85 M;5<5=B>2 C?@02;5=8O Edit Control 7=0G5=8O<8 ?> C<>;G0=8N 

	sprintf( strBuffer,"%g", xMin );

	pEdit_min_x->SetWindowText((CString) strBuffer);

	sprintf( strBuffer,"%g", xMax );

	pEdit_max_x->SetWindowText((CString) strBuffer);

	sprintf( strBuffer,"%g", yMin );

	pEdit_min_y->SetWindowText((CString) strBuffer);

	sprintf( strBuffer,"%g", yMax );

	pEdit_max_y->SetWindowText((CString) strBuffer);

	sprintf( strBuffer,"%i", GRIDNUM );

	pEdit_grid->SetWindowText((CString) strBuffer);

	sprintf( strBuffer,"%i", Num );

	pEdit_num->SetWindowText((CString) strBuffer);

	sprintf( strBuffer,"%f", ei );

	pEdit_ei->SetWindowText((CString) strBuffer);



	

	return TRUE;  

}



void CKursakDlg::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 CKursakDlg::OnQueryDragIcon()

{

	return static_cast<HCURSOR>(m_hIcon);

}





void CKursakDlg::OnBnClickedButton1()

{

	



	// !G8BK20=85 7=0G5=89 ?0@0<5B@>2 ?@>3@0<<K, 7040==KE 2 M;5<5=B0E C?@02;5=8O

	pEdit_min_x->GetWindowText((LPTSTR) strBuffer,20);

	sscanf(strBuffer, "%f", &xMin );

	pEdit_max_x->GetWindowText((LPTSTR) strBuffer,20);

	sscanf( strBuffer, "%f", &xMax );

	pEdit_min_y->GetWindowText((LPTSTR) strBuffer,20);

	sscanf( strBuffer, "%f", &yMin );

	pEdit_max_y->GetWindowText((LPTSTR) strBuffer,20);

	sscanf( strBuffer, "%f", &yMax );

	pEdit_grid->GetWindowText((LPTSTR) strBuffer,20);

	sscanf( strBuffer, "%i", &GRIDNUM );

	pEdit_num->GetWindowText((LPTSTR) strBuffer,20);

	sscanf( strBuffer, "%i", &Num );



	pEdit_ei->GetWindowText((LPTSTR) strBuffer,20);

	sscanf( strBuffer, "%f", &ei );





	// ?@545;5=85 :>=B5:AB=>3> CAB@>9AB20 , A :>B>@K< A2O70= 3@0D8G5A:89 2K2>4

	CWnd* pGraphicRegion = GetDlgItem(IDC_STATIC);



	// !>740=85 :>=B5:AB=>3> CAB@>9AB20 4;O 2K2>40

	CClientDC gdc(pGraphicRegion);



	// ?@545;5=85 @568<0 >B>1@065=8O :>>@48=0B 

	gdc.SetMapMode(MM_ANISOTROPIC);



	// ?@545;5=85 ?@O<>C3>;L=8:0 GraphRect, 2=CB@8 :>B>@>3> 1C45B 2K2>48BLAO 3@0D8:

	CRect GraphRect;

	pGraphicRegion->GetClientRect(&GraphRect);

	GraphRect.right -= 2;		GraphRect.top += 15;

	GraphRect.OffsetRect( 1, -1 );



	// 0;82:0 >1;0AB8 345 1C45B =0E>48BAO 3@0D8:

	CBrush SysBrush;

	SysBrush.CreateSysColorBrush(  COLOR_3DFACE);

	gdc.SelectObject( &SysBrush );

	gdc.FillRect( GraphRect, &SysBrush );



	// ?@545;5=85 >1;0AB8 2K2>40 2 D878G5A:8E :>>@48=0B0E

	int xViewportExt = (int)(0.9*GraphRect.Width());

	int yViewportExt = (int)(0.9*GraphRect.Height());

	gdc.SetViewportExt( xViewportExt, yViewportExt );

	int xViewportOrg = (int)(0.09*GraphRect.Width());

	int yViewportOrg = (int)(0.97*GraphRect.Height());

	gdc.SetViewportOrg( xViewportOrg, yViewportOrg );





	// ?@545;5=85 ;>38G5A:>9 A8AB5<K :>>@48=0B 4;O 3@0D8:0

	gdc.SetWindowExt(X_EXT, -Y_EXT) ;

	int xWindowOrg = (int)(xMin*X_EXT/(xMax-xMin));

	int yWindowOrg = (int)(yMin*Y_EXT/(yMax-yMin));

	gdc.SetWindowOrg( xWindowOrg, yWindowOrg );

	

	// 0;82:0 >1;0AB8 3@0D8:0 15;K< F25B>< 

	CBrush GraphBrush( RGB(255, 255, 255) );

	gdc.SelectObject( &GraphBrush );

	GraphRect.SetRect(xWindowOrg,yWindowOrg+Y_EXT,xWindowOrg+X_EXT,yWindowOrg);

	gdc.FillRect( GraphRect, &GraphBrush );





	// >43>B>2:0 ?5@0 4;O @8A>20=8O ;8=89 A5B:8

	CPen penGrid( PS_SOLID, 1, RGB(0, 0, 0) );

	gdc.SelectObject(&penGrid);



	// >43>B>2:0 AB@>:>2>3> 1CD5@0

	char StringBuffer[100];

	// !>740=85 8 8=8F80;878@>20=85 H@8DB0

	CFont font;

	font.CreatePointFont( (int)(Y_EXT/3),(CString) "Times New Roman" );

	// ?@545;5=85 F25B0 D>=0 4;O 2K2>48<KE =04?8A59

	gdc.SelectObject( &font );

	gdc.SetBkColor( RGB(231,231,231) );

	



	double xVal;

	double yVal;



	// &8:; 4;O ?@>2545=89 ;8=89 A5B:8 8 ?>4?8A59 : =8<

     for(int xPos, yPos, j = 0; j <= GRIDNUM; j++)   {

		xVal = xMin + j*(xMax-xMin)/GRIDNUM;

		xPos = (int)(xVal*X_EXT/(xMax-xMin));

		sprintf( strBuffer,"%g", xVal);

		gdc.TextOut( xPos - (int)(0.035*X_EXT), (int)((yMin/(yMax-yMin)-0.03)*Y_EXT), (CString) strBuffer  );

		gdc.MoveTo( xPos, (int)(yMin*Y_EXT/(yMax-yMin)) );

		gdc.LineTo( xPos, (int)(yMin*Y_EXT/(yMax-yMin))+Y_EXT );

		yVal = yMin + j*(yMax-yMin)/GRIDNUM;

		yPos = (int)(yVal*Y_EXT/(yMax-yMin));

		sprintf( strBuffer,"%g", yVal);

		gdc.TextOut( (int)((xMin/(xMax-xMin)-0.09)*X_EXT), yPos + (int)(0.02*Y_EXT), (CString) strBuffer );

		gdc.MoveTo( (int)(xMin*X_EXT/(xMax-xMin)), yPos );

		gdc.LineTo((int)(xMin*X_EXT/(xMax-xMin))+X_EXT, yPos );



	     }



	// BA5G5=85 ;8=89, 2KE>4OI8E 70 3@0=8FK >1;0AB8 3@0D8:0

	gdc.LPtoDP( &GraphRect );

	CRgn GraphRgn;

	GraphRgn.CreateRectRgnIndirect( &GraphRect );

	gdc.SelectClipRgn( &GraphRgn );





	

	// KG8A;5=85 <0AA82>2 7=0G5=89 DC=:F89

	  for(int j = 0; j <= Num; j++)   {

	

		x[j] = xMin + j*(xMax-xMin)/Num;	// KG8A;5=85 7=0G5=89 x

		

		

		y_integral[j]=integral(1,x[j],ei);	// KG8A;5=85 7=0G5=89 8=B53@0;L=>3> ?@54AB02;5=8O

		y_ryad[j] = r(x[j]);				// KG8A;5=85 7=0G5=89 @07;>65=89 2 @O4





	}





	 // #AB0=>2:0 ?5@0 4;O ?@>@8A>2:8 @07;>65=89 2 @O4

     CPen penGraph( PS_SOLID, 12, RGB(0, 0, 255) );

     gdc.SelectObject(&penGraph);

     gdc.MoveTo( (int)(x[0]*X_EXT/(xMax-xMin)), (int)(y_ryad[0]*Y_EXT/(yMax-yMin)) ); // #AB0=>2:0 =0G0;L=>9 B>G:8



	 for(int j = 0; j <= Num; j++) {

	  gdc.LineTo((int)(x[j]*X_EXT/(xMax-xMin)),(int)(y_ryad[j]*Y_EXT/(yMax-yMin)));	// @>@8A>2:0 ;8=88

	 }



	 // #AB0=>2:0 ?5@0 4;O ?@>@8A>2:8 8=B53@0;L=>3> ?@54AB02;5=8O

	 CPen penGraph2( PS_SOLID, 6, RGB(255, 0, 0) );

     gdc.SelectObject(&penGraph2);

     gdc.MoveTo( (int)(x[0]*X_EXT/(xMax-xMin)), (int)(y_integral[0]*Y_EXT/(yMax-yMin)) ); // #AB0=>2:0 =0G0;L=>9 B>G:8



	 for(int j = 0; j <= Num; j++) {

	  gdc.LineTo((int)(x[j]*X_EXT/(xMax-xMin)),(int)(y_integral[j]*Y_EXT/(yMax-yMin))); // @>@8A>2:0 ;8=88

	 }





}







long double Fact(int x) // $C=:F8O 28G8A;5=8O D0:B>@80;

{

long double xx;

xx=1;



for (int i=2; i<=x; i++) {

xx*= i;

}

return xx;

}



long double pows(long double x, long double y) 

{

	if (x == 0) {

		return 0;

	} else if (y > 0) {

		return pow(x,y);

	} else if (y < 0) {

		return 1.0/pow(x,y);

	} else {

		return 1;

	}



}



// =B53@0;L=>5 ?@54AB02;5=85

long double f(long double t)  // 8G8A;5=8O DC=:F88 8=B53@0;0

{

	if (pows(t * t - 1, (long double)0.5) != 0) {

		return 1.0/pows(t * t - 1, (long double)0.5); 

	} else {



		return 0;

	}



}



/*  50;870F8O DC=   F88, 2KG8A;ONI59 8=B53@0; */

float integral(float a, float b, float e)

{

    long double n =2, i; /* 0G0;L=>5 G8A;> @07185=89 */

    long double s_ab = f(a) + f(b); /* !C<<0 7=0G5=89 DC=   F88 2 a 8 b */

	long double h = (b - a) / n; /* KG8A;O5< H03 */

    long double s_k = 0,  s_m;

    long double s_x = 0, s_x0;

 

    /* !C<<0 7=0G5=89 DC=   F88 2 =5G5B=KE B>G:0E */

    for (i = 2; i < n; i += 2) {

        s_k += f(a + i * h);    

}

do {

        s_m = 0;

        s_x0 = s_x;

        

/* !C<<0 7=0G5=89 DC=   F88 2 G5B=KE B>G:0E */

    for (i = 1; i < n; i += 2) {

            s_m += f(a + i * h);

}

    /* >4AG5B @57C;LB0B0 */

        s_x = h / 3 * (s_ab + 2 * s_k + 4 * s_m);

/* 715305< 45;5=8O =0 =>;L */

        if (s_x == 0) s_x = e;

		

        s_k += s_m;

        n *= 2;

        h /= 2;

} while (fabs((s_x - s_x0) / 15) > e);/* K?>;=OBL 4> B5E  ?>@, ?>:0 @57C;LB0B =5 1C45B C4>2;5B2>@OBL 4>?CAB8<>9 >H81:5 */



return (s_x); 

}







long double r(long double x) // 8G8A;5=8O ?> @07;>65=8N 2 @O4

{

return  log(x + pow(x * x -1,(long double)0.5));



}





Соседние файлы в папке Kursak