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

📄 romberdlg.cpp

📁 采用Romberg积分方法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// RomberDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Romber.h"
#include "RomberDlg.h"
#include <math.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRomberDlg dialog

CRomberDlg::CRomberDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRomberDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRomberDlg)
	m_strFunction = _T("");
	m_fLowerLimit = 0.0f;
	m_fUpperLimit = 0.0f;
	m_fRange_X = 0.0f;
	m_fRange_Y = 0.0f;
	m_nScaleNum_X = 0;
	m_nScaleNum_Y = 0;
	m_dRombergError = 0.0;

	m_bFillRect = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);


	pDC = NULL;
}

void CRomberDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRomberDlg)
	DDX_Text(pDX, IDC_EDIT_function, m_strFunction);
	DDX_Text(pDX, IDC_EDIT_LowerLimit, m_fLowerLimit);
	DDX_Text(pDX, IDC_EDIT_UpperLimit, m_fUpperLimit);
	DDX_Text(pDX, IDC_Range_X, m_fRange_X);
	DDX_Text(pDX, IDC_Range_Y, m_fRange_Y);
	DDX_Text(pDX, IDC_ScaleNum_X, m_nScaleNum_X);
	DDX_Text(pDX, IDC_ScaleNum_Y, m_nScaleNum_Y);
	DDX_Text(pDX, IDC_EDIT_RombergError, m_dRombergError);
	DDX_Check(pDX, IDC_RectFill, m_bFillRect);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRomberDlg, CDialog)
	//{{AFX_MSG_MAP(CRomberDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_DRAWMAP, OnDrawmap)
	ON_BN_CLICKED(IDC_DefaultSet, OnDefaultSet)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRomberDlg message handlers

BOOL CRomberDlg::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
	
	// TODO: Add extra initialization here

	m_strFunction.Format( " sin(x)/x ");
	m_fLowerLimit = 1;
	m_fUpperLimit = 5.0;
	m_dRombergError = 0.5e-7;
	m_dRombergValue = 0;
// 	Romberg( m_dRombergError,m_fUpperLimit,m_fLowerLimit, m_dRombergValue);

	m_fRange_X = 10.0;
	m_nScaleNum_X = 5;
	m_fRange_Y = 10.0;
	m_nScaleNum_Y = 5;
	m_bFillRect = TRUE;


	FontScale.CreateFont(13,
				    0,
					0,
					0,
					FW_BOLD,//FW_NORMAL//FW_HEAVY//FW_MEDIUM
	                FALSE,
					FALSE,
					FALSE,
					ANSI_CHARSET,
					OUT_DEFAULT_PRECIS,
					CLIP_DEFAULT_PRECIS,
					DEFAULT_QUALITY,
					DEFAULT_PITCH|FF_ROMAN,
					"Times New Roman");

	UpdateData( FALSE );


	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CRomberDlg::Romberg(double Error, double up_limit, double low_limit, double &res)
{
	double *T,*S,*C,*R;
	T = new double [100];
	S = new double [100];
	C = new double [100];
	R = new double [100];

	int T_index,S_index,C_index,R_index;
	int n = 0;
	T_index = 0;
	S_index = 0;
	C_index = 0;
	R_index = 0;

	bool m_bOk_C = 0;
	bool m_bOk_R = 0;

	int i=0;
	for ( i=0;i<4;i++)
	{
		n = int(pow(2,i));
		T[T_index] = Get_T_N(n,low_limit,up_limit);
		T_index ++;
	}
	for ( S_index=0;S_index<3;S_index++ )
	{
		S[S_index] = 4./3*T[S_index+1]-1./3*T[S_index];
	}
	for ( C_index=0;C_index<2;C_index++ )
	{
		C[C_index] = 16./15*S[C_index+1]-1./15*S[C_index];
	}
	for ( R_index=0;R_index<1;R_index++ )
	{
		R[R_index] = 64./63*C[R_index+1]-1./63*C[R_index];
	}

	double temp = 0;

	for ( ;(m_bOk_C==0)&&(m_bOk_R==0); )
	{
		temp = 1./63*fabs(C[C_index-1]-C[C_index-2]);

		if ( temp<Error )
		{
			m_bOk_C = 1;
		}
		else
		{
			n = 2*n;
			T[T_index] = Get_T_N(n,low_limit,up_limit);
			T_index++;
			S[S_index] = 4./3*T[S_index+1]-1./3*T[S_index];
			S_index++;
			C[C_index] = 16./15*S[C_index+1]-1./15*S[C_index];
			C_index++;
			R[R_index] = 64./63*C[R_index+1]-1./63*C[R_index];
			R_index++;
			temp = 1./255*fabs(R[R_index-1]-R[R_index-2]);
			if ( temp<Error )
			{
				m_bOk_R = 1;
			}
		}
	}

	res = R[R_index-1];

	if ( T != NULL )
	{
		delete []T;
		T = NULL;
	}
	if ( S != NULL )
	{
		delete []S;
		S = NULL;
	}
	if ( C != NULL )
	{
		delete []C;
		C = NULL;
	}
	if ( R != NULL )
	{
		delete []R;
		R = NULL;
	}
}

double CRomberDlg::Get_T_N(int n, double low, double up)
{
	double sum = 0;
	double h = (up - low)/n;
	int k = 0;
	for ( k=0;k<n;k++ )
	{
		sum += (h/2)*(fun(low+k*h)+fun(low+(k+1)*h));
	}
	return sum;
}

double CRomberDlg::fun(double x)
{

	//函数的更改在这个地方,记为修改1
	
	return ( sin(x) );

}

void CRomberDlg::OnDrawmap() 
{
	// TODO: Add your control notification handler code here
	// TODO: Add your message handler code here and/or call default

	//数据保护
	CString	strFunction = m_strFunction;
	float	fLowerLimit = m_fLowerLimit;
	float	fUpperLimit = m_fUpperLimit;
	float	fRange_X = m_fRange_X;
	float	fRange_Y = m_fRange_Y;
	int		nScaleNum_X = m_nScaleNum_X;
	int		nScaleNum_Y = m_nScaleNum_Y;
	double	dRombergError = m_dRombergError;
//	double	dRombergValue = m_dRombergValue;
	BOOL    bFillRect = m_bFillRect;

	UpdateData( TRUE );

	//函数的显示更改在这个地方,记为修改2
	m_strFunction.Format( "sin(x) ");


	UpdateData( FALSE );

	//错误检查




	//1函数检查
	if ( m_strFunction.IsEmpty() ==  TRUE )

⌨️ 快捷键说明

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