Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- List <TIntset> listOfsets = new();
- Console.WriteLine("1. Создать свое множество");
- Console.WriteLine("2. Создать множество с нулевыми значениями");
- Console.WriteLine("3. Создать множество со случайными значениями");
- Console.WriteLine("4. Объединение 2х множеств");
- Console.WriteLine("5. Проверить равенство множеств");
- Console.WriteLine("6. Просмотр множества");
- int count = -1;
- while (true)
- {
- try
- {
- int choose = Convert.ToInt32(Console.ReadLine());
- if (choose == 1)
- {
- Console.WriteLine("Максимальный размер:");
- int maxsize = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Размер:");
- int size = Convert.ToInt32(Console.ReadLine());
- if (size > maxsize)
- {
- Console.WriteLine("Размер больше максимального! Максимаьный размер установлен на " + size);
- maxsize = size;
- }
- Console.WriteLine("Значения (вводить через пробел):");
- int[] array = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- listOfsets.Add(new TIntset(maxsize, size, array));
- count++;
- Console.WriteLine("Номер этого множества : " + count);
- }
- if (choose == 2)
- {
- listOfsets.Add(new TIntset(true));
- count++;
- Console.WriteLine("Номер этого множества: " + count);
- listOfsets[count].GetInfo();
- }
- if (choose == 3)
- {
- listOfsets.Add(new TIntset());
- count++;
- Console.WriteLine("Номер этого множества: " + count);
- listOfsets[count].GetInfo();
- }
- if (choose == 4)
- {
- Console.WriteLine("Введите номер множества 1: ");
- int numSet1 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Введите номер множества 2: ");
- int numSet2 = Convert.ToInt32(Console.ReadLine());
- listOfsets.Add(listOfsets[numSet1].Unity(listOfsets[numSet2]));
- count++;
- Console.WriteLine("Номер этого множества: " + count);
- listOfsets[count].GetInfo();
- }
- if (choose == 5)
- {
- Console.WriteLine("Введите номер множества 1: ");
- int numSet1 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Введите номер множества 2: ");
- int numSet2 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Множества равны? (False - нет, True - да)");
- Console.WriteLine(listOfsets[numSet1].IsEquale(listOfsets[numSet2]));
- }
- if (choose == 6)
- {
- Console.WriteLine("Введите номер множества: ");
- listOfsets[Convert.ToInt32(Console.ReadLine())].GetInfo();
- }
- }
- catch (Exception e)
- {
- continue;
- }
- }
- }
- }
- public class TIntset: IEnumerable
- {
- private bool Empty { get; set; }
- private int MaxSize { get; set; }
- private int Size { get; set; }
- public int[] Array { get; set; }
- public TIntset(TIntset previousTIntset)
- {
- MaxSize = previousTIntset.MaxSize;
- Size = previousTIntset.Size;
- Array = new int[previousTIntset.Array.Length];
- System.Array.Copy(previousTIntset.Array, Array, previousTIntset.Array.Length);
- }
- public TIntset(bool empt)
- {
- MaxSize = 1;
- Size = MaxSize;
- Array = new int[MaxSize];
- Array.ToList().Distinct().ToArray();
- Size = Array.Length;
- }
- public TIntset()
- {
- Random rnd = new();
- MaxSize = rnd.Next(1, 10);
- Size = rnd.Next(1, MaxSize);
- Array = new int[Size];
- for (int i = 0; i < Size; i++)
- {
- Array[i] = rnd.Next(1, 100);
- }
- Array.ToList().Distinct().ToArray();
- Size = Array.Length;
- }
- public TIntset(int ms)
- {
- Random rnd = new();
- MaxSize = ms;
- Size = rnd.Next(1, ms);
- Array = new int[Size];
- for (int i = 0; i < Size; i++)
- {
- Array[i] = rnd.Next(1, 100);
- }
- Array.ToList().Distinct().ToArray();
- Size = Array.Length;
- }
- public TIntset(int ms, int s)
- {
- Random rnd = new();
- MaxSize = ms;
- Size = s;
- Array = new int[s];
- for (int i = 0; i < Size; i++)
- {
- Array[i] = rnd.Next(1, 100);
- }
- Array.ToList().Distinct().ToArray();
- Size = Array.Length;
- }
- public TIntset(int[] ar)
- {
- MaxSize = ar.Length;
- Size = ar.Length;
- Array = ar.ToList().Distinct().ToArray();
- Size = Array.Length;
- }
- public TIntset(int ms, int s, int[] array)
- {
- MaxSize = ms;
- Size = s;
- Array = array;
- Array = Array.ToList().Distinct().ToArray();
- Size = Array.Length;
- }
- //Информация о множестве
- public void GetInfo()
- {
- Console.WriteLine("Max size: " + MaxSize);
- Console.WriteLine("Size: " + Size);
- Console.Write("Set: [ ");
- for (int i = 0; i < this.Array.Length; i++)
- {
- Console.Write(Array[i] + " ");
- }
- Console.WriteLine("]");
- }
- //Просмотр множества
- public void SetInfo()
- {
- for (int i = 0; i < this.Array.Length; i++)
- {
- Console.Write(this.Array[i] + " ");
- }
- }
- // Проверка на равенство
- public bool IsEquale(TIntset set)
- {
- int[] set1 = this.Array;
- int[] set2 = set.Array;
- System.Array.Sort(set1);
- System.Array.Sort(set2);
- if (set1.Length != set2.Length)
- {
- return false;
- }
- for (int i = 0; i < set1.Length; i++)
- {
- if (set1[i] != set2[i])
- {
- return false;
- }
- }
- return true;
- }
- //Объединение 2х множеств
- public TIntset Unity(TIntset set)
- {
- int max = this.Array.Length + set.Array.Length;
- List<int> m = new();
- for (int i = 0; i < set.Array.Length; i++)
- {
- m.Add(set.Array[i]);
- }
- int temp = set.Array.Length + 1;
- for (int i = 0; i < this.Array.Length; i++)
- {
- m.Add(this.Array[i]);
- }
- m.Distinct();
- return new TIntset(max, m.Count, m.ToArray());
- }
- public IEnumerator GetEnumerator()
- {
- return Array.GetEnumerator();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement