Advertisement
Temabowl

Untitled

Oct 1st, 2021
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace ConsoleApp1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. List <TIntset> listOfsets = new();
  13.  
  14. Console.WriteLine("1. Создать свое множество");
  15. Console.WriteLine("2. Создать множество с нулевыми значениями");
  16. Console.WriteLine("3. Создать множество со случайными значениями");
  17. Console.WriteLine("4. Объединение 2х множеств");
  18. Console.WriteLine("5. Проверить равенство множеств");
  19. Console.WriteLine("6. Просмотр множества");
  20. int count = -1;
  21. while (true)
  22. {
  23. try
  24. {
  25.  
  26. int choose = Convert.ToInt32(Console.ReadLine());
  27. if (choose == 1)
  28. {
  29. Console.WriteLine("Максимальный размер:");
  30. int maxsize = Convert.ToInt32(Console.ReadLine());
  31. Console.WriteLine("Размер:");
  32. int size = Convert.ToInt32(Console.ReadLine());
  33. if (size > maxsize)
  34. {
  35. Console.WriteLine("Размер больше максимального! Максимаьный размер установлен на " + size);
  36. maxsize = size;
  37.  
  38. }
  39. Console.WriteLine("Значения (вводить через пробел):");
  40. int[] array = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  41. listOfsets.Add(new TIntset(maxsize, size, array));
  42. count++;
  43. Console.WriteLine("Номер этого множества : " + count);
  44. }
  45.  
  46. if (choose == 2)
  47. {
  48. listOfsets.Add(new TIntset(true));
  49. count++;
  50. Console.WriteLine("Номер этого множества: " + count);
  51. listOfsets[count].GetInfo();
  52. }
  53.  
  54. if (choose == 3)
  55. {
  56. listOfsets.Add(new TIntset());
  57. count++;
  58. Console.WriteLine("Номер этого множества: " + count);
  59. listOfsets[count].GetInfo();
  60. }
  61. if (choose == 4)
  62. {
  63. Console.WriteLine("Введите номер множества 1: ");
  64. int numSet1 = Convert.ToInt32(Console.ReadLine());
  65. Console.WriteLine("Введите номер множества 2: ");
  66. int numSet2 = Convert.ToInt32(Console.ReadLine());
  67. listOfsets.Add(listOfsets[numSet1].Unity(listOfsets[numSet2]));
  68. count++;
  69. Console.WriteLine("Номер этого множества: " + count);
  70. listOfsets[count].GetInfo();
  71. }
  72. if (choose == 5)
  73. {
  74. Console.WriteLine("Введите номер множества 1: ");
  75. int numSet1 = Convert.ToInt32(Console.ReadLine());
  76. Console.WriteLine("Введите номер множества 2: ");
  77. int numSet2 = Convert.ToInt32(Console.ReadLine());
  78.  
  79. Console.WriteLine("Множества равны? (False - нет, True - да)");
  80. Console.WriteLine(listOfsets[numSet1].IsEquale(listOfsets[numSet2]));
  81.  
  82. }
  83.  
  84. if (choose == 6)
  85. {
  86. Console.WriteLine("Введите номер множества: ");
  87. listOfsets[Convert.ToInt32(Console.ReadLine())].GetInfo();
  88.  
  89. }
  90. }
  91. catch (Exception e)
  92. {
  93. continue;
  94. }
  95.  
  96.  
  97. }
  98.  
  99.  
  100. }
  101. }
  102.  
  103. public class TIntset: IEnumerable
  104. {
  105. private bool Empty { get; set; }
  106. private int MaxSize { get; set; }
  107. private int Size { get; set; }
  108. public int[] Array { get; set; }
  109.  
  110.  
  111. public TIntset(TIntset previousTIntset)
  112. {
  113. MaxSize = previousTIntset.MaxSize;
  114. Size = previousTIntset.Size;
  115. Array = new int[previousTIntset.Array.Length];
  116. System.Array.Copy(previousTIntset.Array, Array, previousTIntset.Array.Length);
  117. }
  118.  
  119. public TIntset(bool empt)
  120. {
  121. MaxSize = 1;
  122. Size = MaxSize;
  123. Array = new int[MaxSize];
  124. Array.ToList().Distinct().ToArray();
  125. Size = Array.Length;
  126. }
  127.  
  128. public TIntset()
  129. {
  130. Random rnd = new();
  131. MaxSize = rnd.Next(1, 10);
  132. Size = rnd.Next(1, MaxSize);
  133. Array = new int[Size];
  134. for (int i = 0; i < Size; i++)
  135. {
  136. Array[i] = rnd.Next(1, 100);
  137. }
  138. Array.ToList().Distinct().ToArray();
  139. Size = Array.Length;
  140. }
  141.  
  142. public TIntset(int ms)
  143. {
  144. Random rnd = new();
  145. MaxSize = ms;
  146. Size = rnd.Next(1, ms);
  147. Array = new int[Size];
  148. for (int i = 0; i < Size; i++)
  149. {
  150. Array[i] = rnd.Next(1, 100);
  151. }
  152. Array.ToList().Distinct().ToArray();
  153. Size = Array.Length;
  154. }
  155.  
  156. public TIntset(int ms, int s)
  157. {
  158. Random rnd = new();
  159. MaxSize = ms;
  160. Size = s;
  161. Array = new int[s];
  162. for (int i = 0; i < Size; i++)
  163. {
  164. Array[i] = rnd.Next(1, 100);
  165. }
  166. Array.ToList().Distinct().ToArray();
  167. Size = Array.Length;
  168. }
  169.  
  170. public TIntset(int[] ar)
  171. {
  172. MaxSize = ar.Length;
  173. Size = ar.Length;
  174. Array = ar.ToList().Distinct().ToArray();
  175. Size = Array.Length;
  176. }
  177.  
  178. public TIntset(int ms, int s, int[] array)
  179. {
  180. MaxSize = ms;
  181. Size = s;
  182. Array = array;
  183. Array = Array.ToList().Distinct().ToArray();
  184. Size = Array.Length;
  185. }
  186.  
  187.  
  188. //Информация о множестве
  189. public void GetInfo()
  190. {
  191. Console.WriteLine("Max size: " + MaxSize);
  192. Console.WriteLine("Size: " + Size);
  193. Console.Write("Set: [ ");
  194. for (int i = 0; i < this.Array.Length; i++)
  195. {
  196. Console.Write(Array[i] + " ");
  197. }
  198. Console.WriteLine("]");
  199. }
  200.  
  201. //Просмотр множества
  202. public void SetInfo()
  203. {
  204. for (int i = 0; i < this.Array.Length; i++)
  205. {
  206. Console.Write(this.Array[i] + " ");
  207. }
  208. }
  209.  
  210. // Проверка на равенство
  211. public bool IsEquale(TIntset set)
  212. {
  213. int[] set1 = this.Array;
  214. int[] set2 = set.Array;
  215.  
  216. System.Array.Sort(set1);
  217. System.Array.Sort(set2);
  218.  
  219. if (set1.Length != set2.Length)
  220. {
  221. return false;
  222. }
  223.  
  224. for (int i = 0; i < set1.Length; i++)
  225. {
  226. if (set1[i] != set2[i])
  227. {
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233.  
  234. //Объединение 2х множеств
  235. public TIntset Unity(TIntset set)
  236. {
  237. int max = this.Array.Length + set.Array.Length;
  238. List<int> m = new();
  239. for (int i = 0; i < set.Array.Length; i++)
  240. {
  241. m.Add(set.Array[i]);
  242. }
  243. int temp = set.Array.Length + 1;
  244. for (int i = 0; i < this.Array.Length; i++)
  245. {
  246. m.Add(this.Array[i]);
  247. }
  248. m.Distinct();
  249.  
  250. return new TIntset(max, m.Count, m.ToArray());
  251. }
  252.  
  253.  
  254. public IEnumerator GetEnumerator()
  255. {
  256. return Array.GetEnumerator();
  257. }
  258. }
  259. }
  260.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement