Ejemplo n.º 1
0
        private object CalcInner()
        {
            int index = 0;
            //存放常数表
            List <decimal?> digit = new List <decimal?>();
            //备份表达式
            List <ExpressionToken> polandCalc = new List <ExpressionToken>(rePoland.Count);

            for (int i = 0; i < rePoland.Count; i++)
            {
                polandCalc.Add(rePoland[i]);
            }
            while (index < polandCalc.Count)
            {
                if (polandCalc.Count == 1 && polandCalc[0].Type == TokenType.Numeric)
                {
                    break;
                }
                ExpressionToken token = polandCalc[index];
                switch (token.Type)
                {
                case TokenType.Numeric:
                    if (polandCalc[index].Data == DBNull.Value)
                    {
                        digit.Add(null);
                    }
                    else
                    {
                        digit.Add(Convert.ToDecimal(polandCalc[index].Data));
                    }
                    index++;
                    break;

                //case TokenType.Variant:
                //    //将变量替换为常量
                //    polandCalc[index].Data = GetVariantValue(token);
                //    polandCalc[index].Type = TokenType.Numeric;
                //    break;
                case TokenType.Operator:
                    int paramCount = 2;                             //一般是二元式,需要二个参数
                    //函数
                    if (IsFunc((OperatorType)token.Data))
                    {
                        FunctionBase func = FuncFactory.GetFunc((int)(OperatorType)token.Data);
                        if (func == null)
                        {
                            throw new ExpressionException("不可识别的函数名.");
                        }
                        paramCount = func.ParamCount;
                    }
                    //计算前面两个数的值
                    if (digit.Count < paramCount)
                    {
                        throw new ExpressionException("缺少操作数", 1002);
                    }
                    //传入参数
                    decimal?[] data = new decimal?[paramCount];
                    for (int i = 0; i < paramCount; i++)
                    {
                        data[i] = digit[index - paramCount + i];
                    }
                    polandCalc[index - paramCount].Data = CalcOperator((OperatorType)token.Data, data);
                    //将参数从 repoland 中移除
                    for (int i = 0; i < paramCount; i++)
                    {
                        polandCalc.RemoveAt(index - i);
                        digit.RemoveAt(index - i - 1);
                    }
                    index -= paramCount;
                    break;

                default:
                    break;
                }
            }
            if (polandCalc.Count == 1)
            {
                switch (polandCalc[0].Type)
                {
                case TokenType.Numeric:
                    return(polandCalc[0].Data);

                default:
                    throw new ExpressionException("缺少操作数", 1002);
                }
            }
            else
            {
                throw new ExpressionException("缺少操作符或操作数", 1002);
            }
        }