Example #1
0
        static void Main(string[] args)
        {
            //-----------------------------------------------------------------------------------------------------------------
            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
            Rectangle rect   = new Rectangle(5, 4);
            Rectangle square = new Rectangle(5);
            Circle    circle = new Circle(5);

            rect.Print();
            square.Print();
            circle.Print();
            Console.ReadLine();
            //-----------------------------------------------------------------------------------------------------------------
            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
            //Создание коллекции класса ArrayList
            ArrayList al = new ArrayList();

            //Добавление объектов в коллекцию
            al.Add(333);
            al.Add(123.123);
            al.Add("строка");
            al.Add(11);
            al.Add("q");
            al.Add(1456);
            al.Add(0.5);
            //Сортировка по типу данных
            foreach (object o in al)
            {
                string type = o.GetType().Name;
                if (type == "Int32")
                {
                    Console.WriteLine("Целое число: " + o.ToString());
                }
                else if (type == "String")
                {
                    Console.WriteLine("Строка: " + o.ToString());
                }
                else
                {
                    Console.WriteLine("Другой тип: " + o.ToString());
                }
            }

            Console.ReadLine();
            //-----------------------------------------------------------------------------------------------------------------
            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
            //Создание коллекции класса List<Figure>
            List <Figure> li = new List <Figure>();

            //Добавление объектов в коллекцию
            li.Add(rect);
            li.Add(square);
            li.Add(circle);
            //Сортировка по площади фигуры
            var orderedNumbers = from i in li
                                 orderby i.Area()
                                 select i;

            foreach (Figure i in orderedNumbers)
            {
                Console.WriteLine(i);
            }
            //-----------------------------------------------------------------------------------------------------------------
            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
            Console.WriteLine("\nМатрица");
            Matrix <Figure> matrix = new Matrix <Figure>(3, 3, 3, new FigureMatrixCheckEmpty());

            matrix[0, 0, 0] = rect;
            matrix[1, 1, 1] = square;
            matrix[2, 2, 2] = circle;
            Console.WriteLine(matrix.ToString());
            Console.ReadLine();
            //-----------------------------------------------------------------------------------------------------------------
            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
            SimpleStack <Figure> stack = new SimpleStack <Figure>();

            //добавление данных в стек
            stack.Push(rect);
            stack.Push(square);
            stack.Push(circle);
            //чтение данных из стека
            while (stack.Count > 0)
            {
                Figure f = stack.Pop();
                Console.WriteLine(f);
            }
            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            //создание экзмепляров классов
            var rect   = new Rectangle(4, 5);
            var square = new Square(6);
            var circle = new Circle(10);

            //создание обобщенного списка и вывод
            var arrayList = new ArrayList {
                square, circle, rect
            };

            Console.WriteLine("Через необобщенную коллекцию ArrayList ");
            Console.WriteLine("До сортировки:");

            foreach (var figure in arrayList)
            {
                Console.WriteLine(((Figure)figure).area());
            }

            // сортировка и вывод
            arrayList.Sort();
            Console.WriteLine("\nПосле сортировки:");

            foreach (var figure in arrayList)
            {
                Console.WriteLine(((Figure)figure).area());
            }

            Console.WriteLine("\nТо же самое, но только через обобщенную коллекцию List<T> ");
            //создание необобщенного списка, сортировка и вывод
            var list = new List <Figure> {
                square, circle, rect
            };

            list.Sort();
            foreach (var figure in list)
            {
                Console.WriteLine(figure.area());
            }


            //создание разреженной матрицы

            Console.WriteLine("\nМатрица");
            var matrix = new Matrix <Figure>(2, 2, 2, new FigureMatrixCheckEmpty());

            for (var i = 0; i < 2; i++)
            {
                for (var j = 0; j < 2; j++)
                {
                    for (var k = 0; k < 2; k++)
                    {
                        matrix[i, j, k] = list[new Random().Next(0, 2)];
                    }
                }
            }
            Console.WriteLine(matrix.ToString());


            Console.WriteLine("Работаем со стеком: ");
            var stack = new SimpleStack <Figure>();

            stack.push(rect);
            stack.push(square);
            stack.push(circle);
            stack.add(circle);


            Console.WriteLine("\nВывод стэка простым перебором обычного списка по принципу FIFO: ");
            foreach (var element in stack)
            {
                Console.WriteLine(element.ToString());
            }

            Console.WriteLine("\nВывод стэка методом pop() по принципу LIFO: ");

            while (stack.count > 0)
            {
                Console.WriteLine(stack.pop().ToString());
            }
        }