示例#1
0
文件: Formula.cs 项目: JET-PEGA/Day01
        /// <summary>
        /// 計算
        /// </summary>
        /// <param name="Parameters">參數陣列</param>
        /// <returns></returns>
        public double Calculate(string[] Parameters)
        {
            double ret = default(double);

            Queue <FormulaElement> postfixQueue = new Queue <FormulaElement>(_postfixQueue); // 取得一個複製的後序佇列副本

            Stack <FormulaElement> stack = new Stack <FormulaElement>();

            while (postfixQueue.Count > 0)
            {
                FormulaElement element = postfixQueue.Dequeue();
                if (element.ElementType != FormulaElementType.Operator) // 若是運算元
                {
                    if (element.ElementType != FormulaElementType.Constant)
                    {
                        element.Constant = Convert.ToDouble(Parameters[element.Variable]);
                    }
                    stack.Push(element);
                }
                else // 若為運算子
                {
                    double operand1 = stack.Pop().Constant;
                    double operand2 = stack.Pop().Constant;
                    switch (element.Operator)
                    {
                    case '+':
                        stack.Push(new FormulaElement(FormulaElementType.Constant, operand2 + operand1));
                        break;

                    case '-':
                        stack.Push(new FormulaElement(FormulaElementType.Constant, operand2 - operand1));
                        break;

                    case '*':
                        stack.Push(new FormulaElement(FormulaElementType.Constant, operand2 * operand1));
                        break;

                    case '/':
                        stack.Push(new FormulaElement(FormulaElementType.Constant, operand2 / operand1));
                        break;
                    }
                }
            }
            ret = stack.Pop().Constant;
            return(ret);
        }
示例#2
0
文件: Formula.cs 项目: JET-PEGA/Day01
        /// <summary>
        /// 將中序佇列轉換成後序佇列
        /// </summary>
        /// <param name="infixQueue"></param>
        /// <returns></returns>
        private Queue <FormulaElement> ConvertToPostfixQueue(Queue <FormulaElement> infixQueue)
        {
            Stack <FormulaElement> stack        = new Stack <FormulaElement>(); // 暫存堆疊
            Queue <FormulaElement> postfixQueue = new Queue <FormulaElement>(); // 存放後序運算式的佇列

            string number = string.Empty;

            // 依序處理中序運算式的佇列的每一個元素
            while (infixQueue.Count > 0)
            {
                FormulaElement element = infixQueue.Dequeue();

                if (element.ElementType == FormulaElementType.Constant || element.ElementType == FormulaElementType.Variable)
                {
                    postfixQueue.Enqueue(element);
                }
                else
                {
                    switch (element.Operator)
                    {
                    case '(':
                        stack.Push(element);
                        break;

                    case ')':
                        while (stack.Count > 0)
                        {
                            if (stack.Peek().Operator != '(')
                            {
                                postfixQueue.Enqueue(stack.Pop());
                            }
                            else
                            {
                                stack.Pop();
                                //break; // Bug 就在這裡 請移除註解,增加break; 即可修復Bug
                            }
                        }
                        break;

                    default:
                        if (stack.Count == 0)     // 堆疊為空時
                        {
                            stack.Push(element);
                        }
                        else
                        {
                            while (stack.Count > 0)
                            {
                                if (element.IsGreaterThan(stack.Peek()))
                                {
                                    break;
                                }
                                else
                                {
                                    postfixQueue.Enqueue(stack.Pop());
                                }
                            }
                            stack.Push(element);
                        }
                        break;
                    }
                }
            }

            // 將運算子堆疊
            while (stack.Count > 0)
            {
                postfixQueue.Enqueue(stack.Pop());
            }

            return(postfixQueue);
        }