コード例 #1
0
        public static double EvaluatePostFixExpression(QueueUsingDoublyLinkedList <string> postfix)
        {
            StackUsingLinkedList <string> stack = new StackUsingLinkedList <string>();
            var    count  = postfix.Count;
            double answer = 0;

            for (int i = 0; i < count; i++)
            {
                if (stack.Count >= 2 && postfix.Peek().Length == 1 && IsOperator(postfix.Peek()[0]) == true)
                {
                    double x = 0;
                    double y = 0;
                    if (Double.TryParse(stack.Peek(), out x))
                    {
                        x = Convert.ToDouble(stack.Pop());
                    }
                    if (Double.TryParse(stack.Peek(), out y))
                    {
                        y = Convert.ToDouble(stack.Pop());
                    }
                    double result = Calculate(y, postfix.Dequeue()[0], x);
                    stack.Push(result.ToString());
                }

                else
                {
                    stack.Push(postfix.Dequeue());
                }
            }

            if (stack.Count != 0)
            {
                if (double.TryParse(stack.Peek(), out answer) == true)
                {
                    answer = Convert.ToDouble(stack.Pop());
                }

                else
                {
                    if (stack.Count != 1)
                    {
                        stack.Pop();
                    }
                    if (double.TryParse(stack.Peek(), out answer) == true)
                    {
                        answer = Convert.ToDouble(stack.Pop());
                    }
                }
            }

            return(answer);
        }
コード例 #2
0
        private void TbInfix_TextChanged(object sender, TextChangedEventArgs e)
        {
            QueueUsingDoublyLinkedList <string> temp    = InfixToPostfixConverter.ConvertToPostfix(TbInfix.Text);
            QueueUsingDoublyLinkedList <string> postfix = new QueueUsingDoublyLinkedList <string>();
            var sb    = new StringBuilder();
            int count = temp.Count;

            for (int x = 0; x < count; x++)
            {
                var y = temp.Dequeue();
                postfix.Enqueue(y);
                sb.Append(y);
            }
            TbPostfix.Text = sb.ToString();

            TbResult.Text = PostfixEvaluator.EvaluatePostFixExpression(postfix).ToString();
        }
コード例 #3
0
        public static QueueUsingDoublyLinkedList <string> ConvertToPostfix(string exp)
        {
            var postfix = new QueueUsingDoublyLinkedList <string>();
            StackUsingLinkedList <char> stack = new StackUsingLinkedList <char>();
            StringBuilder num = new StringBuilder();

            for (int x = 0; x < exp.Length; x++)
            {
                if (Char.IsDigit(exp[x]) == true)
                {
                    if (x != exp.Length - 1 && (Char.IsDigit(exp[x + 1]) == true || exp[x + 1] == '.' || exp[x + 1] == ','))
                    {
                        num.Append(exp[x]);
                    }

                    else
                    {
                        num.Append(exp[x]);
                        postfix.Enqueue(num.ToString());
                        num.Clear();
                        if (x != exp.Length - 1 && exp[x + 1] == '(')
                        {
                            while (!stack.IsEmpty() && IsOperator(stack.Peek()) && GreaterOrEqualPrecedence('*', stack.Peek()) == true)
                            {
                                postfix.Enqueue(stack.Pop().ToString());
                            }
                            stack.Push('*');
                        }
                    }
                }

                else if (exp[x] == '.')
                {
                    num.Append(exp[x]);
                }

                else if (exp[x] == '-')
                {
                    if (x == 0 || IsOperator(exp[x - 1]) == true || exp[x - 1] == '(')
                    {
                        if (exp.Length != 1 && x != exp.Length - 1 && exp[x + 1] == '(')
                        {
                            exp = exp.Substring(0, x + 1) + '1' + exp.Substring(x + 1);
                        }

                        num.Append(exp[x]);
                    }

                    else
                    {
                        while (!stack.IsEmpty() && IsOperator(stack.Peek()) && GreaterOrEqualPrecedence(exp[x], stack.Peek()) == true)
                        {
                            postfix.Enqueue(stack.Pop().ToString());
                        }
                        stack.Push(exp[x]);
                    }
                }

                else if (exp[x] == '(')
                {
                    stack.Push(exp[x]);
                }

                else if (IsOperator(exp[x]) == true)
                {
                    while (!stack.IsEmpty() && IsOperator(stack.Peek()) && GreaterOrEqualPrecedence(exp[x], stack.Peek()) == true)
                    {
                        postfix.Enqueue(stack.Pop().ToString());
                    }
                    stack.Push(exp[x]);
                }

                else if (exp[x] == ')')
                {
                    while (stack.Peek() != '(')
                    {
                        postfix.Enqueue(stack.Pop().ToString());
                    }

                    stack.Pop();

                    if (x != exp.Length - 1 && exp[x + 1] == '(')
                    {
                        while (!stack.IsEmpty() && IsOperator(stack.Peek()) && GreaterOrEqualPrecedence('*', stack.Peek()) == true)
                        {
                            postfix.Enqueue(stack.Pop().ToString());
                        }
                        stack.Push('*');
                    }
                }
            }

            while (!stack.IsEmpty())
            {
                postfix.Enqueue(stack.Pop().ToString());
            }

            return(postfix);
        }