IntegrallerDlg.cpp
上传用户:weigute
上传日期:2007-03-02
资源大小:1287k
文件大小:8k
源码类别:

数学计算

开发平台:

Visual C++

  1. // IntegrallerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Integraller.h"
  5. #include "IntegrallerDlg.h"
  6. #include "Integral.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CIntegrallerDlg dialog
  51. CIntegrallerDlg::CIntegrallerDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CIntegrallerDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CIntegrallerDlg)
  55. // NOTE: the ClassWizard will add member initialization here
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CIntegrallerDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CIntegrallerDlg)
  64. // NOTE: the ClassWizard will add DDX and DDV calls here
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CIntegrallerDlg, CDialog)
  68. //{{AFX_MSG_MAP(CIntegrallerDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  73. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  74. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  75. ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  76. ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
  77. ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
  78. ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
  79. ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
  80. ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
  81. //}}AFX_MSG_MAP
  82. END_MESSAGE_MAP()
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CIntegrallerDlg message handlers
  85. BOOL CIntegrallerDlg::OnInitDialog()
  86. {
  87. CDialog::OnInitDialog();
  88. // Add "About..." menu item to system menu.
  89. // IDM_ABOUTBOX must be in the system command range.
  90. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  91. ASSERT(IDM_ABOUTBOX < 0xF000);
  92. CMenu* pSysMenu = GetSystemMenu(FALSE);
  93. if (pSysMenu != NULL)
  94. {
  95. CString strAboutMenu;
  96. strAboutMenu.LoadString(IDS_ABOUTBOX);
  97. if (!strAboutMenu.IsEmpty())
  98. {
  99. pSysMenu->AppendMenu(MF_SEPARATOR);
  100. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  101. }
  102. }
  103. // Set the icon for this dialog.  The framework does this automatically
  104. //  when the application's main window is not a dialog
  105. SetIcon(m_hIcon, TRUE); // Set big icon
  106. SetIcon(m_hIcon, FALSE); // Set small icon
  107. // TODO: Add extra initialization here
  108. return TRUE;  // return TRUE  unless you set the focus to a control
  109. }
  110. void CIntegrallerDlg::OnSysCommand(UINT nID, LPARAM lParam)
  111. {
  112. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  113. {
  114. CAboutDlg dlgAbout;
  115. dlgAbout.DoModal();
  116. }
  117. else
  118. {
  119. CDialog::OnSysCommand(nID, lParam);
  120. }
  121. }
  122. // If you add a minimize button to your dialog, you will need the code below
  123. //  to draw the icon.  For MFC applications using the document/view model,
  124. //  this is automatically done for you by the framework.
  125. void CIntegrallerDlg::OnPaint() 
  126. {
  127. if (IsIconic())
  128. {
  129. CPaintDC dc(this); // device context for painting
  130. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  131. // Center icon in client rectangle
  132. int cxIcon = GetSystemMetrics(SM_CXICON);
  133. int cyIcon = GetSystemMetrics(SM_CYICON);
  134. CRect rect;
  135. GetClientRect(&rect);
  136. int x = (rect.Width() - cxIcon + 1) / 2;
  137. int y = (rect.Height() - cyIcon + 1) / 2;
  138. // Draw the icon
  139. dc.DrawIcon(x, y, m_hIcon);
  140. }
  141. else
  142. {
  143. CDialog::OnPaint();
  144. }
  145. }
  146. // The system calls this to obtain the cursor to display while the user drags
  147. //  the minimized window.
  148. HCURSOR CIntegrallerDlg::OnQueryDragIcon()
  149. {
  150. return (HCURSOR) m_hIcon;
  151. }
  152. // 变步长梯形求积法
  153. void CIntegrallerDlg::OnButton1() 
  154. {
  155. // 建立CIntegral的子类,在其中重载函数Func
  156. class CInteg : public CIntegral
  157. {
  158. double Func(double x)
  159. {
  160. return exp(-x*x);
  161. }
  162. };
  163. // 求解
  164. CInteg Integ;
  165. double dblValue = Integ.GetValueTrapezia(0.0, 1.0);
  166. // 显示结果
  167. CString s;
  168. s.Format("积分值 = %f", dblValue);
  169. AfxMessageBox(s, MB_OK|MB_ICONINFORMATION);
  170. }
  171. // 变步长辛卜生求积法
  172. void CIntegrallerDlg::OnButton2() 
  173. {
  174. // 建立CIntegral的子类,在其中重载函数Func
  175. class CInteg : public CIntegral
  176. {
  177. double Func(double x)
  178. {
  179. return log(1.0+x)/(1.0+x*x);
  180. }
  181. };
  182. // 求解
  183. CInteg Integ;
  184. double dblValue = Integ.GetValueSimpson(0.0, 1.0);
  185. // 显示结果
  186. CString s;
  187. s.Format("积分值 = %f", dblValue);
  188. AfxMessageBox(s, MB_OK|MB_ICONINFORMATION);
  189. }
  190. // 自适应梯形求积法
  191. void CIntegrallerDlg::OnButton3() 
  192. {
  193. // 建立CIntegral的子类,在其中重载函数Func
  194. class CInteg : public CIntegral
  195. {
  196. double Func(double x)
  197. {
  198. return 1.0/(1.0+25.0*x*x);
  199. }
  200. };
  201. // 求解
  202. CInteg Integ;
  203. double dblValue = Integ.GetValueATrapezia(-1.0, 1.0, 0.0001);
  204. // 显示结果
  205. CString s;
  206. s.Format("积分值 = %f", dblValue);
  207. AfxMessageBox(s, MB_OK|MB_ICONINFORMATION);
  208. }
  209. // 龙贝格求积法
  210. void CIntegrallerDlg::OnButton4() 
  211. {
  212. // 建立CIntegral的子类,在其中重载函数Func
  213. class CInteg : public CIntegral
  214. {
  215. double Func(double x)
  216. {
  217. return x/(4.0+x*x);
  218. }
  219. };
  220. // 求解
  221. CInteg Integ;
  222. double dblValue = Integ.GetValueRomberg(0.0, 1.0);
  223. // 显示结果
  224. CString s;
  225. s.Format("积分值 = %f", dblValue);
  226. AfxMessageBox(s, MB_OK|MB_ICONINFORMATION);
  227. }
  228. // 计算一维积分的连分式法
  229. void CIntegrallerDlg::OnButton5() 
  230. {
  231. // 建立CIntegral的子类,在其中重载函数Func
  232. class CInteg : public CIntegral
  233. {
  234. double Func(double x)
  235. {
  236. return exp(-x*x);
  237. }
  238. };
  239. // 求解
  240. CInteg Integ;
  241. double dblValue = Integ.GetValuePq(0.0, 4.3);
  242. // 显示结果
  243. CString s;
  244. s.Format("积分值 = %f", dblValue);
  245. AfxMessageBox(s, MB_OK|MB_ICONINFORMATION);
  246. }
  247. // 高振荡函数求积法
  248. void CIntegrallerDlg::OnButton6() 
  249. {
  250.     double s[2];
  251. double fa[4]={0.0,1.0,0.0,-3.0};
  252.     double fb[4]={6.2831852,1.0,-6.2831852,-3.0};
  253.     double a=0.0; 
  254. double b=6.2831852;
  255.     int m=30; 
  256. int n=4;
  257. // 求解
  258. CIntegral Integ;
  259. double dblValue = Integ.GetValuePart(a, b, m, n, fa, fb, s);
  260. // 显示结果
  261. CString ss;
  262. ss.Format("s(0) = %ens(1) = %e", s[0], s[1]);
  263. AfxMessageBox(ss, MB_OK|MB_ICONINFORMATION);
  264. }
  265. // 勒让德-高斯求积法
  266. void CIntegrallerDlg::OnButton7() 
  267. {
  268. // 建立CIntegral的子类,在其中重载函数Func
  269. class CInteg : public CIntegral
  270. {
  271. double Func(double x)
  272. {
  273. return x*x+sin(x);
  274. }
  275. };
  276. // 求解
  277. CInteg Integ;
  278. double dblValue = Integ.GetValueLegdGauss(2.5, 8.4);
  279. // 显示结果
  280. CString s;
  281. s.Format("积分值 = %f", dblValue);
  282. AfxMessageBox(s, MB_OK|MB_ICONINFORMATION);
  283. }
  284. // 拉盖尔-高斯求积法
  285. void CIntegrallerDlg::OnButton8() 
  286. {
  287. // 建立CIntegral的子类,在其中重载函数Func
  288. class CInteg : public CIntegral
  289. {
  290. double Func(double x)
  291. {
  292. return x*exp(-x);
  293. }
  294. };
  295. // 求解
  296. CInteg Integ;
  297. double dblValue = Integ.GetValueLgreGauss();
  298. // 显示结果
  299. CString s;
  300. s.Format("积分值 = %f", dblValue);
  301. AfxMessageBox(s, MB_OK|MB_ICONINFORMATION);
  302. }
  303. // 埃尔米特-高斯求积法
  304. void CIntegrallerDlg::OnButton9() 
  305. {
  306. // 建立CIntegral的子类,在其中重载函数Func
  307. class CInteg : public CIntegral
  308. {
  309. double Func(double x)
  310. {
  311. return x*x*exp(-x*x);
  312. }
  313. };
  314. // 求解
  315. CInteg Integ;
  316. double dblValue = Integ.GetValueHermiteGauss();
  317. // 显示结果
  318. CString s;
  319. s.Format("积分值 = %f", dblValue);
  320. AfxMessageBox(s, MB_OK|MB_ICONINFORMATION);
  321. }