③ 如果f(a)f(m)<0,则对区间[a,m]调用函数自身(递归),或将m赋值给b(循环),跳转①,
如果f(m)f(b)<0,则对区间[m,b]调用函数自身(递归),或将m赋值给a(循环),跳转①。 (2)牛顿法
xk?1?xk?{//x为初值x0,e为容许误差,返回值为方程的解
f(xk),x0已知
f'(xk) //每次得到的近似解存入顺序表中,以备后面画图之用
(3)快速弦截法
xk?1?xk?f(xk)xk?xk?1,x0和x1已知
f(xk)?f(xk?1){//x0,x1分别为前两个近似解,用于启动迭代过程,e为容许误差,返回值为方程的解 //将每次迭代结果存入顺序表中,以备后面画图之用 }
也可以定义多个待求解方程为函数,在二分法、牛顿法和弦截法的函数中增加函数指针参数,用以调用不同的待求解方程。
4.4、实验结果
// ThreeDlg.cpp : implementation file //
#include \ #include \ #include \ #include \
#ifdef _DEBUG
#define new DEBUG_NEW #endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx { public:
// Dialog Data
36
CAboutDlg();
#ifdef AFX_DESIGN_TIME
// Implementation protected: };
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX) { }
void CAboutDlg::DoDataExchange(CDataExchange* pDX) { }
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP()
// CThreeDlg dialog
CThreeDlg::CThreeDlg(CWnd* pParent /*=NULL*/) { }
double CThreeDlg::fun(double x) { }
double CThreeDlg::dy(double x) { }
return 1 - 2 * cos(x); return x - 2 * sin(x);
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); : CDialogEx(IDD_THREE_DIALOG, pParent) CDialogEx::DoDataExchange(pDX); DECLARE_MESSAGE_MAP() protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support enum { IDD = IDD_ABOUTBOX }; #endif
37
int CThreeDlg::parseInt(CString mStr) { }
void CThreeDlg::DoDataExchange(CDataExchange* pDX) { }
BEGIN_MESSAGE_MAP(CThreeDlg, CDialogEx)
// CThreeDlg message handlers
BOOL CThreeDlg::OnInitDialog() {
CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL)
// IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000);
// Add \ CDialogEx::OnInitDialog(); ON_WM_SYSCOMMAND() ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CThreeDlg::OnBnClickedButton1) ON_BN_CLICKED(IDC_BUTTON2, &CThreeDlg::OnBnClickedButton2) ON_BN_CLICKED(IDC_BUTTON3, &CThreeDlg::OnBnClickedButton3) ON_BN_CLICKED(IDC_BUTTON4, &CThreeDlg::OnBnClickedButton4) ON_BN_CLICKED(IDC_BUTTON5, &CThreeDlg::OnBnClickedButton5) ON_BN_CLICKED(IDC_BUTTON6, &CThreeDlg::OnBnClickedButton6) CDialogEx::DoDataExchange(pDX); int x = 0;
for (int i = 0; i < mStr.GetLength(); i++) { } return x;
x *= 10;
x += (mStr.GetAt(i) - '0');
END_MESSAGE_MAP()
38
}
{ }
// 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
BOOL bNameValid; CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty()) { }
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
void CThreeDlg::OnSysCommand(UINT nID, LPARAM 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 CThreeDlg::OnPaint() {
if (IsIconic()) {
CPaintDC dc(this); // device context for painting if ((nID & 0xFFF0) == IDM_ABOUTBOX) { } else { }
CDialogEx::OnSysCommand(nID, lParam); CAboutDlg dlgAbout; dlgAbout.DoModal();
39
}
// The system calls this function to obtain the cursor to display while the user drags // the minimized window.
HCURSOR CThreeDlg::OnQueryDragIcon() { }
void CThreeDlg::OnBnClickedButton1() {
// TODO: 在此添加控件通知处理程序代码 CDC *p = GetDC(); int dl = 30; p->MoveTo(50, 250); p->LineTo(450, 250); p->MoveTo(100, 300); p->LineTo(100, 50 - dl); //绘制箭头 int d = 5;
p->MoveTo(100, 50 - dl);
p->LineTo(100 + d, 50 - dl + d); p->MoveTo(100, 50 - dl);
p->LineTo(100 - d, 50 - dl + d); return static_cast
CDialogEx::OnPaint(); // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
// 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;
SendMessage(WM_ICONERASEBKGND, reinterpret_cast
40