Esempio n. 1
0
 //后序表达式求值
 private string PostfixEval(string postExp)
 {
     Stack<double> result = new Stack<double>();
     double x, y;
     for (int i = 0; i <= postExp.Length - 1; i++)
     {
         Operator op = new Operator(postExp[i]);
         if (IsDigit(postExp[i]))
         {
             string operand = "";
             while ((i <= postExp.Length - 1) && IsDigit(postExp[i]))
             {
                 operand += postExp[i];
                 i++;
             }
             i--;
             result.Push(Convert.ToDouble(operand));
             continue;
         }
         if (postExp[i] == ' ') continue;
         if (op.isSingle)
         {
             y = result.Pop();
             result.Push(op.Parse(y));
         }
         else
         {
             y = result.Pop();
             x = result.Pop();
             result.Push(op.Parse(x, y));
         }
     }
     return Convert.ToString(result.Peek());
 }