Ejemplo n.º 1
0
        public static decimal ExecuteFormula(MathParserParams mathParserParams)
        {
            decimal results = 0;

            // build list of all possible args and send it in. It will only use that the formula requires.
            List <Argument> arguments = new List <Argument>();

            Argument saVal = new Argument(mathParserParams.SAExpression, mathParserParams.SAValue);

            arguments.Add(saVal);

            Argument suVal = new Argument(mathParserParams.SUExpression, mathParserParams.SUValue);

            arguments.Add(suVal);

            Argument co = new Argument(mathParserParams.COExpression, mathParserParams.COValue);

            arguments.Add(co);

            Argument txVal = new Argument(mathParserParams.TXExpression, mathParserParams.TXValue);

            arguments.Add(txVal);

            Argument rcVal1 = new Argument(mathParserParams.RCVAL1Expression, mathParserParams.RCVAL1Value);

            arguments.Add(rcVal1);

            Argument acVal1 = new Argument(mathParserParams.ACVAL1Expression, mathParserParams.ACVAL1Value);

            arguments.Add(acVal1);

            Argument acVal2 = new Argument(mathParserParams.ACVAL2Expression, mathParserParams.ACVAL2Value);

            arguments.Add(acVal2);

            Argument tx1Rt = new Argument(mathParserParams.TAX1RTExpression, mathParserParams.TAX1RTValue);

            arguments.Add(tx1Rt);



            Expression exp = new Expression(mathParserParams.Formula);

            exp.addArguments(arguments.ToArray());

            // consider logging the formula...such as exp.getExpressionString();

            var expResults = exp.calculate();

            // consider logging the results ...such as expResults.ToString();

            if (expResults.ToString() == "NaN")
            {
                return(results);
            }

            results = (decimal)exp.calculate();
            results = decimal.Round(results, mathParserParams.DecimalsToRoundAt);
            return(results);
        }
Ejemplo n.º 2
0
        public static void ExecuteDemo()
        {
            decimal results = 0;

            foreach (string forumla in TestFormulas)
            {
                Console.WriteLine($"Calculating {forumla}");

                MathParserParams mathParserParams = new MathParserParams()
                {
                    DecimalsToRoundAt = 2,
                    Formula           = forumla,
                    SAValue           = GetRandomNumber(),
                    SUValue           = GetRandomNumber(),
                    COValue           = GetRandomNumber(),
                    TXValue           = GetRandomNumber(),
                    RCVAL1Value       = GetRandomNumber(),
                    ACVAL1Value       = GetRandomNumber(),
                    ACVAL2Value       = GetRandomNumber(),
                    TAX1RTValue       = GetRandomNumber()
                };

                results = MathParser.ExecuteFormula(mathParserParams);
                Console.WriteLine($"Results: {results}");

                Console.WriteLine("");
            }

            Console.WriteLine("....all done.....");
            Console.ReadLine();
        }