码迷,mamicode.com
首页 > Windows程序 > 详细

C# - 委托_求定积分通用方法

时间:2015-05-24 17:05:42      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 委托_例子
 8 {
 9     static class Program
10     {
11         // 定义委托(Double类型)
12         delegate double Integand(double x);
13 
14         //定义方法
15         static double Method1(double x)
16         {
17             return 2 * x + 1;
18         }
19 
20         static double Method2(double x)
21         {
22             return x * x;
23         }
24 
25         // 定义是用委托的方法
26         static double DefiniteIntegrate(Integand f, double a, double b)
27         {
28             const int sect = 1000;
29 
30             double area = 0;
31 
32             double delta = (b - a) / sect;
33 
34             for (int i = 0; i < sect; i++)
35             {
36                 //此处的 f 就代表 Method1 或是 Method2。
37                 //传给他们一个double类型的值,返回一个double类型的值。
38                 //此时的double值就是长乘以宽“a + i * delta”
39                 area += delta * f(a + i * delta);
40             }
41 
42             return area;
43         }
44 
45         static void Main(string[] args)
46         {
47             //调用方法
48             //传入一个方法
49             Console.WriteLine(Program.DefiniteIntegrate(Method1, 1, 5));
50             Console.WriteLine();
51             Console.WriteLine(Program.DefiniteIntegrate(Method2, 0, 1));
52 
53             Console.ReadKey();
54         }
55     }
56 }
57                     

 

C# - 委托_求定积分通用方法

标签:

原文地址:http://www.cnblogs.com/KTblog/p/4525930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!