Home   Cover Cover Cover Cover
 

Anonyme Methoden

A05.cs
//==========================================================
// Anonymous methods
//==========================================================

using System;

// computes a function y = f(x)
delegate double Function(double x);

class A05 {
  
  // draws a character plot of function f
  static void Plot(Function f) {
    //--- create a 50 x 50 drawing plane
    char[,] dot = new char[50, 50];
    for (int i = 0; i < dot.GetLength(0); i++)
      for (int j = 0; j < dot.GetLength(1); j++)
        dot[i, j] = ' ';
    //--- compute the function
    int height = dot.GetLength(0);
    for (int x = 0; x < dot.GetLength(1); x++) {
      int y = (int)f(x);
      if (0 <= y && y < height) {
        dot[(height-1) - y, x] = '*';
      }
    }
    //--- draw the plot
    for (int i = 0; i < dot.GetLength(0); i++) {
      Console.Write('|');
      for (int j = 0; j < dot.GetLength(1); j++)
        Console.Write(dot[i, j]);
      Console.WriteLine();
    }
    for (int i = 0; i < dot.GetLength(1); i++)
      Console.Write('-');
  }
  
  public static void Main() {
    Plot(delegate (double x) { return 2 * x - 10; }); Console.WriteLine();
    Plot(delegate (double x) { return x * x / 100; }); Console.WriteLine();
    Plot(delegate (double x) { return x * x / 10 - 2 * x; }); Console.WriteLine();
  }
}