Esempio n. 1
0
        /// <summary>
        /// Method to convert an Infix expr to Prefix (Polish Notation). This method uses 2 stacks to hold the values.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string ConvertInfixToPrefix(string input)
        {
            StackUsingLL <int> staging      = new StackUsingLL <int>();
            StackUsingLL <int> result       = new StackUsingLL <int>();
            StringBuilder      resultString = new StringBuilder();

            for (int i = input.Length - 1; i >= 0; i--)
            {
                if (input[i] == ')')
                {
                    staging.Push(input[i]);
                }
                else if (IsOperand(input[i]))
                {
                    result.Push(input[i]);
                }

                else if (input[i] == '(')
                {
                    while (staging.PeekTop() != ')')
                    {
                        result.Push(staging.Pop());
                    }
                    staging.Pop();
                }

                else
                {
                    if (!staging.IsEmpty() && CheckPrecedence(input[i]) >= CheckPrecedence(staging.PeekTop()))
                    {
                        staging.Push(input[i]);
                    }
                    else if (!staging.IsEmpty() && CheckPrecedence(input[i]) < CheckPrecedence(staging.PeekTop()))
                    {
                        result.Push(staging.Pop());
                        staging.Push(input[i]);
                    }
                    else if (staging.IsEmpty())
                    {
                        staging.Push(input[i]);
                    }
                }
            }

            while (!staging.IsEmpty())
            {
                result.Push(staging.Pop());
            }

            while (!result.IsEmpty())
            {
                resultString.Append(Convert.ToChar(result.Pop()));
            }

            return(resultString.ToString());
        }
Esempio n. 2
0
        public string ConvertInfixToPostfix(string infix)
        {
            StackUsingLL <int> staging = new StackUsingLL <int>();
            StringBuilder      result  = new StringBuilder();

            for (int i = 0; i < infix.Length; i++)
            {
                //If scanned character is an operand
                if (IsOperand(infix[i]))
                {
                    result.Append(infix[i]);
                }

                //If scanned character is '(', push it to stack
                else if (infix[i] == '(')
                {
                    staging.Push('(');
                }

                //If scanned character is ')', pop all  operators from stack until '(' is found
                else if (infix[i] == ')')
                {
                    while (!staging.IsEmpty() && staging.PeekTop() != '(')
                    {
                        result.Append(Convert.ToChar(staging.Pop()));
                    }
                    //Invalid string check
                    if (!staging.IsEmpty() && staging.PeekTop() != '(')
                    {
                        return(null);
                    }
                    else
                    {
                        staging.Pop();
                    }
                }

                //If operator
                else
                {
                    while (!staging.IsEmpty() && (CheckPrecedence(infix[i]) <= CheckPrecedence(Convert.ToChar(staging.PeekTop()))))
                    {
                        result.Append(Convert.ToChar(staging.Pop()));
                    }
                    staging.Push(infix[i]);
                }
            }

            while (!staging.IsEmpty())
            {
                result.Append(Convert.ToChar(staging.Pop()));
            }

            return(result.ToString());
        }