コード例 #1
0
        protected override void ParseText()
        {
            var expression = Text.Trim().Replace(",", ".");

            if (string.IsNullOrWhiteSpace(expression))
            {
                Value  = DefaultValue;
                Result = DefaultValue;
            }
            else
            {
                try
                {
                    _suppressEvent = true;
                    Result         = _calculator.Evaluate(expression);
                    Value          = Result;
                }
                catch (DivideByZeroException)
                {
                    // ignore it
                }
                catch (SyntaxException)
                {
                    // ignore it
                }
                finally
                {
                    _suppressEvent = false;
                }
            }
        }
コード例 #2
0
        public void Calc()
        {
            var w = new Stopwatch();

            w.Start();
            var calc = new CalcContext <decimal>();

            calc.Constants.Add("j", 5);
            calc.Constants.Add("j12", 2);
            calc.Constants.Add("j1", 3);
            Console.WriteLine(calc.Evaluate("j"));
            Console.WriteLine(calc.Evaluate("j+j12"));
            Console.WriteLine(calc.Evaluate("j+j1/2+j"));
            w.Stop();
            Console.WriteLine(w.Elapsed);
        }
コード例 #3
0
ファイル: EcoCalc.cs プロジェクト: rodrigogalhardo/asms-md
        public IEnumerable<CoefficientValue> CalculateCoefficientValues(IEnumerable<IndicatorValue> indicatorValues, IEnumerable<Dossier> dossiers, IEnumerable<Coefficient> coefficients)
        {
            var coefficientValues = new List<CoefficientValue>();

            if (dossiers.Select(o => o.FieldsetId).Distinct().Count() > 1)
                throw new AsmsEx("in luna respectiva sunt dosare inregistrate la diferite seturi de campuri");

            if (dossiers.Select(o => o.MeasuresetId).Distinct().Count() > 1)
                throw new AsmsEx("in luna respectiva sunt dosare inregistrate la diferite seturi de masuri");

            //calculate each coefficient for all dossiers
            foreach (var coefficient in coefficients)
            {
                EvalSums(coefficient, indicatorValues);

                var calc = new CalcContext<decimal>();
                foreach (var dossier in dossiers)
                {
                    calc.Constants.Clear();
                    foreach (var indicatorValue in indicatorValues.Where(o => o.DossierId == dossier.Id))
                    {
                        calc.Constants.Add("i" + indicatorValue.IndicatorId, indicatorValue.Value);
                    }

                    coefficientValues.Add(new CoefficientValue
                                {
                                    CoefficientId = coefficient.Id,
                                    DossierId = dossier.Id,
                                    Value = Zero(() => calc.Evaluate(coefficient.Formula)),
                                });
                }
            }

            return coefficientValues;
        }
コード例 #4
0
        public void CalcMethod()
        {
            var calc = new CalcContext <decimal>();

            calc.Constants.Add("i2", 23);
            calc.Functions.Add("sumai", o => o);
            calc.Culture = CultureInfo.InvariantCulture;

            Console.WriteLine(calc.Evaluate("sumai(i2)"));
        }
コード例 #5
0
        public void DoCalc()
        {
            var z = eMathTextBox.Text;

            z = z.Replace(",", ".");

            try
            {
                var calc = new CalcContext <double>();
                Value = calc.Evaluate(z);
            }
            catch
            {
                //error
            }

            Modificato = false;
        }
コード例 #6
0
        public decimal?Calculate(BudgetCalculatorEquation equation)
        {
            StringBuilder expressionBuilder = new StringBuilder();

            foreach (var item in equation.Items)
            {
                switch (item.ValueType)
                {
                case CalculatorValueType.UserValue:
                    expressionBuilder.Append(item.Value.GetValueOrDefault(0));
                    break;

                case CalculatorValueType.Operator:
                    expressionBuilder.AppendFormat(" {0} ", item.OperatorType.ToMath());
                    break;

                default:
                    if (item.Evaluator == null)
                    {
                        AttachEvaluator(item);
                    }
                    var value = item.CalculateValue();
                    if (value.HasValue)
                    {
                        expressionBuilder.Append(value.Value);
                    }
                    break;
                }
            }

            var expression = expressionBuilder.ToString().Replace(",", ".");

            try
            {
                var result = _calculator.Evaluate(expression);
                return(result);
            }
            catch (SyntaxException)
            {
                return(null);
            }
        }
コード例 #7
0
ファイル: EcoCalc.cs プロジェクト: ZakMed/asms-md
        public IEnumerable <CoefficientValue> CalculateCoefficientValues(IEnumerable <IndicatorValue> indicatorValues, IEnumerable <Dossier> dossiers, IEnumerable <Coefficient> coefficients)
        {
            var coefficientValues = new List <CoefficientValue>();

            if (dossiers.Select(o => o.FieldsetId).Distinct().Count() > 1)
            {
                throw new AsmsEx("in luna respectiva sunt dosare inregistrate la diferite seturi de campuri");
            }

            if (dossiers.Select(o => o.MeasuresetId).Distinct().Count() > 1)
            {
                throw new AsmsEx("in luna respectiva sunt dosare inregistrate la diferite seturi de masuri");
            }

            //calculate each coefficient for all dossiers
            foreach (var coefficient in coefficients)
            {
                EvalSums(coefficient, indicatorValues);

                var calc = new CalcContext <decimal>();
                foreach (var dossier in dossiers)
                {
                    calc.Constants.Clear();
                    foreach (var indicatorValue in indicatorValues.Where(o => o.DossierId == dossier.Id))
                    {
                        calc.Constants.Add("i" + indicatorValue.IndicatorId, indicatorValue.Value);
                    }

                    coefficientValues.Add(new CoefficientValue
                    {
                        CoefficientId = coefficient.Id,
                        DossierId     = dossier.Id,
                        Value         = Zero(() => calc.Evaluate(coefficient.Formula)),
                    });
                }
            }

            return(coefficientValues);
        }
コード例 #8
0
ファイル: EcoCalc.cs プロジェクト: ZakMed/asms-md
        public IEnumerable <IndicatorValue> CalculateIndicatorValues(Dossier dossier, IEnumerable <FieldValue> fieldValues, IEnumerable <Indicator> indicators)
        {
            var calc = new CalcContext <decimal>();

            foreach (var o in fieldValues)
            {
                calc.Constants.Add("c" + o.FieldId, o.Value);
            }

            var indicatorValues = indicators
                                  .Select(
                indicator =>
                new IndicatorValue
            {
                DossierId   = dossier.Id,
                IndicatorId = indicator.Id,
                Value       = Zero(() =>
                                   calc.Evaluate(
                                       indicators.Where(i => i.Id == indicator.Id).Single().Formula))
            }).ToList();

            return(indicatorValues);
        }
コード例 #9
0
ファイル: EcoCalc.cs プロジェクト: rodrigogalhardo/asms-md
        public IEnumerable<IndicatorValue> CalculateIndicatorValues(Dossier dossier, IEnumerable<FieldValue> fieldValues, IEnumerable<Indicator> indicators)
        {
            var calc = new CalcContext<decimal>();

            foreach (var o in fieldValues)
            {
                calc.Constants.Add("c" + o.FieldId, o.Value);
            }

            var indicatorValues = indicators
                .Select(
                indicator =>
                        new IndicatorValue
                            {
                                DossierId = dossier.Id,
                                IndicatorId = indicator.Id,
                                Value = Zero(() =>
                                    calc.Evaluate(
                                    indicators.Where(i => i.Id == indicator.Id).Single().Formula))
                            }).ToList();

            return indicatorValues;
        }