Advertisement
Margoshinka

collections

Nov 13th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5.  
  6. namespace lab5
  7. {
  8. class Program
  9. {
  10. static int N=0;
  11. static Random rnd = new Random();
  12. static int kol = 0;
  13. static int nmb = 1;
  14. static void AddL(List<int> list, int n)
  15. {
  16. for (int i = 0; i < n; i++)
  17. {
  18. list.Add(rnd.Next(0, 10));
  19. }
  20. }
  21. static void PrintL(List<int> list, int n)
  22. {
  23. for (int i = 0; i < list.Count; i++)
  24. {
  25. Console.Write(list[i] + " ");
  26.  
  27. }
  28. Console.WriteLine();
  29. }
  30. static void print_hashset(int i,HashSet<char>[] arr)
  31. {
  32. Console.WriteLine($"{i+1}-ю страну посетили:");
  33. foreach (var val in arr[i])
  34. {
  35. Console.Write(val + " ");
  36. }
  37. Console.WriteLine();
  38. }
  39. static IEnumerable<char> Union(int i, HashSet<char>[] arr, IEnumerable<char> query)
  40. {
  41. query = from planet in query.Union(arr[i])
  42. select planet;
  43. return query;
  44. }
  45. /* public static HashSet<char> ToHashSet<char> (this IEnumerable<char> query)
  46. {
  47. HashSet<char> result = new HashSet<char>();
  48. result.UnionWith(query);
  49. return result;
  50.  
  51. }*/
  52. static HashSet<char> Intersect(int i, HashSet<char>[] arr, IEnumerable<char> query, HashSet<char> rez)
  53. {
  54. query = from planet in query.Intersect(arr[i])
  55. select planet;
  56. rez.UnionWith(query);
  57. foreach (var val in rez)
  58. {
  59. Console.Write(val + " ");
  60. }
  61.  
  62. return rez;
  63. }
  64. static void prntarr(int i, HashSet<char>[] arr, int quantity)
  65. {
  66. if (arr[i].Count == quantity) Console.WriteLine($"{i+1}-ю страну посетили все туристы");
  67. else if (arr[i].Count == 0) Console.WriteLine($"{i + 1}-ю никто из туристов не посетил");
  68. else Console.WriteLine($"{i + 1}-ю страну посетили {arr[i].Count} туристов");
  69. Console.WriteLine();
  70.  
  71. }
  72. static void Write_text(string name) //заполнение файла текстом
  73. {
  74. try
  75. {
  76. using (StreamWriter f = new StreamWriter(name))
  77. {
  78.  
  79. Console.WriteLine("Введите N");
  80. string buf = Console.ReadLine();
  81. int N = int.Parse(buf);
  82. Console.WriteLine("Заполнение файла");
  83. for (int i = 1; i <= N; i++)
  84. {
  85.  
  86. buf = Console.ReadLine();
  87.  
  88. f.WriteLine(buf);
  89.  
  90. }
  91.  
  92. }
  93. }
  94. catch (IOException e)
  95. {
  96. Console.WriteLine("Error:" + e.Message);
  97. return;
  98. }
  99. }
  100. static void Find_Text(string name, HashSet<char>[] arr1) //чтение из файла
  101. {
  102.  
  103. try
  104. {
  105. using (StreamReader f = new StreamReader(name))
  106. {
  107.  
  108. int i = 0;
  109. string s;
  110. while ((s = f.ReadLine()) != null && i<10)
  111. {
  112. string[] splitLine = s.Split(' ');
  113. kol += splitLine.Length;
  114. for (int j = 0; j < splitLine.Length; j++)
  115. { int k = 0;
  116. while (k < splitLine[j].Length)
  117. {
  118. if ((splitLine[j][k] > 'а' && splitLine[j][k] < 'е') || (splitLine[j][k] >= 'ж' && splitLine[j][k] <= 'з'))
  119. arr1[i].Add(splitLine[j][k++]);
  120. else k++;
  121. }
  122. i++;
  123.  
  124. }
  125.  
  126.  
  127.  
  128. }
  129.  
  130. }
  131. }
  132. catch (IOException e)
  133. {
  134. Console.WriteLine("Error:" + e.Message);
  135. return;
  136. }
  137. }
  138.  
  139. static void Dublicates_HashSet(HashSet<char>[] arr, HashSet<char> rez ) //поиск повторяющихся звонких согласных
  140. {
  141. var set = new HashSet<char>();
  142. for (int i = 0; i < kol; i++)
  143. {
  144. foreach (var item in arr[i])
  145. if (!set.Add(item))
  146. rez.Add(item);
  147. }
  148. }
  149. static void Write_Text(string name) //заполнение файла текстом
  150. {
  151. try
  152. {
  153. using (StreamWriter f = new StreamWriter(name))
  154. {
  155.  
  156. Console.WriteLine("Введите N");
  157. string buf = Console.ReadLine();
  158. f.WriteLine(buf);
  159. int N = int.Parse(buf);
  160. Console.WriteLine("Заполнение файла");
  161. for (int i = 1; i <= N; i++)
  162. {
  163.  
  164. buf = Console.ReadLine();
  165.  
  166. f.WriteLine(buf);
  167.  
  168. }
  169.  
  170. }
  171. }
  172. catch (IOException e)
  173. {
  174. Console.WriteLine("Error:" + e.Message);
  175. return;
  176. }
  177. }
  178. static void Read_Text(string name, Dictionary<string, int> d)
  179. {
  180.  
  181. try
  182. {
  183. using (StreamReader f = new StreamReader(name))
  184. {
  185.  
  186.  
  187. string s;
  188. s = f.ReadLine();
  189. N = int.Parse(s);
  190. while ((s = f.ReadLine()) != null)
  191. {
  192. string[] splitLine = s.Split(' ');
  193.  
  194.  
  195.  
  196. string[] splitline2 = splitLine[2].Split('-');
  197. string nomber = splitline2[0] + "-" + splitline2[1];
  198. if (!d.ContainsKey(nomber))
  199. d.Add(nomber, nmb);
  200. else { int v = d[nomber]; d[nomber] = v + 1; }
  201. /* if (nmb != "") if (!nmb.Contains(nomber)) nmb += nomber + " ";
  202. int nom = int.Parse(splitline2[2]); ;
  203. Employee e = new Employee(splitLine[0], splitLine[1], nomber, nom);
  204. d.Add(nom, e);*/
  205.  
  206.  
  207.  
  208.  
  209. }
  210.  
  211. }
  212. }
  213. catch (IOException e)
  214. {
  215. Console.WriteLine("Error:" + e.Message);
  216. return;
  217. }
  218. }
  219.  
  220. static void Main(string[] args)
  221. {
  222. /* List<int> list = new List<int>();
  223.  
  224. Console.WriteLine("Введите n");
  225. string buf = Console.ReadLine();
  226. int n = int.Parse(buf);
  227.  
  228. AddL(list, n);
  229. PrintL(list, list.Count);
  230.  
  231. Console.WriteLine("Введите цифру");
  232. buf = Console.ReadLine();
  233. int k = int.Parse(buf);
  234.  
  235. list.RemoveAll(x=>x==k);
  236. PrintL(list, list.Count);
  237. Console.WriteLine("Введите e");
  238. buf = Console.ReadLine();
  239. int e = int.Parse(buf);
  240.  
  241. int e1 = list.FindIndex(x => x == e);
  242. int e2 = list.FindLastIndex(x => x == e);
  243. if (e2 == e1) e2 = -1;
  244. if (e1 != -1 && e2 != -1) list.Reverse(e1+1, e2-e1-1);
  245. else Console.WriteLine("e входит менее двух раз");
  246. PrintL(list, list.Count);
  247. Console.WriteLine("Введите количество стран");
  248. buf = Console.ReadLine();
  249. n = int.Parse(buf);
  250.  
  251. HashSet<char>[] arr = new HashSet<char>[n];
  252.  
  253. for(int i=0; i<n; i++)
  254. { HashSet<char> hs = new HashSet<char>();
  255. arr[i] = hs;
  256. }
  257.  
  258. for (int i = 0; i < n; i++)
  259. {
  260. int k1 = rnd.Next(0, 11);
  261. for (int j = 0; j < k1; j++)
  262. {
  263. char ch = (char)rnd.Next(0x0410, 0x44F);
  264. arr[i].Add(ch);
  265. }
  266. }
  267.  
  268. for (int i = 0; i < n; i++)
  269. {
  270. print_hashset(i, arr);
  271. }
  272.  
  273. IEnumerable<char> query = from planet in arr[0].Union(arr[1])
  274. select planet;
  275. for (int i = 2; i < n; i++)
  276. {
  277. query= Union(i, arr, query);
  278. }
  279.  
  280. int quantity = query.Count();
  281.  
  282. Console.WriteLine($"Всего туристов:{quantity}");
  283.  
  284.  
  285. for (int i = 0; i < n; i++)
  286. {
  287. prntarr(i, arr, quantity);
  288. }*/
  289. /* Write_text("Text.dat"); //№4
  290. HashSet<char>[] arr1 = new HashSet<char>[10];
  291. for (int i = 0; i < 10; i++)
  292. {
  293. HashSet<char> hs = new HashSet<char>();
  294. arr1[i] = hs;
  295. }
  296. Find_Text("Text.dat", arr1);
  297. for (int i = 0; i < kol; i++)
  298. {
  299. print_hashset(i, arr1);
  300. }
  301. HashSet<char> rez = new HashSet<char>();
  302.  
  303.  
  304. Dublicates_HashSet(arr1, rez);
  305.  
  306.  
  307. var result = rez.OrderBy(s => s);
  308.  
  309. foreach (var val in result)
  310. {
  311. Console.Write(val + " ");
  312. }
  313. Console.WriteLine();*/
  314.  
  315. //№5
  316. Dictionary<string, int> employee = new Dictionary<string, int>();
  317. Read_Text("Text_.dat", employee);
  318. foreach (var val in employee)
  319. {
  320. Console.Write($"Работников с номером {val.Key} - {val.Value} штук, в среднем {val.Value/employee.Count}");
  321. Console.WriteLine();
  322. }
  323. Console.WriteLine();
  324.  
  325.  
  326. }
  327. }
  328. }
  329.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement