Exemple #1
0
        private string ShuntingYard(string input)
        {
            LinkedListStack <char> operators = new LinkedListStack <char>();
            Queue <char>           output    = new LinkedListQueue <char>();

            foreach (var i in input)
            {
                if (Char.IsNumber(i))
                {
                    output.Enqueue(i);
                }
                if (IsOperator(i))
                {
                    while (!operators.IsEmpty() && OperatorPrecedence(operators.Peek()) >= OperatorPrecedence(i))
                    {
                        output.Enqueue(operators.Pop());
                    }
                    operators.Push(i);
                }
                if (i == '(')
                {
                    operators.Push(i);
                }
                if (i == ')')
                {
                    while (operators.Peek() != '(')
                    {
                        output.Enqueue(operators.Pop());
                    }
                    operators.Pop();
                }
            }
            while (!operators.IsEmpty())
            {
                output.Enqueue(operators.Pop());
            }

            return(new string(output.ToArray()));
        }