Advanced  Services C#-Praktikum Back Next Up Home
Einfache Methoden (Lösung)
// ------------------------------ SimpleMethods.cs ------------------------------- \\

public class SimpleMethods
{
   static void Main(string[] args)
   {
      double a, b, c ;
      Random rd = new Random();

      a = rd.Next(100) ;
      b = rd.Next(100) ;
      c = rd.Next(100) ;

      Console.WriteLine("a = " +a);
      Console.WriteLine("b = " +b);
      Console.WriteLine("c = " +c);
      Console.WriteLine("max = " +  SimpleMethods.Max(a, b, c) );
      Console.WriteLine("min = " +  SimpleMethods.Min(a, b, c) );

   }  // end main


   public static int Max(int a, int b, int c)
   {
      int max=a ;

      if(b>max) max=b;
      if(c>max) max=c;

      return max ;
   }

   public static double Max(double a, double b, double c)
   {
      double max=a ;

      if(b>max) max=b;
      if(c>max) max=c;

      return max ;
   }

   public static int Min(int a, int b, int c)
   {
      int min=a ;

      if(b<min) min=b;
      if(c<min) min=c;

      return min ;
   }

   public static double Min(double a, double b, double c)
   {
      double min=a ;

      if(b<min) min=b;
      if(c<min) min=c;

      return min ;
   }

   public static int Max(int[] arr)
   {
      int max=arr[0] ;

      for(int i=1 ; i<arr.Length; i++)
         if( arr[i]>max) max=arr[i];

      return max ;
   }

   public static double Max(double[] arr)
   {
      double max=arr[0] ;

      for(int i=1 ; i<arr.Length; i++)
         if( arr[i]>max) max=arr[i];

      return max ;
   }

   public static int Min(int[] arr)
   {
      int min=arr[0] ;

      for(int i=1 ; i<arr.Length; i++)
         if( arr[i]<min) min=arr[i];

      return min ;
   }

   public static double Min(double[] arr)
   {
      double min=arr[0] ;

      for(int i=1 ; i<arr.Length; i++)
         if( arr[i]<min) min=arr[i];

      return min ;
   }

   public static double Compute(double a, double b, char ch)
   {
      switch(ch)
      {
         case '+' : return a+b ;
         case '-' : return a-b ;
         case '*' : return a*b ;
         case '/' : return b!=0 ? a/b : a>0 ? double.PositiveInfinity :
                                        a<0 ? double.NegativeInfinity : double.NaN ;
      }
      return double.NaN ;
   }

}  // end class
Valid XHTML 1.0 Strict top Back Next Up Home