Esempio n. 1
0
        private void GetPostfix()
        {
            foreach (string character in Function)
            {
                if (!Operations.Contains(character))
                {
                    PostFix.Enqueue(character == "x" ? character : double.Parse(character));
                }

                if (character is "(")
                {
                    Stack.Push(character);
                }
                else if (character is ")")
                {
                    while (Stack.Count != 0)
                    {
                        if (Stack.Peek() == "(")
                        {
                            Stack.Pop();
                            break;
                        }
                        else
                        {
                            PostFix.Enqueue(Stack.Pop());
                        }
                    }
                }

                if (IsOperator(character))
                {
                    if (Stack.Count == 0)
                    {
                        Stack.Push(character);
                    }
                    else
                    {
                        if (GetPrior(character) > GetPrior(Stack.Peek()))
                        {
                            Stack.Push(character);
                        }
                        else if ((GetPrior(character) == GetPrior(Stack.Peek())) && (character == "^"))
                        {
                            Stack.Push(character);
                        }
                        else
                        {
                            while ((Stack.Count != 0) && (GetPrior(character) <= GetPrior(Stack.Peek())))
                            {
                                PostFix.Enqueue(Stack.Pop());
                            }

                            Stack.Push(character);
                        }
                    }
                }
            }

            while (Stack.Count != 0)
            {
                PostFix.Enqueue(Stack.Pop());
            }
        }