Temabowl

lab_3

Oct 24th, 2021
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.66 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace lab_3
  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. Console.WriteLine("7. Дополнение множества");
  21. Console.WriteLine("8. Дополнение Логическое");
  22.  
  23.  
  24.  
  25.  
  26. int count = -1;
  27. while (true)
  28. {
  29. try
  30. {
  31.  
  32. int choose = Convert.ToInt32(Console.ReadLine());
  33. if (choose == 1)
  34. {
  35. Console.WriteLine("Максимальный размер:");
  36. int maxsize = Convert.ToInt32(Console.ReadLine());
  37. Console.WriteLine("Значения (вводить через пробел):");
  38. int[] array = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  39. int size = array.Length;
  40.  
  41. if (size > maxsize)
  42. {
  43. Console.WriteLine("Размер больше максимального! Максимаьный размер установлен на " + size);
  44. maxsize = size;
  45.  
  46. }
  47.  
  48. listOfsets.Add(CreateAnySet(maxsize, size, array));
  49.  
  50. count++;
  51. Console.WriteLine("Номер этого множества : " + count);
  52. Console.WriteLine(listOfsets[count].GetType());
  53. listOfsets[count].GetInfo();
  54.  
  55. }
  56.  
  57. if (choose == 2) // zeroset
  58. {
  59. listOfsets.Add(new ZeroSet(true));
  60. count++;
  61. Console.WriteLine("Номер этого множества: " + count);
  62. Console.WriteLine(listOfsets[count]);
  63. Console.WriteLine(listOfsets[count].GetType());
  64. listOfsets[count].GetInfo();
  65. }
  66.  
  67. if (choose == 3) // случайное
  68. {
  69. Console.WriteLine("Введите диапазон принимаемых значений");
  70. Console.WriteLine("min:");
  71. int min = Convert.ToInt32(Console.ReadLine());
  72. Console.WriteLine("max:");
  73. int max = Convert.ToInt32(Console.ReadLine());
  74. Console.WriteLine("Максимальный размер множества");
  75. int maxsize = Convert.ToInt32(Console.ReadLine());
  76. Console.WriteLine("Размер множества");
  77. int size = Convert.ToInt32(Console.ReadLine());
  78.  
  79. if (size > maxsize)
  80. {
  81. Console.WriteLine("Размер больше максимального! Максимаьный размер установлен на " + size);
  82. maxsize = size;
  83.  
  84. }
  85.  
  86. Random rnd = new();
  87. int[] array = new int[maxsize];
  88. for (int i = 0; i < size; i++)
  89. {
  90. array[i] = rnd.Next(min, max);
  91. }
  92. listOfsets.Add(CreateAnySet(maxsize, size, array));
  93. count++;
  94. Console.WriteLine("Номер этого множества : " + count);
  95. Console.WriteLine(listOfsets[count].GetType());
  96. listOfsets[count].GetInfo();
  97.  
  98.  
  99. }
  100. if (choose == 4) // объединение
  101. {
  102. Console.WriteLine("Введите номер множества 1: ");
  103. int numSet1 = Convert.ToInt32(Console.ReadLine());
  104. Console.WriteLine("Введите номер множества 2: ");
  105. int numSet2 = Convert.ToInt32(Console.ReadLine());
  106. TIntset temp = listOfsets[numSet1].Unity(listOfsets[numSet2]);
  107. listOfsets.Add(CreateAnySet(temp.MaxSize, temp.Size, temp.Array));
  108. count++;
  109. Console.WriteLine("Номер этого множества: " + count);
  110. Console.WriteLine(listOfsets[count].GetType());
  111. listOfsets[count].GetInfo();
  112. }
  113. if (choose == 5) // сравнение
  114. {
  115. Console.WriteLine("Введите номер множества 1: ");
  116. int numSet1 = Convert.ToInt32(Console.ReadLine());
  117. Console.WriteLine("Введите номер множества 2: ");
  118. int numSet2 = Convert.ToInt32(Console.ReadLine());
  119.  
  120. Console.WriteLine("Множества равны? (False - нет, True - да)");
  121. Console.WriteLine(listOfsets[numSet1].IsEquale(listOfsets[numSet2]));
  122.  
  123. }
  124.  
  125. if (choose == 6) // просмотр
  126. {
  127. Console.WriteLine("Введите номер множества: ");
  128. var temp = Convert.ToInt32(Console.ReadLine());
  129. Console.WriteLine(listOfsets[temp].GetType());
  130. listOfsets[temp].GetInfo();
  131.  
  132. }
  133. if (choose == 7) // дополнение
  134. {
  135. Console.WriteLine("Введите номер множества 1 (будем дополнять): ");
  136. int numSet1 = Convert.ToInt32(Console.ReadLine());
  137. Console.WriteLine("Введите номер множества 2: ");
  138. int numSet2 = Convert.ToInt32(Console.ReadLine());
  139. TIntset temp = listOfsets[numSet1].Unity(listOfsets[numSet2]);
  140.  
  141. listOfsets[numSet1] = CreateAnySet(temp.MaxSize, temp.Size, temp.Array);
  142. Console.WriteLine("Номер этого множества: " + numSet1);
  143. Console.WriteLine(listOfsets[count].GetType());
  144. listOfsets[numSet1].GetInfo();
  145. }
  146.  
  147. if (choose == 8) // дополнение логическое
  148. {
  149. Console.WriteLine("Введите номер множества 1 (будем дополнять): ");
  150. int numSet1 = Convert.ToInt32(Console.ReadLine());
  151. Console.WriteLine("Введите номер множества 2: ");
  152. int numSet2 = Convert.ToInt32(Console.ReadLine());
  153. TIntset temp = listOfsets[numSet1].LogicComplement(listOfsets[numSet2]);
  154. Console.WriteLine(temp.Array);
  155. listOfsets[numSet1] = CreateAnySet(temp.MaxSize, temp.Size, temp.Array);
  156. Console.WriteLine("Номер этого множества: " + numSet1);
  157. Console.WriteLine(listOfsets[count].GetType());
  158. listOfsets[numSet1].GetInfo();
  159. }
  160.  
  161. }
  162. catch (Exception e)
  163. {
  164. Console.WriteLine("Прозишола непредвиденная ошибка! Возвращение в меню");
  165. Console.WriteLine(e.Message);
  166. continue;
  167. }
  168. }
  169.  
  170. TIntset CreateAnySet(int maxsize, int size, int[] array)
  171. {
  172. if (array.All(x => x > 0))
  173. {
  174. Creator crt = new CreatorPosSet();
  175. return crt.Create(maxsize, size, array);
  176. }
  177.  
  178. if (array.All(x => x < 0))
  179. {
  180. Creator crt = new CreatorNegSet();
  181. return crt.Create(maxsize, size, array);
  182. }
  183.  
  184. if (array.All(x => x == 0))
  185. {
  186. Creator crt = new CreatorZeroSet();
  187. return crt.Create(maxsize, size, array);
  188. }
  189. if (true)
  190. {
  191. return new TIntset(maxsize, size, array);
  192. }
  193. }
  194.  
  195. }
  196. }
  197.  
  198.  
  199.  
  200.  
  201. class TIntset // множество супер класс
  202. {
  203. public bool Empty { get; set; }
  204. public int MaxSize { get; set; }
  205. public int Size { get; set; }
  206. public int[] Array { get; set; }
  207.  
  208.  
  209. public TIntset(TIntset previousTIntset)
  210. {
  211. MaxSize = previousTIntset.MaxSize;
  212. Size = previousTIntset.Size;
  213. Array = new int[previousTIntset.Array.Length];
  214. System.Array.Copy(previousTIntset.Array, Array, previousTIntset.Array.Length);
  215. }
  216.  
  217. public TIntset(bool empt)
  218. {
  219. MaxSize = 5;
  220. Size = MaxSize;
  221. Array = new int[MaxSize];
  222. Size = Array.Length;
  223. }
  224.  
  225. public TIntset()
  226. {
  227. Random rnd = new();
  228. MaxSize = rnd.Next(1, 10);
  229. Size = rnd.Next(1, MaxSize);
  230. Array = new int[Size];
  231. for (int i = 0; i < Size; i++)
  232. {
  233. Array[i] = rnd.Next(1, 100);
  234. }
  235. Size = Array.Length;
  236. }
  237.  
  238. public TIntset(int ms)
  239. {
  240. Random rnd = new();
  241. MaxSize = ms;
  242. Size = rnd.Next(1, ms);
  243. Array = new int[Size];
  244. for (int i = 0; i < Size; i++)
  245. {
  246. Array[i] = rnd.Next(1, 100);
  247. }
  248. Size = Array.Length;
  249. }
  250.  
  251. public TIntset(int ms, int s)
  252. {
  253. Random rnd = new();
  254. MaxSize = ms;
  255. Size = s;
  256. Array = new int[s];
  257. for (int i = 0; i < Size; i++)
  258. {
  259. Array[i] = rnd.Next(1, 100);
  260. }
  261. Size = Array.Length;
  262. }
  263.  
  264. public TIntset(int[] ar)
  265. {
  266. MaxSize = ar.Length;
  267. Size = ar.Length;
  268. Size = Array.Length;
  269. }
  270.  
  271. public TIntset(int ms, int s, int[] array)
  272. {
  273. MaxSize = ms;
  274. Size = s;
  275. Array = array;
  276. Size = Array.Length;
  277. }
  278.  
  279. public void GetInfo()
  280. {
  281. Console.WriteLine("Max size: " + MaxSize);
  282. Console.WriteLine("Size: " + Size);
  283. Console.Write("Set: [ ");
  284. for (int i = 0; i < this.Array.Length; i++)
  285. {
  286. Console.Write(Array[i] + " ");
  287. }
  288. Console.WriteLine("]");
  289. }
  290. public bool IsEquale(TIntset set)
  291. {
  292. int[] set1 = this.Array;
  293. int[] set2 = set.Array;
  294.  
  295. System.Array.Sort(set1);
  296. System.Array.Sort(set2);
  297.  
  298. if (set1.Length != set2.Length)
  299. {
  300. return false;
  301. }
  302.  
  303. for (int i = 0; i < set1.Length; i++)
  304. {
  305. if (set1[i] != set2[i])
  306. {
  307. return false;
  308. }
  309. }
  310. return true;
  311. }
  312. public TIntset Unity(TIntset set)
  313. {
  314. int max = this.Array.Length + set.Array.Length;
  315. List<int> m = new();
  316. for (int i = 0; i < set.Array.Length; i++)
  317. {
  318. m.Add(set.Array[i]);
  319. }
  320. int temp = set.Array.Length + 1;
  321. for (int i = 0; i < this.Array.Length; i++)
  322. {
  323. m.Add(this.Array[i]);
  324. }
  325. m.Sort(); // упорядочить по возрастанию
  326. return new TIntset(max, m.Count, m.ToArray());
  327. }
  328.  
  329. public TIntset LogicComplement(TIntset set)
  330. {
  331. int max = this.Array.Length + set.Array.Length;
  332. List<int> m = new();
  333. for (int i = 0; i < set.Array.Length; i++)
  334. {
  335. m.Add(set.Array[i]);
  336. }
  337. int temp = set.Array.Length + 1;
  338. for (int i = 0; i < this.Array.Length; i++)
  339. {
  340. m.Add(this.Array[i]);
  341. }
  342. m.Sort();
  343. return new TIntset(max, m.Count, m.Distinct().ToArray());
  344. }
  345.  
  346. public IEnumerator GetEnumerator()
  347. {
  348. return Array.GetEnumerator();
  349. }
  350.  
  351.  
  352.  
  353. }
  354. class PosIntSet : TIntset // множество положительных чисел
  355. {
  356. public PosIntSet(TIntset previousTIntset) : base(previousTIntset)
  357. {
  358.  
  359. }
  360. public PosIntSet(int min) : base(min)
  361. {
  362.  
  363. }
  364.  
  365. public PosIntSet()
  366. {
  367.  
  368. }
  369.  
  370. public PosIntSet(int ms, int s, int[] array)
  371. {
  372. MaxSize = ms;
  373. Size = s;
  374. Array = array;
  375. Size = Array.Length;
  376. }
  377.  
  378. }
  379.  
  380. class NegIntSet : TIntset // множество отрицательных чисел
  381. {
  382. public NegIntSet(TIntset previousTIntset) : base(previousTIntset)
  383. {
  384.  
  385. }
  386. public NegIntSet(int min) : base(min)
  387. {
  388.  
  389. }
  390. public NegIntSet()
  391. {
  392.  
  393. }
  394. public NegIntSet(int ms, int s, int[] array)
  395. {
  396. MaxSize = ms;
  397. Size = s;
  398. Array = array;
  399. Size = Array.Length;
  400. }
  401. }
  402.  
  403. class ZeroSet : TIntset // множество нулевых значений
  404. {
  405. public ZeroSet(TIntset previousTIntset) : base(previousTIntset)
  406. {
  407.  
  408. }
  409. public ZeroSet(bool empt) : base(empt)
  410. {
  411.  
  412. }
  413. }
  414.  
  415. abstract class Creator // абстрактный класс создатель
  416. {
  417. public abstract TIntset Create(int ms, int s, int[] array);
  418. }
  419. class CreatorPosSet : Creator // создатель положительного множества
  420. {
  421. public override TIntset Create(int ms, int s, int[] array) { return new PosIntSet(ms, s, array); }
  422. }
  423. class CreatorNegSet : Creator // создатель отрицательного множества
  424. {
  425. public override TIntset Create(int ms, int s, int[] array) { return new NegIntSet(ms, s, array); }
  426. }
  427. class CreatorZeroSet : Creator // создатель нулевого множества
  428. {
  429. public override TIntset Create(int ms, int s, int[] array) { return new ZeroSet(true); }
  430. }
  431.  
  432. }
  433.  
Advertisement
Add Comment
Please, Sign In to add comment