Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication3
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14.  
  15. IEnumerable<String> enu = new String[] {"koko", "kol"};
  16. enu.IsEmpty();
  17. enu.MeanLength();
  18.  
  19. IEnumerable<float> floatt = new float[] { 2.3f, 4.5f};
  20. floatt.IsEmpty();
  21. floatt.Mean();
  22.  
  23. IEnumerable<int> intt = new int[] {};
  24. intt.IsEmpty();
  25. intt.Mean();
  26.  
  27.  
  28. Console.ReadKey();
  29. }
  30. }
  31.  
  32. static class Rozszerzajaca
  33. {
  34.  
  35. public static bool IsEmpty(this IEnumerable source)
  36. {
  37. if (source != null && source.GetEnumerator().MoveNext())
  38. {
  39.  
  40. Console.WriteLine("IsEmpty: Not Empty!");
  41. return false;
  42.  
  43. }
  44. else
  45. {
  46. Console.WriteLine("IsEmpty: Empty!");
  47. return true;
  48. }
  49.  
  50. }
  51.  
  52. public static void Mean(this IEnumerable<float> source)
  53. {
  54. if (source != null && source.GetEnumerator().MoveNext())
  55. {
  56.  
  57. float sum = 0;
  58. int il = 0;
  59. foreach (float i in source)
  60. {
  61. sum = sum + i;
  62. il++;
  63. }
  64.  
  65. float res = sum / il;
  66. Console.WriteLine("Mean: " + res);
  67. }
  68. else
  69. Console.WriteLine("Mean: Empty array!");
  70.  
  71. }
  72.  
  73.  
  74. public static void Mean(this IEnumerable<int> source)
  75. {
  76. if (source != null && source.GetEnumerator().MoveNext())
  77. {
  78. double sum = 0;
  79. int il = 0;
  80. foreach (int i in source)
  81. {
  82. sum = sum + i;
  83. il++;
  84. }
  85.  
  86. double res = sum / il;
  87. Console.WriteLine("Mean: " + res);
  88. }
  89. else
  90. Console.WriteLine("Mean: Empty array!");
  91.  
  92.  
  93.  
  94. }
  95.  
  96. public static void MeanLength(this IEnumerable<string> source)
  97. {
  98. if (source != null && source.GetEnumerator().MoveNext())
  99. {
  100. double sum = 0;
  101. int il = 0;
  102. foreach (var i in source)
  103. {
  104. sum = sum + i.Length;
  105. il++;
  106. }
  107. double res = sum / il;
  108. Console.WriteLine("Mean length: " + res);
  109. }
  110. else
  111. Console.WriteLine("Mean length: Empty array!");
  112.  
  113. }
  114.  
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement