Program.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. namespace ConsoleApp2
  3. {
  4. class Program
  5. {
  6. delegate double Function(double x);
  7. static void Main(string[] args)
  8. {
  9. double[] a = { 0.0, 0.5, 1.0 };
  10. //double[] squares = Apply(a, Square);
  11. //double[] sines = Apply(a, Math.Sin);
  12. double[] squares = Apply(a, new Function(Square));
  13. double[] sines = Apply(a, new Function(Math.Sin));
  14. Multiplier m = new Multiplier(2.0);
  15. double[] doubles = Apply(a, m.Multiply);
  16. }
  17. static double Square(double x)
  18. {
  19. return x * x;
  20. }
  21. static double[] Apply(double[] a, Function f)
  22. {
  23. double[] result = new double[a.Length];
  24. for (int i = 0; i < a.Length; i++) {
  25. result[i] = f(a[i]);
  26. }
  27. return result;
  28. }
  29. }
  30. class Multiplier
  31. {
  32. double factor;
  33. public Multiplier(double factor)
  34. {
  35. this.factor = factor;
  36. }
  37. public double Multiply(double x)
  38. {
  39. return x * factor;
  40. }
  41. }
  42. }