Exemple #1
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());
        }
Exemple #2
0
        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);
            }
        }
Exemple #3
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);
            }
        }
        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 + " "));
        }
        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());
        }