⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 code8.cpp

📁 differentation and integration
💻 CPP
字号:
#include "Code8.h"

CMyWinApp MyApplication;

BOOL CMyWinApp::InitInstance()
{
    CCode8* pFrame = new CCode8;
    m_pMainWnd = pFrame;
    pFrame->ShowWindow(SW_SHOW);
    pFrame->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CCode8,CFrameWnd)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
    ON_BN_CLICKED(IDC_BUTTON,OnButton)
END_MESSAGE_MAP()

CCode8::CCode8()
{
	Create(NULL,"Code8: Differentiation and Integration",
		WS_OVERLAPPEDWINDOW,CRect(0,0,800,635),NULL);
	Arial80.CreatePointFont(80,"Arial");
	pt=new PT [M+1];
	fMenu=0; fStatus=0; idc=301;
	curve.hm=CPoint(320,320); curve.end=CPoint(760,590);
	curve.rc=CRect(curve.hm.x-10,curve.hm.y-10,curve.end.x+10,curve.end.y+10);
	menu[1].item="Differentiation"; 
	menu[2].item="Integration";
	for (int i=1;i<=nMenuItems;i++)
	{
		menu[i].hm=CPoint(20,30+(i-1)*30);
		menu[i].rc=CRect(menu[i].hm.x,menu[i].hm.y,
			menu[i].hm.x+150,menu[i].hm.y+20);
	}
	input[0].hm=CPoint(200,10);
	input[0].rc=CRect(input[0].hm.x,input[0].hm.y,
		input[0].hm.x+560,input[0].hm.y+260);
	for (i=1;i<=maxInput;i++)
		input[i].hm=CPoint(input[0].hm.x+10,input[0].hm.y+30+(i-1)*25);
}

CCode8::~CCode8()
{	
	delete pt;
}

void CCode8::OnPaint()
{
	CPaintDC dc(this);
	CString str;
	dc.SelectObject(Arial80);
	dc.SetBkColor(RGB(150,150,150));
	dc.SetTextColor(RGB(255,255,255));
	for (int i=1;i<=nMenuItems;i++)
	{
		dc.FillSolidRect(&menu[i].rc,RGB(150,150,150));
		dc.TextOut(menu[i].hm.x+5,menu[i].hm.y+5,menu[i].item);
	}
	dc.SetBkColor(RGB(255,255,255));
	dc.SetTextColor(RGB(100,100,100));
	dc.Rectangle(input[0].rc);
	dc.TextOut(input[0].hm.x+150,input[0].hm.y+5,menu[fMenu].item);
	if (fMenu!=0)
	{
		for (i=1;i<=nInput;i++)
			dc.TextOut(input[i].hm.x+10,input[i].hm.y,input[i].label);
		if (fStatus)
		{
			if (fMenu==1)
				DrawCurve();
			if (fMenu==2)
			{
				str.Format("Trapezium: %lf",aTrap); 
				dc.TextOut(curve.hm.x,curve.hm.y+50,str);
				str.Format("Simpson's: %lf",aSimp); 
				dc.TextOut(curve.hm.x,curve.hm.y+70,str);
				str.Format("Simpson's 3/8: %lf",aSimp38);
				dc.TextOut(curve.hm.x,curve.hm.y+90,str);
				str.Format("Gauss-Legendre's 3-point: %lf",aGL);
				dc.TextOut(curve.hm.x,curve.hm.y+110,str);
			}
		}
	}
}

void CCode8::DrawCurve()
{
	CClientDC dc(this);
	int i;
	double m1,m2,c1,c2;
	CString str;
	CPoint px;
	if (fMenu==1)
	{
		left.x=pt[0].x;
		right.x=pt[m].x;
	}
	max.y=pt[0].y; min.y=pt[0].y;
	max.yd1=pt[1].yd1; min.yd1=pt[1].yd1;
	max.yd2=pt[1].yd2; min.yd2=pt[1].yd2;
	for (i=0;i<=m;i++)
	{
		if (max.y<pt[i].y)
			max.y=pt[i].y;	
		if (min.y>pt[i].y)
			min.y=pt[i].y;	
		if (i>0 && i<m)
		{
			if (max.yd1<pt[i].yd1)
				max.yd1=pt[i].yd1;
			if (min.yd1>pt[i].yd1)
				min.yd1=pt[i].yd1;
			if (max.yd2<pt[i].yd2)
				max.yd2=pt[i].yd2;
			if (min.yd2>pt[i].yd2)
				min.yd2=pt[i].yd2;
		}
	}

	// Cartesian-Windows conversion coordinates
	m1=(double)(curve.end.x-curve.hm.x)/(right.x-left.x);
	c1=(double)curve.hm.x-left.x*m1;
	m2=(double)(curve.hm.y-curve.end.y)/(max.y-min.y);
	c2=(double)curve.end.y-min.y*m2;

	// Draw & label the x,y axis
	CPen pGray(PS_SOLID,1,RGB(100,100,100));
	dc.SelectObject(pGray);
	dc.SelectObject(Arial80);
	
	str.Format("%.0lf",left.x); dc.TextOut(px.x,px.y,str);
	str.Format("%.0lf",right.x); dc.TextOut(px.x-10,px.y,str);

	// draw the main curve
	CPen pCurve1(PS_SOLID,2,RGB(0,250,0));
	dc.SelectObject(pCurve1);
	for (i=0;i<=m;i++)
	{
		px=CPoint((int)(m1*pt[i].x+c1),(int)(m2*pt[i].y+c2));
		if (i==0)
			dc.MoveTo(px);
		else
			dc.LineTo(px);
		if (pt[i].y==max.y)
		{
			str.Format("%.6lf",max.y); 
			dc.TextOut(px.x,px.y-10,str);
		}
		if (pt[i].y==min.y)
		{
			str.Format("%.6lf",min.y); 
			dc.TextOut(px.x,px.y,str);
		}
	}
	// draw the first derivative curve
	CPen pCurve2(PS_SOLID,2,RGB(250,0,0));
	dc.SelectObject(pCurve2);
	m2=(double)(curve.hm.y-curve.end.y)/(max.yd1-min.yd1);
	c2=(double)curve.end.y-min.yd1*m2;
	for (i=0;i<=m;i++)
	{
		px=CPoint((int)(m1*pt[i].x+c1),(int)(m2*pt[i].yd1+c2));
		if (i==1)
			dc.MoveTo(px);
		if (i>1 && i<m)
			dc.LineTo(px);
		if (pt[i].yd1==max.yd1)
		{
			str.Format("%.6lf",max.yd1); 
			dc.TextOut(px.x,px.y-10,str);
		}
		if (pt[i].yd1==min.yd1)
		{
			str.Format("%.6lf",min.yd1); 
			dc.TextOut(px.x,px.y,str);
		}
	}
	// draw the second derivative curve
	CPen pCurve3(PS_SOLID,2,RGB(0,0,250));
	dc.SelectObject(pCurve3);
	m2=(double)(curve.hm.y-curve.end.y)/(max.yd2-min.yd2);
	c2=(double)curve.end.y-min.yd2*m2;
	for (i=0;i<=m;i++)
	{
		px=CPoint((int)(m1*pt[i].x+c1),(int)(m2*pt[i].yd2+c2));
		if (i==1)
			dc.MoveTo(px);
		if (i>1 && i<m)
			dc.LineTo(px);
		if (pt[i].yd2==max.yd2)
		{
			str.Format("%.6lf",max.yd2); 
			dc.TextOut(px.x,px.y-10,str);
		}
		if (pt[i].yd2==min.yd2)
		{
			str.Format("%.6lf",min.yd2); 
			dc.TextOut(px.x,px.y,str);
		}
	}
}

void CCode8::OnLButtonDown(UINT nFlags,CPoint pt)
{
	for (int k=1;k<=nMenuItems;k++)		// menu items
		if (menu[k].rc.PtInRect(pt))
		{
			fMenu=k;
			fStatus=0;
			InvalidateRect(input[0].rc);
			InvalidateRect(curve.rc);
			btn.DestroyWindow();
			table.DestroyWindow();
			btn.Create("Compute",WS_CHILD| WS_VISIBLE| BS_DEFPUSHBUTTON,
				CRect(CPoint(input[0].hm.x+10,input[0].hm.y+5),
				CSize(100,20)),this,IDC_BUTTON);
			switch(k)
			{
				case 1: 
					nInput=4; 
					input[1].label="y=f(x)"; 
					input[2].label="h";
					input[3].label="x[0]"; 
					input[4].label="x[m]";
					break;
				case 2: 
					nInput=4;
					input[1].label="y=f(x)"; 
					input[2].label="m";
					input[3].label="x[0]"; 
					input[4].label="x[m]";
					break;
			}
			for (int i=1;i<=maxInput-1;i++)
				input[i].ed.DestroyWindow();
			for (i=1;i<=nInput;i++)
				input[i].ed.Create(WS_CHILD | WS_VISIBLE | WS_BORDER, 
					CRect(input[i].hm.x+100,input[i].hm.y,
					input[i].hm.x+520,input[i].hm.y+20),this,idc++);
		}
}

void CCode8::OnButton()
{
	CString str;
	if (fMenu!=0)
	{
		for (int i=1;i<=nInput;i++)
			input[i].ed.GetWindowText(input[i].item);
		if (fMenu==1)
			Differentiation();
		if (fMenu==2)
			Integration();
		if (fStatus)
		{
			ShowTable();
			InvalidateRect(curve.rc);
		}
	}
}

void CCode8::Differentiation()
{
	int i,psi[6];
	double psv[6];
	CString str;
	fStatus=1;
	pt[0].x=atof(input[3].item);
	h=atof(input[2].item);
	m=(int)(atof(input[4].item)-atof(input[3].item))/h;
	m=((m<M)?m:M);
	pt[m].x=pt[0].x+(double)m*h;
	psi[1]=23; psv[1]=pt[0].x; 
	pt[0].y=parse(input[1].item,1,psv,psi);
	for (i=0;i<=m;i++)
		if (i<m)
		{
			pt[i+1].x=pt[i].x+h;
			psv[1]=pt[i+1].x; 
			pt[i+1].y=parse(input[1].item,1,psv,psi);
			if (i>0)
			{
				pt[i].yd1=(pt[i+1].y-pt[i-1].y)/(2*h);
				pt[i].yd2=(pt[i+1].y-2*pt[i].y+pt[i-1].y)/(h*h);
			}
		}
}

void CCode8::Integration()
{
	int i,psi[2];
	double psv[2];
	aTrap=0; aSimp=0; aSimp38=0; aGL=0;
	fStatus=1;
	pt[0].x=atof(input[3].item);
	m=atoi(input[2].item); m=((m<M)?m:M);
	h=(atof(input[4].item)-atof(input[3].item))/(double)m;
	pt[m].x=pt[0].x+(double)m*h;
	psi[1]=23;
	for (i=0;i<=m;i++)
	{
		psv[1]=pt[i].x; 
		pt[i].y=parse(input[1].item,1,psv,psi);
		if (i>0 && i<m)
		{
			aTrap += 2*pt[i].y;
			if (m%2==0)
			{
				if (i%2==1)
					aSimp += 4*pt[i].y;
				if (i%2==0)
					aSimp += 2*pt[i].y;
			}
			if (m%3==0)
			{
				if (i%3==0)
					aSimp38 += 2*pt[i].y;
				else
					aSimp38 += 3*pt[i].y;
			}
		}
		if (i<m)
			pt[i+1].x=pt[i].x+h;
	}
	aTrap += pt[0].y+pt[m].y;
	aTrap *= h/2;
	if (m%2==0)
	{
		aSimp += pt[0].y+pt[m].y;
		aSimp *= h/3;
	}
	if (m%3==0)
	{
		aSimp38 += pt[0].y+pt[m].y;
		aSimp38 *= 3*h/8;
	}
	double g1,g2,g3,t1,t2,t3,a,b;
	a=pt[0].x; b=pt[m].x; 
	t1=-0.774597; psv[1]=((b-a)*t1+(b+a))/2;
	g1=(b-a)/2*parse(input[1].item,1,psv,psi);
	t2=0; psv[1]=((b-a)*t2+(b+a))/2;
	g2=(b-a)/2*parse(input[1].item,1,psv,psi);
	t3=0.774597; psv[1]=((b-a)*t3+(b+a))/2;
	g3=(b-a)/2*parse(input[1].item,1,psv,psi);
	aGL=5*g1/9+8*g2/9+5*g3/9;
}

void CCode8::ShowTable()
{
	CString str;
	CPoint hTable=CPoint(20,310);
	CRect rcTable=CRect(hTable.x,hTable.y,hTable.x+280,hTable.y+290);
	table.DestroyWindow();
    table.Create(WS_VISIBLE| WS_CHILD| WS_DLGFRAME| LVS_REPORT
        | LVS_NOSORTHEADER,rcTable,this,idc++);
	table.InsertColumn(0,"i",LVCFMT_CENTER,25);
	table.InsertColumn(1,"x",LVCFMT_CENTER,50);
	table.InsertColumn(2,"y=f(x)",LVCFMT_CENTER,60);
	if (fMenu==1)
	{
		table.InsertColumn(3,"yd1=f'(x)",LVCFMT_CENTER,60);
		table.InsertColumn(4,"yd2=f''(x)",LVCFMT_CENTER,60);
	}
	for (int i=0;i<=m;i++)
	{
		str.Format("%d",i); table.InsertItem(i,str,0);
		str.Format("%.4lf",pt[i].x); table.SetItemText(i,1,str);
		str.Format("%.4lf",pt[i].y); table.SetItemText(i,2,str);
		if (fMenu==1)
			if (i>0 && i<m)
			{
				str.Format("%.4lf",pt[i].yd1); table.SetItemText(i,3,str);
				str.Format("%.4lf",pt[i].yd2); table.SetItemText(i,4,str);
			}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -