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

📄 calculatedlg.cpp

📁 用于数学上的一些常用的基本计算,可以作为开发其他大型程序的基础算法.
💻 CPP
字号:
// CalculateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Calculate.h"
#include "CalculateDlg.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCalculateDlg dialog

CCalculateDlg::CCalculateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCalculateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalculateDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCalculateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculateDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculateDlg, CDialog)
	//{{AFX_MSG_MAP(CCalculateDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalculateDlg message handlers

BOOL CCalculateDlg::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 CCalculateDlg::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 CCalculateDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCalculateDlg::OnButton1() 
{
	double X1,X2;
	int looptime=0;
	X2=X1=0.5;
	do{
		looptime++;
		
		X2=powf(2,-X1);
		
		if(fabs(X2-X1)<0.00001)
			break;
		else
			X1=X2;
	}while(true);
	CString str;
	str.Format("迭代%d次,X=%f",looptime,X2);
	AfxMessageBox(str);
}

void CCalculateDlg::OnButton2() 
{
	double X1,X2;
	int looptime=0;
	X2=X1=0.5;
	do{
		looptime++;
		
		X2=X1-(X1-powf(2,-X1))/(1+X1*powf(2,-X1-1));
		
		if(fabs(X2-X1)<0.00001)
			break;
		else
			X1=X2;
	}while(true);
	CString str;
	str.Format("迭代%d次,X=%f",looptime,X2);
	AfxMessageBox(str);
}

void CCalculateDlg::OnButton3() 
{
	double X1,X2,X3;
	int looptime=0;
	X1=0.5;
	X2=1.5;
	do{
		looptime++;
		X3=X2-(X2-X1)*(X2-powf(2,-X2))/(X2-powf(2,-X2)-X1+powf(2,-X1));
		if(fabs(X3-X2)<0.00001)
			break;
		else
		{
			X1=X2;
			X2=X3;
		}
	}while(true);
	CString str;
	str.Format("迭代%d次,X=%f",looptime,X3);
	AfxMessageBox(str);
}

void CCalculateDlg::OnButton4() 
{
	
	double X1,X2,Y1,Z1;
	int looptime=0;
	X1=X2=0.5;
	do{
		looptime++;
		Y1=powf(2,-X1);
		Z1=powf(2,-Y1);
		X2=X1-powf(Y1-X1,2)/(Z1-2*Y1+X1);
		if(fabs(X2-X1)<0.00001)
			break;
		else
			X1=X2;
	}while(true);
	CString str;
	str.Format("迭代%d次,X=%f",looptime,X2);
	AfxMessageBox(str);
}

void CCalculateDlg::OnButton5() 
{
	float y[6];
	float r;
	memset(y,0,sizeof(float)*6);
	y[0]=1;
	int i=0;
	CString str;
	CString msg="";
	for(float x=0;x<0.6;x+=0.1)
	{
		r=x+expf(-x);
		y[i+1]=y[i]+0.1*(x-y[i]+1);
		r=fabs(r-y[i]);
		str.Format("y[%d]=%f    r=%f\r\n",i,y[i],r);
		i++;
		msg+=str;
	}
	AfxMessageBox(msg);
}

void CCalculateDlg::OnButton6() 
{
	float y[6];
	float T1,T2;
	float r;
	memset(y,0,sizeof(float)*6);
	y[0]=1;
	int i=0;
	CString str;
	CString msg="";
	for(float x=0;x<0.6;x+=0.1)
	{
		r=x+expf(-x);
		T1=y[i]+0.1*(x-y[i]+1);
		T2=y[i]+0.1*((x+0.1)-T1+1);
		y[i+1]=(T1+T2)/2;
		r=fabs(r-y[i]);
		str.Format("y[%d]=%f    r=%f\r\n",i,y[i],r);
		i++;
		msg+=str;
	}
	AfxMessageBox(msg);
}

void CCalculateDlg::OnButton7() 
{
	float y[6];
	float K1,K2,K3,K4;
	float r;
	memset(y,0,sizeof(float)*6);
	y[0]=1;
	int i=0;
	CString str;
	CString msg="";
	for(float x=0;x<0.6;x+=0.1)
	{
		r=x+expf(-x);
		K1=x-y[i]+1;
		K2=(x+(float)(0.1/2))-(y[i]+K1*(float)(0.1/2))+1;
		K3=(x+(float)(0.1/2))-(y[i]+K2*(float)(0.1/2))+1;
		K4=(x+0.1)-(y[i]+K3*0.1)+1;
		y[i+1]=y[i]+(float)(0.1*(K1+2*K2+2*K3+K4)/6);
		r=fabs(r-y[i]);
		str.Format("y[%d]=%f    r=%f\r\n",i,y[i],r);
		i++;
		msg+=str;
	}
	AfxMessageBox(msg);
}

⌨️ 快捷键说明

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