Esempio n. 1
0
        private static void Main(string[] args)
        {
            int[] sortArray;
            int   choose;

            Console.WriteLine("enter 1 for bubble sorting");
            Console.WriteLine("enter 2 for insertion sorting");
            Console.WriteLine("enter 3 for stack simulation");
            Console.WriteLine("enter 4 for queue simulation");
            if (!int.TryParse(Console.ReadLine(), out choose) || choose < 1 || choose > 4)
            {
                Console.WriteLine("unsuported expression");
                Console.ReadKey();
                return;
            }
            switch (choose)
            {
            case 1:

                sortArray = new int[] { 1, 2, 6, 4, 3, 5, 6, 5, 7, 0 };
                BubbleSorter tryBubble = new BubbleSorter(sortArray);
                tryBubble.Sort();
                tryBubble.Print();
                Console.ReadLine();
                break;

            case 2:
                sortArray = new int[] { 1, 2, 6, 4, 3, 5, 6, 1233, -3333333, 5, 7, 0 };
                InsertionSorter tryInsertion = new InsertionSorter(sortArray);
                tryInsertion.Sort();
                tryInsertion.Print();
                Console.ReadLine();
                break;

            case 3:
                int     topush;
                int     topop;
                MyStack tryStack = new MyStack();
                for (int i = 1; i < 20; i++)
                {
                    Console.WriteLine("Please input next element to be pushed to stack");
                    if (!int.TryParse(Console.ReadLine(), out topush))
                    {
                        Console.WriteLine("unsuported expression");
                        Console.ReadKey();
                        return;
                    }
                    tryStack.Push(topush);

                    if (i % 2 == 0)
                    {
                        if (!tryStack.IsEmpty())
                        {
                            topop = tryStack.Pop();
                            Console.WriteLine("{0}", topop);
                        }
                        else
                        {
                            Console.WriteLine("Stack is empty");
                        }
                    }
                }

                /*   int topeek;
                 * if (!tryStack.isEmpty())
                 * {
                 *     Console.WriteLine("{0}", topeek);
                 * }
                 * else
                 * {
                 *     Console.WriteLine("Stack is empty");
                 * }*/

                break;



            case 4:
                MyQueue tryQueue    = new MyQueue();
                int     dequevaleue = 0;
                for (int j = 1; j < 40; j++)
                {
                    Console.WriteLine("plese input next element to be put in queue");
                    int value = Convert.ToInt32(Console.ReadLine());
                    tryQueue.Enqueue(value);
                    if (j % 2 == 0)
                    {
                        if (!tryQueue.IsEmpty())
                        {
                            dequevaleue = tryQueue.Dequeue();
                            Console.WriteLine(dequevaleue);
                        }
                        else
                        {
                            Console.WriteLine("queue is empty");
                        }
                    }
                }
                break;
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            //
            //*******************************************************//
            // BuBBLE SORT
            //*******************************************************//

            Console.WriteLine("************************");
            Console.WriteLine("******buble************");

            int[] array1 = new int[] { 5, 4, 1, 9, 1 };

            //BubbleSorter bubbleSorter = new BubbleSorter(array1);  ///создаем нову переменну bubbleSorter з типом ВubbleSorter
            Sorter       sorter       = new Sorter(array1);       ///создаем нову переменну bubbleSorter з типом ВubbleSorter
            BubbleSorter bubblesorter = new BubbleSorter(array1); ///создаем нову переменну bubbleSorter з типом ВubbleSorter


            bubblesorter.Print(array1);
            bubblesorter.Sort();    // визов метода Sort,з класса бабл сортер, передача йому значень масива для сортування.
                                    // він приймає значення, біжить по коду і результат друкуєм в тілі програми.
            sorter.Print(array1);


            //*******************************************************//
            // INSERTING SORT
            //*******************************************************//

            Console.WriteLine("************************");
            Console.WriteLine("******insert************");
            int[]           array2          = new int[] { 9, 6, 3, 1, 4 };
            InsertionSorter insertionsorter = new InsertionSorter(array2);

            sorter.Print(array2);
            insertionsorter.Sort();
            sorter.Print(array2);

            //*******************************************************//
            // Stack
            //*******************************************************//

            int add = 0;

            Console.WriteLine("************************");
            Console.WriteLine("******My Stack************");
            int[] array3 = new int[] { 0, 0, 0, 0, 0 };

            Buffer  buffer  = new Buffer(array3);
            MyStack mystack = new MyStack(array3);


            buffer.Print(array3);
            add = 789;                  // debug value
            mystack.Push(add);
            buffer.Print(array3);
            mystack.Peek();
            mystack.Pop();

            //*******************************************************//
            // CIrcular Q
            //*******************************************************//


            int Buffsize = 0;
            int put      = 0;
            int free1    = 1;
            int choise   = 0;

            Console.WriteLine("Enter the size of an stack");
            buffer.ParceD(ref Buffsize);           // parce size of stack
            int[] array4 = new int[Buffsize];      // define stack
            buffer.Print(array4);

            MyQueue myqueue = new MyQueue(array4);

            while (free1 > 0)
            {
                Console.WriteLine("To add value press :1, to delete value press :2, to exit press: 3 \n");

                buffer.ParceD(ref choise);
                switch (choise)
                {
                case 1:
                    Console.WriteLine("Enter what you want to add \n");
                    buffer.ParceD(ref put);                         //parce value which needs to be added
                    myqueue.Enqueue(put);
                    buffer.Print(array4);
                    break;

                case 2:

                    myqueue.Dequeue();
                    buffer.Print(array4);

                    break;

                case 3:

                {
                    free1 = 0;
                }
                break;
                }
            }

            Console.ReadLine();
        }