示例#1
0
        static void Main(string[] args)
        {
            var n = 100;

            var AllTime = 0L;
            var stack   = new MyStack <int>(4, 2);

            for (var j = 0; j < 50; ++j)
            {
                var watch = Stopwatch.StartNew();

                for (var i = 0; i != n; ++i)
                {
                    stack.Push(i);
                }

                for (var i = 0; i != n; ++i)
                {
                    stack.Peek();
                    stack.Pop();
                }

                watch.Stop();
                AllTime += watch.ElapsedMilliseconds;
            }

            Console.WriteLine(AllTime / 50);

            while (SonertTop)
            {
                try
                {
                    ReversePolish(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("Exit");
                    break;
                }
            }

            Console.ReadKey();
        }
示例#2
0
        private static void ReversePolish(string sample)
        {
            var result = string.Empty;

            var stack = new MyStack <char>();

            for (var i = 0; i < sample.Length; i++)
            {
                if (char.IsNumber(sample[i]))
                {
                    if (i != 0 && !char.IsNumber(sample[i - 1]))
                    {
                        result += ' ';
                    }

                    result += sample[i];
                }
                else
                {
                    switch (sample[i])
                    {
                    case '(':
                        stack.Push(sample[i]);
                        break;

                    case ')':
                    {
                        while (stack.Peek() != '(' && Priority(stack.Peek()) >= Priority(sample[i]))
                        {
                            result += $" {stack.Peek()}";
                            stack.Pop();
                        }

                        stack.Pop();
                        break;
                    }

                    default:
                    {
                        while (!stack.Empty() && Priority(stack.Peek()) >= Priority(sample[i]))
                        {
                            result += $" {stack.Peek()}";
                            stack.Pop();
                        }

                        stack.Push(sample[i]);
                        break;
                    }
                    }
                }
            }

            while (!stack.Empty())
            {
                result += $" {stack.Peek()}";

                stack.Pop();
            }

            Console.WriteLine(result);
        }