D:\SixCocos2d-xVC2012\Cocos2d-x\XWH\cstest>Program
input array size n(0-50):3
input n numbers:a[1]=4
a[2]=2
a[3]=3
min:2,max:4.
array after sorted:
2 3 4
D:\SixCocos2d-xVC2012\Cocos2d-x\XWH\cstest>Program 0
input array size n(0-50):3
input n numbers:a[1]=5
a[2]=3
a[3]=4
min:5,max:3.
array after sorted:
5 4 3
// C#中的泛型方法与Lambda表达式
using System;
namespace MyExample
{
class Generic
{
public static void swap(ref T t1,ref T t2)
{
T t = t1;
t1=t2;
t2=t;
}
public static void sort(T[] MyArray,Func
{
for(int i=0;i
if(myFunc(MyArray[i],MyArray[j])){
swap(ref MyArray[i],ref MyArray[j]);
}
};
}
}
class Program
{
static void Main(string[] args)
{
Generic
Console.Write("input array size n(0-50):");
string s = Console.ReadLine();
int n = int.Parse(s);
Console.Write("input n numbers:");
int[] MyArray = new int[n];
for(int i=0;i
Console.Write("a[{0}]=", i + 1);
string s1 =Console.ReadLine();
MyArray[i] = int.Parse(s1);
}
Func
if(args.Length>0){
myFunc=(x,y)=>x
Generic
Console.WriteLine("min:{0},max:{1}.\narray after sorted:", MyArray[0], MyArray[n - 1]);
try
{
foreach (int i in MyArray)
{
Console.Write(i.ToString() + "\t");
}
}
catch (Exception e) { Console.WriteLine(e.ToString()); }
Console.WriteLine("\n");
}
}
}
// C#中的Func泛型委托与Lambda表达式
/* D:\cstest>csc /d:USE_DELEGATE,USE_LAMBDA Integral.cs
D:\cstest>Integral
result1 = 28.016
result2 = 0.3338335
result3 = 1.46265176390149
D:\cstest>csc /d:USE_DELEGATE Integral.cs
D:\cstest>Integral
result1 = 28.016
result2 = 0.3338335
result3 = 1.46265176390149
D:\cstest>csc Integral.cs
D:\cstest>Integral
result1 = 28.016
result2 = 0.3338335
result3 = 1.46265176390149 */
using System;
namespace HanMath
{
class Integral
{
//被积函数
static double F1(double x)
{
return 2 * x + 1;
}
//被积函数
static double F2(double x)
{
return x * x;
}
//被积函数
static double F3(double x)
{
return Math.Exp(x * x);
}
//被积函数的委托
#if USE_DELEGATE
delegate double Integrand(double x);
//函数:定积分
static double DefiniteIntegrate(double a, double b, Integrand f)
#else
static double DefiniteIntegrate(double a, double b, Func
#endif
{
const int sect = 1000; //分割数目
double delta = (b - a) / sect;
double area = 0;
for (int i = 1; i <= 1000; i++)
{
area += delta * f(a + i * delta);
}
return area;
}
// 变步长辛普生法
// a为积分下限;b为积分上限;eps为精度要求.
#if USE_DELEGATE
static double bcsimp(Integrand bcsimpf, double a, double b, double eps)
#else
static double bcsimp(Func
#endif
{
int n, k;
double h, t1, t2, s1, s2, ep, p, x;
s2 = 0;
n = 1; h = b - a;
t1 = h * (bcsimpf(a) + bcsimpf(b)) / 2.0;
s1 = t1;
ep = eps + 1.0;
while (ep >= eps)
{
p = 0.0;
for (k = 0; k <= n - 1; k++)
{
x = a + (k + 0.5) * h;
p = p + bcsimpf(x);
}
t2 = (t1 + h * p) / 2.0;
s2 = (4.0 * t2 - t1) / 3.0;
ep = Math.Abs(s2 - s1);
t1 = t2; s1 = s2; n = n + n; h = h / 2.0;
}
return s2;
}
//进行定积分运算
static void Main(string[] args)
{
#if USE_DELEGATE
#if USE_LAMBDA
Integrand f1 = new Integrand(x=>2*x+1);
Integrand f2 = new Integrand(x=>x*x);
Integrand f3 = new Integrand(x=>Math.Exp(x*x));
#else
Integrand f1 = new Integrand(F1);
Integrand f2 = new Integrand(F2);
Integrand f3 = new Integrand(F3);
#endif
#else
Func
Func
Func
#endif
double result1 = DefiniteIntegrate(1, 5, f1);
double result2 = DefiniteIntegrate(0, 1, f2);
Console.WriteLine("result1 = {0}", result1);
Console.WriteLine("result2 = {0}", result2);
double result3 = bcsimp(f3, 0.0, 1.0, 0.000001);
Console.WriteLine("result3 = {0}", result3);//C/C++:1.462652e+000
}
}
}