예제 #1
0
        static void Main(string[] args)
        {
            var stack = new MyStack();

            stack.Push(1);
            stack.Push(2);
            stack.Push(3);

            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
        }
예제 #2
0
        static void Main(string[] args)
        {
            MyStack stack = new MyStack();

            for (int i = 0; i < 4; i++)
            {
                stack.Push(i);
            }

            stack.Pop();
            stack.Pop();
            stack.Pop();
            stack.Pop();
            Console.WriteLine(stack.Peek());
        }
        public static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(" ");

            MyStack <int> myStack = new MyStack <int>();

            while (input[0] != "END")
            {
                try
                {
                    switch (input[0])
                    {
                    case "Pop":
                        myStack.Pop();
                        break;

                    case "Push":
                        int[] elements = string.Join(" ", input.Skip(1)).Split(", ").Select(int.Parse).ToArray();

                        elements.ToList().ForEach(x => myStack.Push(x));
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                input = Console.ReadLine().Split(" ");
            }

            Console.WriteLine(myStack);
        }
예제 #4
0
        static void Main(string[] args)
        {
            var stack = new MyStack <string>();

            var command = Console.ReadLine();

            while (command != "END")
            {
                var sep = command.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);


                if (sep[0] == "Push")
                {
                    for (int i = 1; i < sep.Length; i++)
                    {
                        stack.Push(sep[i]);
                    }
                }
                else if (sep[0] == "Pop")
                {
                    stack.Pop();
                }
                command = Console.ReadLine();
            }

            foreach (var item in stack)
            {
                Console.WriteLine(item);
            }
            foreach (var item in stack)
            {
                Console.WriteLine(item);
            }
        }
        public static void Main()
        {
            var stack = new MyStack <int>();

            stack.Push(1);
            stack.Push(4);
            stack.Push(5);
            stack.Push(8);
            stack.Push(11);

            Console.WriteLine(stack.Count);

            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());

            Console.WriteLine(stack.Count);

            stack.ForEach(x => Console.Write(x + " "));
        }
예제 #6
0
        private int Evaluate()
        {
            var ProcessStack = new MyStack <int>(EvaluateList.Count);

            foreach (string part in EvaluateList)
            {
                if (elements.Contains(part))
                {
                    int result = 0;
                    switch (part)
                    {
                    case "*":
                        result = ProcessStack.Pop() * ProcessStack.Pop();
                        break;

                    case "/":
                        var divisor   = ProcessStack.Pop();
                        var numerator = ProcessStack.Pop();
                        result = numerator / divisor;
                        break;

                    case "+":
                        result = ProcessStack.Pop() + ProcessStack.Pop();
                        break;

                    case "-":
                        result = -ProcessStack.Pop() + ProcessStack.Pop();
                        break;
                    }
                    ProcessStack.Push(result);
                }
                else
                {
                    ProcessStack.Push(Convert.ToInt32(part));
                }
            }
            if (ProcessStack.Length != 1)
            {
                throw new InvalidOperationException();
            }
            return(ProcessStack.Pop());
        }
예제 #7
0
파일: Program.cs 프로젝트: viliyani/SoftUni
        public static void Main(string[] args)
        {
            MyStack stack = new MyStack();

            string[] info = Console.ReadLine().Split();

            foreach (var item in info.Skip(1))
            {
                int element = int.Parse(item.Replace(",", ""));
                stack.Push(element);
            }

            string input = String.Empty;

            while ((input = Console.ReadLine()) != "END")
            {
                string[] options = input.Split();

                try
                {
                    if (options[0] == "Pop")
                    {
                        Console.WriteLine(stack.Pop());
                    }
                    else if (options[0] == "Push")
                    {
                        stack.Push(int.Parse(options[1]));
                    }
                }
                catch (InvalidOperationException ioe)
                {
                    Console.WriteLine(ioe.Message);
                }
            }

            foreach (var item in stack)
            {
                Console.WriteLine(item);
            }
            foreach (var item in stack)
            {
                Console.WriteLine(item);
            }
        }
예제 #8
0
        static void Main(string[] args)
        {
            MyStack <string> myCollection = new MyStack <string>();

            while (true)
            {
                string input = Console.ReadLine();
                if (input == "END")
                {
                    break;
                }

                string[] commandLine = input.Split(new string[] { " ", ", " }, StringSplitOptions.RemoveEmptyEntries);
                string   command     = commandLine[0];

                try
                {
                    if (command == "Push")
                    {
                        for (int i = 1; i < commandLine.Length; i++)
                        {
                            myCollection.Push(commandLine[i]);
                        }
                    }
                    else if (command == "Pop")
                    {
                        myCollection.Pop();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            foreach (var item in myCollection)
            {
                Console.WriteLine(item);
            }
            foreach (var item in myCollection)
            {
                Console.WriteLine(item);
            }
        }