예제 #1
0
        private static void Stack()
        {
            StackNodes <int>     stack      = new StackNodes <int>();
            StackArrays <string> stackArray = new StackArrays <string>();
            int limitItems;

            Console.WriteLine("Please type the amount of items you might want to create: ");
            limitItems = Convert.ToInt32(Console.ReadLine());

            for (int i = 1; i <= limitItems; i++)
            {
                stack.Push(new Node <int>(i));
            }
            Console.WriteLine(stack.Peek().Data);
            for (int i = 1; i <= limitItems; i++)
            {
                Console.WriteLine(stack.Pop().Data);
            }

            //for (int i = 1; i <= limitItems; i++)
            //{
            //    stackArray.Push(i.ToString());
            //}
            //Console.WriteLine(stackArray.Peek());
            //for (int i = 1; i <= limitItems; i++)
            //{
            //    Console.WriteLine(stackArray.Pop());
            //}
        }
예제 #2
0
        private static void PostFixCalculator(string[] tokens)
        {
            string            postFixExpression;
            StackArrays <int> stackArray = new StackArrays <int>();

            //Console.WriteLine("Please type the postfix expression ");
            //postFixExpression = Console.ReadLine().ToLower();

            foreach (string operand in tokens)
            {
                int value;
                if (int.TryParse(operand, out value))
                {
                    stackArray.Push(value);
                }
                else
                {
                    int rightToken = stackArray.Pop();
                    int leftToken  = stackArray.Pop();
                    int result     = default(int);
                    switch (operand)
                    {
                    case "*":
                        result = leftToken * rightToken;
                        break;

                    case "/":
                        result = leftToken / rightToken;
                        break;

                    case "-":
                        result = leftToken - rightToken;
                        break;

                    case "+":
                        result = leftToken + rightToken;
                        break;

                    default:
                        throw new ArgumentException("it's not a operand valid");
                        break;
                    }

                    stackArray.Push(result);
                }
            }
            Console.WriteLine($"Result: {stackArray.Peek()} ");
        }