Esempio n. 1
0
        /// <summary>
        /// A method changes the infix expression to the postfix expression. </summary>
        /// <param name="separated"> A infix notation expression InputElement list </param>
        /// <param name="ret"> A postfix notation InputElement queue </param>
        /// <returns> A result status of expression's reorder </returns>
        private bool ChangeToPostfix(IEnumerable <InputElement> separated, out Queue <InputElement> ret)
        {
            Queue <InputElement> postfix   = new Queue <InputElement>();
            Stack <InputElement> operators = new Stack <InputElement>();

            try
            {
                foreach (InputElement token in separated)
                {
                    if (token is Literal)
                    {
                        postfix.Enqueue(token);
                    }
                    else if (token.CompareTo("(") == 0)
                    {
                        operators.Push(token);
                    }
                    else if (token.CompareTo(")") == 0)
                    {
                        InputElement topToken = operators.Pop();
                        while (topToken.CompareTo("(") != 0)
                        {
                            postfix.Enqueue(topToken);
                            topToken = operators.Pop();
                        }
                    }
                    else
                    {
                        while ((operators.Count > 0) &&
                               (OperatorsPriority[operators.Peek()].Priority >= OperatorsPriority[token].Priority))
                        {
                            postfix.Enqueue(operators.Pop());
                        }

                        operators.Push(token);
                    }
                }

                while (operators.Count > 0)
                {
                    postfix.Enqueue(operators.Pop());
                }
            }
#pragma warning disable CS0168
            catch (Exception e)
#pragma warning restore CS0168
            {
                postfix.Clear();
                ret = postfix;
                return(false);
            }

            ret = postfix;
            return(true);
        }