Ejemplo n.º 1
0
        public CalculationResult Calculate(string expression, params Parameter[] parameters)
        {
            var dict = new Dictionary <string, double>();

            foreach (var parameter in parameters)
            {
                dict[parameter.Name] = parameter.Value;
            }

            var exp   = _calculator.ParseExpression(expression, dict);
            var value = exp.Compile().Invoke();

            return(new CalculationResult(value, ExtractCalledFunctions(exp).ToArray()));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Calculates the value for a given calculation expression based on the supplied datafields (if datafields are part of the calculation expression)
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="precision"></param>
        /// <param name="datafields"></param>
        /// <returns></returns>
        public double Calculate(string expression, int precision, List <DataFieldResult> datafields)
        {
            var filledExpression = expression;

            if (string.IsNullOrEmpty(filledExpression) == false)
            {
                foreach (var item in datafields)
                {
                    if (item.FieldType == DataFieldType.Currency)
                    {
                        filledExpression = filledExpression.Replace("[" + item.Name + "]", MakeCurrencyTextParseableAsFloat(item.Value));
                    }
                    else
                    {
                        filledExpression = filledExpression.Replace("[" + item.Name + "]", item.Value.Replace(',', '.').Replace(" ", string.Empty).Replace("'", string.Empty));
                    }
                }

                var calculator     = new XtensibleCalculator();
                var linqExpression = calculator.ParseExpression(filledExpression);
                var linqFunction   = linqExpression.Compile();

                var retVal = linqFunction();
                if (precision >= 0)
                {
                    retVal = Math.Round(retVal, precision);
                }

                return(retVal);
            }
            else
            {
                return(0);
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            var calc = new XtensibleCalculator()
                       .RegisterFunction("Multiply", (a, b, c) => a * b * c)
                       .RegisterFunction("MultiplySquare", "Multiply(a, b, c) * Multiply(a, b, c)", "a", "b", "c");

            var func    = calc.ParseExpression("Multiply(x, y, PI)", x => 2, y => 2 + 3).Compile();
            var product = calc.ParseExpression("MultiplySquare(a, b, c)",
                                               new Dictionary <string, double> {
                { "a", 1 }, { "b", 2 }, { "c", 3 }
            }).Compile();

            Console.WriteLine($"Multiply: {func()}");
            Console.WriteLine($"Product: {product()}");
            Console.ReadKey();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var calc = new XtensibleCalculator()
                       .RegisterFunction("Multiply", (a, b, c) => a * b * c);

            var func = calc.ParseExpression("Multiply(x, y, PI)", x => 2, y => 2 + 3).Compile();

            Console.WriteLine("Result: {0}", func());
            Console.ReadKey();
        }
Ejemplo n.º 5
0
        public string EvalStringWithVars(string input, Dictionary <string, double> vars)
        {
            // HACK
            var newVars = new Dictionary <string, double>();

            foreach (var item in vars)
            {
                newVars[item.Key.Replace("_", "SUPER")] = item.Value;
            }

            var newInput = input.Replace("_", "SUPER").Replace("'", "");

            var normalInput = NormalizeCommas(newInput);

            var expr = calc.ParseExpression(normalInput, newVars);
            var func = expr.Compile();

            return(func().ToString(CultureInfo.InvariantCulture).Replace("NaN", "error"));
        }