예제 #1
0
        public static SimpleSet operator *(SimpleSet set1, SimpleSet set2)
        {
            var limit     = Math.Min(set1.set.Length, set2.set.Length);
            var resultSet = new SimpleSet(limit);

            for (int i = 1; i <= limit; i++)
            {
                if (set1.IsInSet(i) && set2.IsInSet(i))
                {
                    resultSet.IncludeElementInSet(i);
                }
            }
            return(resultSet);
        }
예제 #2
0
        static void Main(string[] args)
        {
            Set set; string input = ""; int limit = 0; int[] intArrayFromFile = new int[0];

            Console.WriteLine("Введите L/l для логического представления множества,\nB/b для битового представления множества, \nлибо любые другие символы для мультимножества: ");
            var type = Console.ReadLine();

            Console.WriteLine("Введите F/f для чтения из файла, либо любые другие символы для чтения из строки: ");
            var inputType = Console.ReadLine();

            if (inputType.ToLower() == "f")
            {
                Console.WriteLine("Введите директорию: ");
                var dir = Console.ReadLine();
                if (File.Exists(dir))
                {
                    var words = File.ReadAllLines(dir);
                    limit            = Int32.Parse(words[0]);
                    intArrayFromFile = new int[words.Length];
                    for (int i = 1; i < words.Length; i++)
                    {
                        intArrayFromFile[i] = Int32.Parse(words[i]);
                    }
                }
                else
                {
                    Console.WriteLine("Ошибка чтения файла. Программа завершит работу.");
                    return;
                }
            }
            else
            {
                do
                {
                    do
                    {
                        Console.WriteLine("Введите максимум множества: ");
                    }while (!Int32.TryParse(Console.ReadLine(), out limit));
                    Console.WriteLine("Введите элементы множества: ");
                    input = Console.ReadLine();
                } while (limit <= 0);
            }
            if (type.ToLower() == "l")
            {
                set = new SimpleSet(limit);
            }
            else if (type.ToLower() == "b")
            {
                set = new BitSet(limit);
            }
            else
            {
                set = new MultiSet(limit);
            }
            bool isReadOk = true;

            if (inputType.ToLower() == "f")
            {
                set.FillSet(intArrayFromFile);
            }
            else
            {
                isReadOk = set.FillSet(input);
            }
            if (!isReadOk)
            {
                Console.WriteLine("Ошибка чтения. Программа завершит работу.");
                Console.ReadKey();
                return;
            }
            string userChoice;

            do
            {
                Console.WriteLine(@"Введите команду и через пробел - элемент. Команды: ""print"" для печати множества, ""include""  для добавления элемента, ""exclude"" для удаления эелемента, ""find"" для проверки наличия элемента во множестве, ""union"" для объединения с другим множеством, ""intersect"" для пересечения с другим множеством,""exit"" для выхода из программы: ");
                input      = Console.ReadLine();
                userChoice = input.Split()[0];
                int number;
                if (input.Split().Length > 1)
                {
                    try
                    {
                        number = Int32.Parse(input.Split()[1]);
                    }
                    catch
                    {
                        Console.WriteLine("Команда не была распознана, попробуйте снова:  \n");
                        continue;
                    }
                    if (userChoice == "include")
                    {
                        try
                        {
                            set.IncludeElementInSet(number);
                        }
                        catch (SetUpperBoundExceededException e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                    else if (userChoice == "exclude")
                    {
                        try
                        {
                            set.ExcludeElementFromSet(number);
                        }
                        catch (SetUpperBoundExceededException e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                    else if (userChoice == "find")
                    {
                        try
                        {
                            if (set.IsInSet(number))
                            {
                                Console.WriteLine("Элемент присутствует во множестве \n");
                            }
                            else
                            {
                                Console.WriteLine("Элемент отсутствует во множестве \n");
                            }
                        }
                        catch (SetUpperBoundExceededException e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }
                else if (userChoice == "union" || userChoice == "intersect")
                {
                    if (type.ToLower() != "l" && type.ToLower() != "b")
                    {
                        Console.WriteLine("Мультимножества не участвуют в пересечении и объединении.");
                        continue;
                    }
                    Set secondSet;
                    do
                    {
                        do
                        {
                            Console.WriteLine("Введите максимум второго множества: ");
                        }while (!Int32.TryParse(Console.ReadLine(), out limit));
                        Console.WriteLine("Введите второе множество: ");
                        input = Console.ReadLine();
                    } while (limit <= 0);
                    if (type == "L" || type == "l")
                    {
                        secondSet = new SimpleSet(limit);
                    }
                    else if (type == "B" || type == "b")
                    {
                        secondSet = new BitSet(limit);
                    }
                    else
                    {
                        secondSet = new MultiSet(limit);
                    }
                    isReadOk = secondSet.FillSet(input);
                    if (!isReadOk)
                    {
                        Console.WriteLine("Ошибка чтения. Программа завершит работу.");
                        Console.ReadKey();
                        return;
                    }
                    if (userChoice == "union")
                    {
                        if (type.ToLower() == "l")
                        {
                            set = (SimpleSet)set + (SimpleSet)secondSet;
                        }
                        else if (type.ToLower() == "b")
                        {
                            set = (BitSet)set + (BitSet)secondSet;
                        }
                    }
                    else if (userChoice == "intersect")
                    {
                        if (type.ToLower() == "l")
                        {
                            set = (SimpleSet)set * (SimpleSet)secondSet;
                        }
                        else if (type.ToLower() == "b")
                        {
                            set = (BitSet)set * (BitSet)secondSet;
                        }
                    }
                }
                else if (userChoice == "print")
                {
                    set.PrintSet();
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Команда не была распознана, попробуйте снова: \n");
                }
            } while (userChoice != "exit");
        }