Advertisement
zlatkov

MinMaxSumAvrgProductGeneric

Jan 13th, 2013
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MinMaxSumAvrgProductGeneric
  8. {
  9.     class MinMaxSumAvrgProductGeneric
  10.     {
  11.         static T Min<T>(params T[] sequence)
  12.         {
  13.             int sequenceLength = sequence.GetLength(0);
  14.             if (sequenceLength > 0)
  15.             {
  16.                 if (sequenceLength == 2)
  17.                 {
  18.                     dynamic x = sequence[0];
  19.                     dynamic y = sequence[1];
  20.                     return x < y ? x : y;
  21.                 }
  22.                 else
  23.                 {
  24.                     dynamic result = sequence[0];
  25.                     for (int i = 1; i < sequenceLength; ++i)
  26.                     {
  27.                         result = Min(result, sequence[i]);
  28.                     }
  29.                     return result;
  30.                 }
  31.             }
  32.             else
  33.             {
  34.                 return default(T);
  35.             }
  36.         }
  37.  
  38.         static T Max<T>(params T[] sequence)
  39.         {
  40.             int sequenceLength = sequence.GetLength(0);
  41.             if (sequenceLength > 0)
  42.             {
  43.                 if (sequenceLength == 2)
  44.                 {
  45.                     dynamic x = sequence[0];
  46.                     dynamic y = sequence[1];
  47.                     return x > y ? x : y;
  48.                 }
  49.                 else
  50.                 {
  51.                     dynamic result = sequence[0];
  52.                     for (int i = 0; i < sequenceLength; ++i)
  53.                     {
  54.                         result = Max<T>(result, sequence[i]);
  55.                     }
  56.  
  57.                     return result;
  58.                 }
  59.             }
  60.             return default(T);
  61.         }
  62.  
  63.         static T Sum<T>(params T[] sequence)
  64.         {
  65.             if (sequence.GetLength(0) > 0)
  66.             {
  67.                 dynamic sum = 0;
  68.                 foreach (T x in sequence)
  69.                 {
  70.                     sum += x;
  71.                 }
  72.                 return sum;
  73.             }
  74.             else
  75.             {
  76.                 return default(T);
  77.             }
  78.         }
  79.  
  80.         static T Average<T>(params T[] sequence)
  81.         {
  82.             int sequenceLength = sequence.GetLength(0);
  83.  
  84.             if (sequenceLength > 0)
  85.             {
  86.                 dynamic sum = 0;
  87.                 foreach (T x in sequence)
  88.                 {
  89.                     sum += x;
  90.                 }
  91.  
  92.                 return sum / sequenceLength;
  93.             }
  94.             else
  95.             {
  96.                 return default(T);
  97.             }
  98.         }
  99.  
  100.         static T Product<T>(params T[] sequence)
  101.         {
  102.             if (sequence.GetLength(0) > 0)
  103.             {
  104.                 dynamic product = 1;
  105.                 foreach (T x in sequence)
  106.                 {
  107.                     product *= x;
  108.                 }
  109.  
  110.                 return product;
  111.             }
  112.             else
  113.             {
  114.                 return default(T);
  115.             }
  116.         }
  117.  
  118.         static void Main(string[] args)
  119.         {
  120.             decimal min = Min(6.1m, 3.11111m, 2.543m);
  121.             decimal max = Max(6.1m, 3.11111m, 2.543m);
  122.             decimal sum = Sum(6.1m, 3.11111m, 2.543m);
  123.             decimal average = Average(6.1m, 3.11111m, 2.543m);
  124.             decimal product = Product(6.1m, 3.11111m, 2.543m);
  125.  
  126.             Console.WriteLine("Min: {0}.\nMax: {1}.\nSum: {2}.\nAverage: {3}.\nProduct: {4}.", min, max, sum, average, product);
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement