Example #1
0
        ///////////////////////////////////////////

        public static void Test()
        {
            ExpressionCalculator calculator = new ExpressionCalculator();

            calculator.RegisterBasicMathFunctions();

            Dictionary <string, double> parameters = new Dictionary <string, double>();

            parameters.Add("x", 10);
            parameters.Add("Y", -3);

            Dictionary <string, double> tests = new Dictionary <string, double>();

            tests.Add("5 + 3 * 2", 11);
            tests.Add("5 * 3 * 2", 30);
            tests.Add("5 / 3 * 2", 3.333333333333333);
            tests.Add("(5 + 3) * 2", 16);
            tests.Add("- (5 +3)", -8);
            tests.Add("(5 + 3) * -2", -16);
            tests.Add("-3 + -4*(-4/2) + -3 + +3", 5);
            tests.Add("(5 + 3) - (5 - 3)", 6);
            tests.Add("x * (5 + y)", 20);
            tests.Add("Sin(1)", 0.8414709848078965066525023216303);
            tests.Add("2 + Sin(Cos(0))", 2.8414709848078965066525023216303);
            tests.Add("3 + Cos(5 + x) * 2", 1.4806241742823574523037071927334);
            tests.Add("Pow(2, 3)", 8);
            tests.Add("Max( 3 + x, 4 + x)", 14);
            tests.Add("4 + Pow(  4,5) * 4", 4100);
            tests.Add("Pow( x + 10,2)", 400);
            tests.Add("4 + Pow(  x + 10,2) * 4", 1604);
            tests.Add("4 + Pow(  (x + 10) * 2 - Pow(2,2), 2) * 4", 5188);
            tests.Add("4 + Pow(  (x + 10) * 2 + Pow(Pow(2 *2, 2),2), Cos(0) + 1) * 4", 350468);

            string s = "";

            foreach (KeyValuePair <string, double> test in tests)
            {
                string expresssion = test.Key;
                double trueResult  = test.Value;

                double returnValue;
                string error;
                bool   success = calculator.Calculate(expresssion, parameters, out returnValue, out error);

                string resultStr;
                if (success)
                {
                    if (Math.Abs(returnValue - trueResult) > .000001)
                    {
                        resultStr = "INVALID RESULT";
                    }
                    else
                    {
                        resultStr = returnValue.ToString();
                    }
                }
                else
                {
                    resultStr = "ERROR: " + error;
                }
                s += string.Format("\"{0}\" = {1}\n", expresssion, resultStr);
            }

            Log.Fatal("ExpressionCalculator: Test: \n" + s);
        }
Example #2
0
            //

            internal PreparedExpression(ExpressionCalculator calculator, object postfix)              // List<Symbol> postfix )
            {
                this.calculator = calculator;
                this.postfix    = (List <Symbol>)postfix;
            }