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 lab_3
- {
- 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. Просмотр множества");
- Console.WriteLine("7. Дополнение множества");
- Console.WriteLine("8. Дополнение Логическое");
- 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[] array = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- int size = array.Length;
- if (size > maxsize)
- {
- Console.WriteLine("Размер больше максимального! Максимаьный размер установлен на " + size);
- maxsize = size;
- }
- listOfsets.Add(CreateAnySet(maxsize, size, array));
- count++;
- Console.WriteLine("Номер этого множества : " + count);
- Console.WriteLine(listOfsets[count].GetType());
- listOfsets[count].GetInfo();
- }
- if (choose == 2) // zeroset
- {
- listOfsets.Add(new ZeroSet(true));
- count++;
- Console.WriteLine("Номер этого множества: " + count);
- Console.WriteLine(listOfsets[count]);
- Console.WriteLine(listOfsets[count].GetType());
- listOfsets[count].GetInfo();
- }
- if (choose == 3) // случайное
- {
- Console.WriteLine("Введите диапазон принимаемых значений");
- Console.WriteLine("min:");
- int min = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("max:");
- int max = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Максимальный размер множества");
- int maxsize = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Размер множества");
- int size = Convert.ToInt32(Console.ReadLine());
- if (size > maxsize)
- {
- Console.WriteLine("Размер больше максимального! Максимаьный размер установлен на " + size);
- maxsize = size;
- }
- Random rnd = new();
- int[] array = new int[maxsize];
- for (int i = 0; i < size; i++)
- {
- array[i] = rnd.Next(min, max);
- }
- listOfsets.Add(CreateAnySet(maxsize, size, array));
- count++;
- Console.WriteLine("Номер этого множества : " + count);
- Console.WriteLine(listOfsets[count].GetType());
- listOfsets[count].GetInfo();
- }
- if (choose == 4) // объединение
- {
- Console.WriteLine("Введите номер множества 1: ");
- int numSet1 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Введите номер множества 2: ");
- int numSet2 = Convert.ToInt32(Console.ReadLine());
- TIntset temp = listOfsets[numSet1].Unity(listOfsets[numSet2]);
- listOfsets.Add(CreateAnySet(temp.MaxSize, temp.Size, temp.Array));
- count++;
- Console.WriteLine("Номер этого множества: " + count);
- Console.WriteLine(listOfsets[count].GetType());
- 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("Введите номер множества: ");
- var temp = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine(listOfsets[temp].GetType());
- listOfsets[temp].GetInfo();
- }
- if (choose == 7) // дополнение
- {
- Console.WriteLine("Введите номер множества 1 (будем дополнять): ");
- int numSet1 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Введите номер множества 2: ");
- int numSet2 = Convert.ToInt32(Console.ReadLine());
- TIntset temp = listOfsets[numSet1].Unity(listOfsets[numSet2]);
- listOfsets[numSet1] = CreateAnySet(temp.MaxSize, temp.Size, temp.Array);
- Console.WriteLine("Номер этого множества: " + numSet1);
- Console.WriteLine(listOfsets[count].GetType());
- listOfsets[numSet1].GetInfo();
- }
- if (choose == 8) // дополнение логическое
- {
- Console.WriteLine("Введите номер множества 1 (будем дополнять): ");
- int numSet1 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Введите номер множества 2: ");
- int numSet2 = Convert.ToInt32(Console.ReadLine());
- TIntset temp = listOfsets[numSet1].LogicComplement(listOfsets[numSet2]);
- Console.WriteLine(temp.Array);
- listOfsets[numSet1] = CreateAnySet(temp.MaxSize, temp.Size, temp.Array);
- Console.WriteLine("Номер этого множества: " + numSet1);
- Console.WriteLine(listOfsets[count].GetType());
- listOfsets[numSet1].GetInfo();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("Прозишола непредвиденная ошибка! Возвращение в меню");
- Console.WriteLine(e.Message);
- continue;
- }
- }
- TIntset CreateAnySet(int maxsize, int size, int[] array)
- {
- if (array.All(x => x > 0))
- {
- Creator crt = new CreatorPosSet();
- return crt.Create(maxsize, size, array);
- }
- if (array.All(x => x < 0))
- {
- Creator crt = new CreatorNegSet();
- return crt.Create(maxsize, size, array);
- }
- if (array.All(x => x == 0))
- {
- Creator crt = new CreatorZeroSet();
- return crt.Create(maxsize, size, array);
- }
- if (true)
- {
- return new TIntset(maxsize, size, array);
- }
- }
- }
- }
- class TIntset // множество супер класс
- {
- public bool Empty { get; set; }
- public int MaxSize { get; set; }
- public 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 = 5;
- Size = MaxSize;
- Array = new int[MaxSize];
- 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);
- }
- 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);
- }
- 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);
- }
- Size = Array.Length;
- }
- public TIntset(int[] ar)
- {
- MaxSize = ar.Length;
- Size = ar.Length;
- Size = Array.Length;
- }
- public TIntset(int ms, int s, int[] array)
- {
- MaxSize = ms;
- Size = s;
- Array = array;
- 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 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;
- }
- 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.Sort(); // упорядочить по возрастанию
- return new TIntset(max, m.Count, m.ToArray());
- }
- public TIntset LogicComplement(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.Sort();
- return new TIntset(max, m.Count, m.Distinct().ToArray());
- }
- public IEnumerator GetEnumerator()
- {
- return Array.GetEnumerator();
- }
- }
- class PosIntSet : TIntset // множество положительных чисел
- {
- public PosIntSet(TIntset previousTIntset) : base(previousTIntset)
- {
- }
- public PosIntSet(int min) : base(min)
- {
- }
- public PosIntSet()
- {
- }
- public PosIntSet(int ms, int s, int[] array)
- {
- MaxSize = ms;
- Size = s;
- Array = array;
- Size = Array.Length;
- }
- }
- class NegIntSet : TIntset // множество отрицательных чисел
- {
- public NegIntSet(TIntset previousTIntset) : base(previousTIntset)
- {
- }
- public NegIntSet(int min) : base(min)
- {
- }
- public NegIntSet()
- {
- }
- public NegIntSet(int ms, int s, int[] array)
- {
- MaxSize = ms;
- Size = s;
- Array = array;
- Size = Array.Length;
- }
- }
- class ZeroSet : TIntset // множество нулевых значений
- {
- public ZeroSet(TIntset previousTIntset) : base(previousTIntset)
- {
- }
- public ZeroSet(bool empt) : base(empt)
- {
- }
- }
- abstract class Creator // абстрактный класс создатель
- {
- public abstract TIntset Create(int ms, int s, int[] array);
- }
- class CreatorPosSet : Creator // создатель положительного множества
- {
- public override TIntset Create(int ms, int s, int[] array) { return new PosIntSet(ms, s, array); }
- }
- class CreatorNegSet : Creator // создатель отрицательного множества
- {
- public override TIntset Create(int ms, int s, int[] array) { return new NegIntSet(ms, s, array); }
- }
- class CreatorZeroSet : Creator // создатель нулевого множества
- {
- public override TIntset Create(int ms, int s, int[] array) { return new ZeroSet(true); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment