示例#1
0
文件: MyStack.cs 项目: Hardshok/Part1
 public void Push(T item)
 {
     if (!this.IsFull())
     {
         buffer[++top] = item;
     }
     else
     {
         MyServiceFunctions.WriteColorLine(ConsoleColor.Red, "\nStack is full. To add new element do 'Pop' first");
     }
 }
示例#2
0
文件: MyStack.cs 项目: Hardshok/Part1
        public override void Print()
        {
            Console.Write("Now Stack is: ");
            string Sum = "";

            for (int iCount = 0; iCount <= top; iCount++)
            {
                Sum += buffer[iCount] + "; ";
            }
            MyServiceFunctions.WriteColorLine(ConsoleColor.Green, "[ " + Sum + "]");
        }
示例#3
0
        public void Print()
        {
            Console.Write("Actual Buffer is: ");
            string Sum = "";

            for (int iCount = 0; iCount < arrayOfObjects.Length; iCount++)
            {
                Sum += ArrayOfObjects[iCount].ComparableItem + "; ";
            }
            MyServiceFunctions.WriteColorLine(ConsoleColor.Green, "[ " + Sum + "]");
        }
示例#4
0
文件: Program.cs 项目: Hardshok/Part1
 // ****** ARRAY SORTING ******
 static int[] InitUserArray()
 {
     Console.Write("Enter the SIZE of array: ");
     int[] userArray = new int[MyServiceFunctions.ReadAndValidate(false)]; // 'false' arg. means Zero is not allowed as the size of the array
     Console.WriteLine("\nEnter the array's elements (each number to separate by Enter):");
     for (int iCount = 0; iCount < userArray.Length; iCount++)
     {
         userArray[iCount] = MyServiceFunctions.ReadAndValidate();
     }
     return(userArray);
 }
示例#5
0
文件: MyStack.cs 项目: Hardshok/Part1
        public T Peek()
        {
            //string item = "";
            T item = default(T);

            if (!IsEmpty())
            {
                item = buffer[top];
            }
            else
            {
                MyServiceFunctions.WriteColorLine(ConsoleColor.Red, "\nStack is EMPTY. To get top element do 'Push' first");
            }
            return(item);
        }
示例#6
0
文件: MyQueue.cs 项目: Hardshok/Part1
 public override void Print()
 {
     Console.WriteLine("\nNow cycle buffer is:");
     for (int iCount = 0; iCount < buffer.Length; iCount++)
     {
         if (head == iCount)
         {
             Console.Write("              <-- head");
         }
         if (tail == iCount)
         {
             Console.Write("              <-- tail");
         }
         MyServiceFunctions.WriteColorLine(ConsoleColor.Green, "\r  Item " + iCount + ": " + buffer[iCount]);
     }
 }
示例#7
0
文件: MyQueue.cs 项目: Hardshok/Part1
 public void Enqueue(T item)  //Write element in queue
 {
     if (!IsFull())
     {
         buffer[tail++] = item;
         if (tail == buffer.Length)
         {
             tail = 0;
         }
         count++;
     }
     else
     {
         MyServiceFunctions.WriteColorLine(ConsoleColor.Red, "\nQueue is FULL. To write new element do 'Dequeue' first");
     }
 }
示例#8
0
文件: MyStack.cs 项目: Hardshok/Part1
        public T Pop()
        {
            T item = default(T);

            if (!this.IsEmpty())
            {
                item = buffer[top--];
                //buffer[top + 1] = ""; //just make item to be empty
                return(item);
            }
            else
            {
                MyServiceFunctions.WriteColorLine(ConsoleColor.Red, "\nStack is EMPTY. To get top element do 'Push' first");
            }
            return(item);
        }
示例#9
0
文件: MyQueue.cs 项目: Hardshok/Part1
        //Remove from queue (through out parameter, true if element got from queue, false otherwise)
        //public bool Dequeue(out string queueItem)
        public bool Dequeue(out T queueItem)
        {
            //queueItem = "";
            queueItem = default(T);
            bool bResult = false;

            if (!IsEmpty())
            {
                queueItem = buffer[head++];
                if (head == buffer.Length)
                {
                    head = 0;
                }
                count--;
                bResult = true;
            }
            else
            {
                MyServiceFunctions.WriteColorLine(ConsoleColor.Red, "\nQueue is EMPTY. Write new element first ('Enqueue')");
            }
            return(bResult);
        }
示例#10
0
文件: Program.cs 项目: Hardshok/Part1
        static void Main()
        {
            Console.Title = "SORT ARRAY METHODS, STACK AMD QUEUE";
            Console.SetWindowSize(80, 45);
            bool bMainMenu = true;

            do
            { // user can back to the main manu
                Console.Clear();
                MyServiceFunctions.WriteColorLine(ConsoleColor.Yellow, "Modes:\n 1 = Sort Array: Two Methods\n 2 = Stack API with array\n 3 = Queue API with array\n 4 = EXIT");
                Console.Write("Please enter your selection: ");
                switch (MyServiceFunctions.ReadAndValidate(false))
                {
                case 1:
                    #region **** TWO SORTONG METHODS FOR ARRAY ****
                    Console.Clear();
                    MyServiceFunctions.WriteColorLine(ConsoleColor.Blue, "*** Sort Array: Two Methods***\n ");
                    int[] userArray = InitUserArray();
                    BubbleSorter <int>    bublesorter     = new BubbleSorter <int>(userArray);
                    InsertionSorter <int> insertionSorter = new InsertionSorter <int>(userArray);
                    bool bRepeat = true;
                    do          // user can select sorting method again
                    {
                        MyServiceFunctions.WriteColorLine(ConsoleColor.Yellow, "\nSort method:\n 1 = Bubble sorting by Ascending\n 2 = Bubble sorting by Descending");
                        MyServiceFunctions.WriteColorLine(ConsoleColor.Yellow, " 3 = Insertion Sort by Ascending\n 4 = Insertion Sort by Descending\n 5 = Back to Main menu");
                        Console.Write("Please enter your selection: ");
                        switch (MyServiceFunctions.ReadAndValidate(false))
                        {
                        case 1:
                            MyServiceFunctions.WriteColorLine(ConsoleColor.Blue, "\n***Bubble sorting by Ascending***");
                            bublesorter.SortAscending();
                            bublesorter.Print();
                            break;

                        case 2:
                            MyServiceFunctions.WriteColorLine(ConsoleColor.Blue, "\n***Bubble sorting by Descending***");
                            bublesorter.SortDescending();
                            bublesorter.Print();
                            break;

                        case 3:
                            MyServiceFunctions.WriteColorLine(ConsoleColor.Blue, "\n***Insertion sort by Ascending***");
                            insertionSorter.SortAscending();
                            insertionSorter.Print();
                            break;

                        case 4:
                            MyServiceFunctions.WriteColorLine(ConsoleColor.Blue, "\n***Insertion sort by Descending***");
                            insertionSorter.SortDescending();
                            insertionSorter.Print();
                            break;

                        case 5:
                            bRepeat = false;
                            break;

                        default:
                            MyServiceFunctions.WriteColorLine(ConsoleColor.Red, "Invalid selection. Please select 1, 2, 3, 4, or 5");
                            break;
                        }
                    } while (bRepeat);
                    break;

                    #endregion
                case 2:
                    #region **** IMPLEMENT STACK ****
                    Console.Clear();
                    MyServiceFunctions.WriteColorLine(ConsoleColor.Blue, "*** Implement Stack ***\n");
                    Console.Write("Enter the SIZE of Stack: ");
                    MyStack <int> stack   = new MyStack <int>(MyServiceFunctions.ReadAndValidate(false)); // 'false' arg. means Zero is not allowed as the size of the array
                    bool          bAction = true;
                    do                                                                                    //user can repeat operations with Stack
                    {
                        MyServiceFunctions.WriteColorLine(ConsoleColor.Yellow, "\n 1 = Push\n 2 = Pop\n 3 = Peek\n 4 = State of Stack (IsEmpty, IsFull)\n 5 = Back to Main Menu");
                        Console.Write("Select operation for Stack: ");
                        switch (MyServiceFunctions.ReadAndValidate(false))
                        {
                        case 1:
                            Console.Write("\nEnter string to PUSH: ");
                            //stack.Push(Console.ReadLine());
                            stack.Push(MyServiceFunctions.ReadAndValidate());
                            stack.Print();
                            break;

                        case 2:
                            if (!stack.IsEmpty())
                            {
                                Console.Write("\nPOP gets the element: ");
                            }
                            MyServiceFunctions.WriteColorLine(ConsoleColor.Green, Convert.ToString(stack.Pop()));
                            stack.Print();
                            break;

                        case 3:
                            if (!stack.IsEmpty())
                            {
                                Console.Write("\nPEEK gets the top element in Stack: ");
                            }
                            MyServiceFunctions.WriteColorLine(ConsoleColor.Green, Convert.ToString((stack.Peek())));
                            stack.Print();
                            break;

                        case 4:
                            Console.WriteLine("\nStack state are:");
                            if (stack.IsEmpty())
                            {
                                MyServiceFunctions.WriteColorLine(ConsoleColor.White, " - Stack is empty");
                            }
                            else
                            {
                                MyServiceFunctions.WriteColorLine(ConsoleColor.White, " - Stack is not empty");
                            }
                            if (stack.IsFull())
                            {
                                MyServiceFunctions.WriteColorLine(ConsoleColor.White, " - Stack is full");
                            }
                            else
                            {
                                MyServiceFunctions.WriteColorLine(ConsoleColor.White, " - Stack is not full");
                            }
                            break;

                        case 5:
                            bAction = false;
                            break;

                        default:
                            MyServiceFunctions.WriteColorLine(ConsoleColor.Red, "Invalid selection. Please select 1, 2, 3, 4 or 5");
                            break;
                        }
                    } while (bAction);
                    break;

                    #endregion
                case 3:
                    #region **** IMPLEMENT CIRCULAR BUFFER QUEUE ****
                    Console.Clear();
                    MyServiceFunctions.WriteColorLine(ConsoleColor.Blue, "*** Implement Queue - Circular Buffer ***\n");
                    int queueItem;
                    Console.Write("Enter the SIZE of buffer: ");
                    MyQueue <int> userQueue    = new MyQueue <int>(MyServiceFunctions.ReadAndValidate(false));
                    bool          bRepeatQueue = true;
                    do        //user can repeat operations with Queue
                    {
                        MyServiceFunctions.WriteColorLine(ConsoleColor.Yellow, "\n 1 = Enqueue\n 2 = Dequeue\n 3 = State of Buffer (IsEmpty, IsFull)");
                        MyServiceFunctions.WriteColorLine(ConsoleColor.Yellow, " 4 = Print Buffer\n 5 = Back to Main menu");
                        Console.Write("Select operation for Buffer: ");
                        switch (MyServiceFunctions.ReadAndValidate(false))
                        {
                        case 1:          // Enqueue
                            Console.Write("\nEnter the string you want to write in queue: ");
                            userQueue.Enqueue(MyServiceFunctions.ReadAndValidate());
                            userQueue.Print();
                            break;

                        case 2:          // Dequeue
                            if (userQueue.Dequeue(out queueItem))
                            {
                                Console.Write("\nThe element takken from queue is: ");
                                MyServiceFunctions.WriteColorLine(ConsoleColor.Green, Convert.ToString(queueItem));
                                userQueue.Print();
                            }
                            break;

                        case 3:          // Buffer's state (IsEmpty, IsFull)
                            Console.WriteLine("\nBuffer state are:");
                            if (userQueue.IsEmpty())
                            {
                                MyServiceFunctions.WriteColorLine(ConsoleColor.White, " - Queue is empty");
                            }
                            else
                            {
                                MyServiceFunctions.WriteColorLine(ConsoleColor.White, " - Queue is not empty");
                            }
                            if (userQueue.IsFull())
                            {
                                MyServiceFunctions.WriteColorLine(ConsoleColor.White, " - Queue is full");
                            }
                            else
                            {
                                MyServiceFunctions.WriteColorLine(ConsoleColor.White, " - Queue is not full");
                            }
                            break;

                        case 4:
                            userQueue.Print();
                            break;

                        case 5:
                            bRepeatQueue = false;
                            break;

                        default:
                            MyServiceFunctions.WriteColorLine(ConsoleColor.Red, "Invalid selection. Please select 1, 2, 3, 4, or 5");
                            break;
                        }
                    } while (bRepeatQueue);
                    break;

                    #endregion
                case 4:
                    bMainMenu = false;
                    break;

                default:
                    MyServiceFunctions.WriteColorLine(ConsoleColor.Red, "Invalid selection. Please select 1, 2, 3, or 4");
                    break;
                }
            } while (bMainMenu);
            MyServiceFunctions.WriteColorLine(ConsoleColor.White, "\nPress any key to exit...");
            Console.ReadKey();
        }