using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;delegate double Function(double x);
class Multiplier
{
double factor;
public Multiplier(double factor)
{
this.factor = factor;
}
public double Multiply(double x)
{
return x * factor;
}
}class Test
{
static double Square(double x)
{
return x * x;
}
static double[] Apply(double[] a, Function f)
{
double[] result = new double[a.Length];
for (int i = 0; i <= a.Length-1; i++)
result[i] = f(a[i]);
return result;
} static void Main(string[] args)
{
double[] a = { 0, 5, 1, 0, 0, 0, 0, 0 };
double[] squares = Apply(a, new Function(Square));
double[] sines = Apply(a, new Function(Math.Sin));
Multiplier m = new Multiplier(2);
double[] doubles = Apply(a, new Function(m.Multiply));
}
}
------------------------------
for (int i = 0; i < a.Length; i++)这里越界了,0 a.length-1 是合理的范围
{
double[] result = new double[a.Length];
for (int i = 0; i <= a.Length; i++)
result[i] = f(a[i]);
return result;
}
-------------------------------------
改成
static double[] Apply(double[] a, Function f)
{
double[] result = new double[a.Length];
for (int i = 0; i < a.Length; i++)
result[i] = f(a[i]);
return result;
}