コード例 #1
0
        //'=L4+L5+L6+L7
        bool CalcSum(int lineNo, Dictionary <int, CalTempObj> dict, out decimal amount)
        {
            var result = false;

            amount = 0;
            if (!dict.ContainsKey(lineNo))
            {
                logger.Debug("dict don't have key:{0}", lineNo);
                return(result);
            }

            var           expression = dict[lineNo].templateItem.c;
            List <string> lstParams  = CommonUtils.MatchPattern(expression, @"L([0-9]+)");
            List <string> lstOpratio = CommonUtils.MatchPattern(expression, "(\\+|\\-|\\*|/)");
            var           size       = lstParams.Count;

            if (size == 0)
            {
                return(result);
            }

            if (lstOpratio.Count + 1 != size)
            {
                logger.Error("error expression:" + expression);
                return(result);
            }

            result = size > 0;

            for (int i = 0; i < size; i++)
            {
                var rowStr = lstParams[i].Substring(1);
                var row    = 0;
                if (int.TryParse(rowStr, out row))
                {
                    if (!dict.ContainsKey(row))
                    {
                        logger.Debug("dict don't have key:{0}", row);
                        continue;
                    }
                    var tmp = dict[row].originAmount;
                    if (i > 0)
                    {
                        amount = SampleCalculator.CalcOpration(amount, lstOpratio[i - 1], tmp);
                    }
                    else
                    {
                        amount = tmp;
                    }
                }
            }
            logger.Debug("calcSum : [{0}]{1}{2}", lineNo, amount, expression);
            return(result);
        }
コード例 #2
0
        //'=SY("6301")-SY("1221")
        decimal CalcFormula(int lineNo, string formula)
        {
            if (string.IsNullOrEmpty(formula))
            {
                return(0);
            }
            if (!formula.StartsWith("="))
            {
                return(0);
            }

            List <string> lstMethod = CommonUtils.MatchPattern(formula, "(SY|C|SJY|SL)");

            if (lstMethod.Count == 0)
            {
                return(0);
            }

            List <string> lstParams  = CommonUtils.MatchPattern(formula, "(?<=\")[^\"^(^)]*(?=\")");
            List <string> lstOpratio = CommonUtils.MatchPattern(formula, "(\\+|\\-|\\*|/)");

            if (lstMethod.Count != lstParams.Count)
            {
                throw new FinanceException(FinanceResult.IMPERFECT_DATA, "公式错误");
            }

            decimal result = 0M;

            for (int i = 0; i < lstMethod.Count; i++)
            {
                var param = lstParams[i];
                var ids   = SampleCalculator.GetAccountSubjectIds(m_lstAso, param);
                if (ids.Count == 0)
                {
                    continue;
                }
                var method = lstMethod[i];
                var amount = CalcMethod(method, ids);
                if (i > 0)
                {
                    result = SampleCalculator.CalcOpration(result, lstOpratio[i - 1], amount);
                }
                else
                {
                    result = amount;
                }
            }

            logger.Debug("CalcFormula : [{0}]{1}{2}", lineNo, result, formula);
            return(result);
        }
コード例 #3
0
        string Calc(string formula)
        {
            if (string.IsNullOrEmpty(formula))
            {
                return("");
            }
            if (!formula.StartsWith("="))
            {
                return(formula);
            }

            List <string> lstMethod  = CommonUtils.MatchPattern(formula, "(Y|C|JY|DC|DY)");
            List <string> lstParams  = CommonUtils.MatchPattern(formula, "(?<=\")[^\"^(^)]*(?=\")");
            List <string> lstOpratio = CommonUtils.MatchPattern(formula, "(\\+|\\-|\\*|/)");

            if (lstMethod.Count != lstParams.Count)
            {
                throw new FinanceException(FinanceResult.IMPERFECT_DATA, "公式错误");
            }
            if (lstMethod.Count != lstOpratio.Count + 1)
            {
                throw new FinanceException(FinanceResult.IMPERFECT_DATA, "公式错误");
            }

            decimal result = 0M;

            for (int i = 0; i < lstMethod.Count; i++)
            {
                var param = lstParams[i];
                var ids   = SampleCalculator.GetAccountSubjectIds(m_lstAso, param);
                if (ids.Count == 0)
                {
                    continue;
                }
                var method = lstMethod[i];
                var amount = CalcMethod(method, ids);
                if (i > 0)
                {
                    result = SampleCalculator.CalcOpration(result, lstOpratio[i - 1], amount);
                }
                else
                {
                    result = amount;
                }
            }
            return(result.ToString("0.00"));
        }