コード例 #1
0
        // Method: ConvertToPostfix: converts the infix expression to postfix notation
        public string ConvertToPostfix(string UserInput)
        {
            StackInheritance stack = new StackInheritance();
            stack.Push('(');
            infix.Append(')');
            infix.Insert(0,UserInput,1);
            while(!(stack.IsEmpty()))
            {
                for(int i = 0; i < infix.Length; i++)
                {

                    if ((infix[i] == '0') | (infix[i] == '1') | (infix[i] == '2') | (infix[i] == '3') | (infix[i] == '4') | (infix[i] == '5') | (infix[i] == '6') | (infix[i] == '7') | (infix[i] == '8') | (infix[i] == '9'))

                    {
                        postfix.Append(infix[i]);

                    }

                    else if (infix[i] == '(')
                    {
                        stack.Push(infix[i]);

                    }
                    else if (stack.IsOperator(infix[i])) // checks if infix is operator, not to be confused with the stack being checked.
                    {
                        // While there is an operator at the top of the stack
                        while (stack.IsOperator((char)stack.Peek()))
                        {
                            // if the operator at the top of the stack is equal or greater precedence
                            // then pop the operator at the top of the stack
                            if (stack.Precedence(infix[i], (char)stack.Peek()))
                            {
                                postfix.Append(stack.Pop());

                            }

                            // else append the operator from infix to postfix
                            //else
                            {

                            }
                        }
                        stack.Push(infix[i]);
                    }
                    else if(infix[i] == ')')
                    {
                        while (stack.IsOperator((char)stack.Peek()))
                        {
                            postfix.Append(stack.Pop());
                        }
                        Console.WriteLine("The character you are about to Pop is:{0}", stack.Peek());

                        char parenthesisCheck = (char)stack.Peek();
                        if (parenthesisCheck == '(')
                        {
                            stack.Pop();

                        }
                        else
                        {
                            Console.WriteLine("For some reason you have not reached the final leftparenthesis");
                        }
                    }
                }
            }

            return postfix.ToString();
        }