public static void NumberArraySum()
        {
            var length = 10;
            var rnd    = new Random();

            Console.WriteLine("Sum of int array demonstration:");
            var arrInt = new int[length];

            for (var i = 0; i < arrInt.Length; i++)
            {
                arrInt[i] = rnd.Next(0, 50);
            }
            Console.WriteLine("Int array:");
            CustomSort41.DisplayArray(arrInt);
            Console.WriteLine();
            Console.WriteLine($"Sum: {arrInt.Sum()}");
            Console.WriteLine("Sum of double array demonstration:");
            var arrDouble = new double[length];

            for (var i = 0; i < arrDouble.Length; i++)
            {
                arrDouble[i] = rnd.NextDouble() * 10;
            }
            Console.WriteLine("Double array:");
            CustomSort41.DisplayArray(arrDouble);
            Console.WriteLine();
            Console.WriteLine("Sum: {0:0.#}  ", arrDouble.Sum());
        }
        public static void SortingUnit()
        {
            var length    = 100;
            var threads   = 0;
            var rnd       = new Random();
            var arrInt    = new int[length];
            var arrDouble = new double[length];

            OnSortFinish += (element) =>
            {
                Console.WriteLine(element);
                threads++;
            };
            for (var i = 0; i < length; i++)
            {
                arrInt[i]    = rnd.Next(0, 50);
                arrDouble[i] = rnd.NextDouble() * 10;
            }
            SortThread(arrInt, (a, b) => a < b);
            SortThread(arrDouble, (a, b) => a > b);
            while (threads != 2)
            {
                Console.WriteLine("Sorting...");
                Thread.Sleep(1000);
                Thread.Sleep(1000);
            }
            Console.WriteLine("Elements in ascending sorted int array");
            CustomSort41.DisplayArray(arrInt);
            Console.WriteLine();
            Console.WriteLine("Elements in descending sorted double array");
            CustomSort41.DisplayArray(arrDouble);
        }
        public static void CustomSortDemo()
        {
            Console.WriteLine("Default text:\n");
            var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" +
                      "Mauris vel purus aliquet, faucibus nisl quis, sollicitudin lectus.\n" +
                      "Nam aliquam scelerisque sem eget ornare.\n" +
                      "Donec at pharetra nulla, a iaculis ex.";

            Console.WriteLine(str);
            var words = str.Split(new char[] { ' ', '.', ',', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            CustomSort41.SortArray(words, (n1, n2) =>
            {
                if (n1.Length != n2.Length)
                {
                    return(n1.Length < n2.Length);
                }
                for (var i = 0; i < n1.Length; i++)
                {
                    if (n1[i] != n2[i])
                    {
                        return(n1[i] < n2[i]);
                    }
                }
                return(false);
            });
            Console.WriteLine();
            Console.WriteLine("Sorted:\n");
            CustomSort41.DisplayArray(words);
        }
        public static void SortThread <T>(T[] arr, Func <T, T, bool> ordering)
        {
            var threadNumber = ++_threadNumber;

            new Thread(() =>
            {
                CustomSort41.SortArray(arr, ordering);
                OnSortFinish?.Invoke($"Sorting done in {threadNumber} thread");
            }).Start();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("TASK 04");
            int select;

            do
            {
                Console.Clear();
                Console.WriteLine("Select the number of a task:");
                Console.WriteLine("1. CUSTOM SORT.");
                Console.WriteLine("2. CUSTOM SORT DEMO.");
                Console.WriteLine("3. SORTING UNIT.");
                Console.WriteLine("4. NUMBER ARRAY SUM.");
                Console.WriteLine("5. TO INT OR NOT TO INT?");
                Console.WriteLine("6. I SEEK YOU.");
                Console.WriteLine("0. Exit.");
                if (int.TryParse(Console.ReadLine(), out select))
                {
                    switch (select)
                    {
                    case 1:
                        Console.Clear();
                        Console.WriteLine("Task 4.1 CUSTOM SORT:");
                        CustomSort41.CustomSort();
                        Console.ReadKey();
                        break;

                    case 2:
                        Console.Clear();
                        Console.WriteLine("Task 4.2 CUSTOM SORT DEMO:");
                        CustomSortDemo42.CustomSortDemo();
                        Console.ReadKey();
                        break;

                    case 3:
                        Console.Clear();
                        Console.WriteLine("Task 4.3 SORTING UNIT:");
                        SortingUnit43.SortingUnit();
                        Console.ReadKey();
                        break;

                    case 4:
                        Console.Clear();
                        Console.WriteLine("Task 4.4 NUMBER ARRAY SUM:");
                        NumberArraySum44.NumberArraySum();
                        Console.ReadKey();
                        break;

                    case 5:
                        Console.Clear();
                        Console.WriteLine("Task 4.5 TO INT OR NOT TO INT?:");
                        ToIntOrNotToInt45.ToIntOrNotToInt();
                        Console.ReadKey();
                        break;

                    case 6:
                        Console.Clear();
                        Console.WriteLine("Task 4.6 I SEEK YOU:");
                        Console.WriteLine("Task is not completed\n" +
                                          "Dear trainer,\n" +
                                          "I am in my fourth year at university, so the exam time has been shifted to early December. " +
                                          "I have to take exams every day, so unfortunately, I did not have enough time for this task." +
                                          " After I pass the exams, I will definitely complete it. Please excuse me.\n" +
                                          "Yours sincerely,\n" +
                                          "Nikita Vasin\n" +
                                          "Student");
                        Console.ReadKey();
                        break;

                    case 0:
                        break;

                    default:
                        Console.WriteLine("Wrong number!");
                        Console.ReadKey();
                        break;
                    }
                }
                else
                {
                    select = -1;
                    Console.WriteLine("Invalid input!");
                    Console.ReadKey();
                }
            }while (select != 0);
        }