Exemple #1
0
        /// <summary>
        /// 後序轉結果的演算過程
        /// </summary>
        /// <param name="postfix">後序</param>
        /// <param name="stack">堆疊</param>
        private static void ComputeIterative(List <string> postfix, Stack <decimal> stack)
        {
            foreach (var item in postfix)
            {
                //判斷是不是單元或雙元運算子
                bool isBinary = OperatorController.GetInstance().GetBinaryMarks().Contains(item);

                bool isUnary = OperatorController.GetInstance().GetUnaryMarks().Contains(item);

                if (IsNumber(item))
                {
                    stack.Push(decimal.Parse(item));
                }
                else if (isBinary)
                {
                    //防呆
                    if (stack.Count < 2)
                    {
                        throw new Exception("後序有誤");
                    }

                    //計算
                    decimal number1 = stack.Pop();
                    decimal number2 = stack.Pop();
                    stack.Push(BinaryCompute(item, number2, number1));
                }
                else if (isUnary)
                {
                    //防呆
                    if (stack.Count < 1)
                    {
                        throw new Exception("後序有誤");
                    }

                    //計算
                    decimal number = stack.Pop();
                    stack.Push(UnaryCompute(item, number));
                }
                else
                {
                    Console.WriteLine($"item = {item}");
                    throw new Exception("無法識別的符號");
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// 中序轉後序的演算法
 /// </summary>
 /// <param name="infix">中序</param>
 /// <param name="result">結果</param>
 /// <param name="stack">堆疊</param>
 private void ComputeIterative(List <string> infix, List <string> result, Stack <string> stack)
 {
     foreach (var item in infix)
     {
         if (IsNumber(item))
         {
             result.Add(item);
         }
         else if (item.Equals("("))
         {
             stack.Push(item);
         }
         else if (item.Equals(")"))
         {
             while (stack.Count > 0 &&
                    !stack.Peek().Equals("("))
             {
                 result.Add(stack.Pop());
             }
             //無效運算式
             if (stack.Count > 0 &&
                 !stack.Peek().Equals("("))
             {
                 throw new Exception("無效運算式");
             }
             else
             {
                 stack.Pop();
             }
         }
         else
         {
             //限定成既有的運算符號。若輸入了未定義運算符會error
             while (stack.Count > 0 &&
                    OperatorController.GetInstance().GetPriority(item) <= OperatorController.GetInstance().GetPriority(stack.Peek()))
             {
                 result.Add(stack.Pop());
             }
             stack.Push(item);
         }
     }
 }
Exemple #3
0
        /// <summary>
        /// 單元運算
        /// </summary>
        /// <param name="op">運算規則的名稱</param>
        /// <param name="number">數字</param>
        /// <returns>運算結果</returns>
        private static decimal UnaryCompute(string op, decimal number)
        {
            var formula = OperatorController.GetInstance().GetUnaryFormula(op);

            return(formula(number));
        }