public CustomParser() : base() { OperatorList.Add("!"); OperatorList.Add("_"); OperatorAction.Add("!", (x, y) => Factorial(x)); OperatorAction.Add("_", (x, y) => 10.130M); LocalFunctions["log"] = (input) => { if (input.Length == 1) { return((decimal)Math.Log((double)input[0])); } else if (input.Length == 2) { return((decimal)Math.Log((double)input[1], (double)input[0])); } else { return(0); // false } }; LocalFunctions.Add("ln", x => (decimal)Math.Log((double)x[0])); LocalFunctions.Add("logn", x => (decimal)Math.Log((double)x[0])); }
/// <summary> /// This constructor will add some basic operators, functions, and variables /// to the parser. Please note that you are able to change that using /// boolean flags /// </summary> /// <param name="loadPreDefinedFunctions">This will load "abs", "cos", "cosh", "arccos", "sin", "sinh", "arcsin", "tan", "tanh", "arctan", "sqrt", "rem", "round"</param> /// <param name="loadPreDefinedOperators">This will load "%", "*", ":", "/", "+", "-", ">", "<", "="</param> /// <param name="loadPreDefinedVariables">This will load "pi" and "e"</param> public MathParser(bool loadPreDefinedFunctions = true, bool loadPreDefinedOperators = true, bool loadPreDefinedVariables = true) { if (loadPreDefinedOperators) { // by default, we will load basic arithmetic operators. // please note, its possible to do it either inside the constructor, // or outside the class. the lowest value will be executed first! OperatorList.Add("%"); // modulo OperatorList.Add("^"); // to the power of OperatorList.Add(":"); // division 1 OperatorList.Add("/"); // division 2 OperatorList.Add("*"); // multiplication OperatorList.Add("-"); // subtraction OperatorList.Add("+"); // addition OperatorList.Add(">"); // greater than OperatorList.Add("<"); // less than OperatorList.Add("="); // are equal // when an operator is executed, the parser needs to know how. // this is how you can add your own operators. note, the order // in this list does not matter. _operatorAction.Add("%", (numberA, numberB) => numberA % numberB); _operatorAction.Add("^", (numberA, numberB) => (decimal)Math.Pow((double)numberA, (double)numberB)); _operatorAction.Add(":", (numberA, numberB) => numberA / numberB); _operatorAction.Add("/", (numberA, numberB) => numberA / numberB); _operatorAction.Add("*", (numberA, numberB) => numberA * numberB); _operatorAction.Add("+", (numberA, numberB) => numberA + numberB); _operatorAction.Add("-", (numberA, numberB) => numberA - numberB); _operatorAction.Add(">", (numberA, numberB) => numberA > numberB ? 1 : 0); _operatorAction.Add("<", (numberA, numberB) => numberA < numberB ? 1 : 0); _operatorAction.Add("=", (numberA, numberB) => numberA == numberB ? 1 : 0); } if (loadPreDefinedFunctions) { // these are the basic functions you might be able to use. // as with operators, localFunctions might be adjusted, i.e. // you can add or remove a function. // please open the "MathosTest" project, and find MathParser.cs // in "CustomFunction" you will see three ways of adding // a new function to this variable! // EACH FUNCTION MAY ONLY TAKE ONE PARAMETER, AND RETURN ONE // VALUE. THESE VALUES SHOULD BE IN "DECIMAL FORMAT"! LocalFunctions.Add("abs", x => (decimal)Math.Abs((double)x[0])); LocalFunctions.Add("cos", x => (decimal)Math.Cos((double)x[0])); LocalFunctions.Add("cosh", x => (decimal)Math.Cosh((double)x[0])); LocalFunctions.Add("arccos", x => (decimal)Math.Acos((double)x[0])); LocalFunctions.Add("sin", x => (decimal)Math.Sin((double)x[0])); LocalFunctions.Add("sinh", x => (decimal)Math.Sinh((double)x[0])); LocalFunctions.Add("arcsin", x => (decimal)Math.Asin((double)x[0])); LocalFunctions.Add("tan", x => (decimal)Math.Tan((double)x[0])); LocalFunctions.Add("tanh", x => (decimal)Math.Tanh((double)x[0])); LocalFunctions.Add("arctan", x => (decimal)Math.Atan((double)x[0])); //LocalFunctions.Add("arctan2", x => (decimal)Math.Atan2((double)x[0], (double)x[1])); LocalFunctions.Add("sqrt", x => (decimal)Math.Sqrt((double)x[0])); LocalFunctions.Add("rem", x => (decimal)Math.IEEERemainder((double)x[0], (double)x[1])); LocalFunctions.Add("root", x => (decimal)Math.Pow((double)x[0], 1.0 / (double)x[1])); LocalFunctions.Add("ln", x => (decimal)Math.Log((double)x[0], Math.E)); LocalFunctions.Add("pow", x => (decimal)Math.Pow((double)x[0], (double)x[1])); LocalFunctions.Add("e", x => (decimal)Math.Pow(Math.E, (double)x[0])); LocalFunctions.Add("exp", x => (decimal)Math.Exp((double)x[0])); //LocalFunctions.Add("log", x => (decimal)Math.Log((double)x[0])); //LocalFunctions.Add("log10", x => (decimal)Math.Log10((double)x[0])); LocalFunctions.Add("log", delegate(decimal[] input) { // input[0] is the number // input[1] is the base switch (input.Length) { case 1: return((decimal)Math.Log((double)input[0])); case 2: return((decimal)Math.Log((double)input[0], (double)input[1])); default: return(0); // false } }); LocalFunctions.Add("round", x => (decimal)Math.Round((double)x[0])); LocalFunctions.Add("truncate", x => (decimal)(x[0] < 0.0m ? -Math.Floor(-(double)x[0]) : Math.Floor((double)x[0]))); LocalFunctions.Add("floor", x => (decimal)Math.Floor((double)x[0])); LocalFunctions.Add("ceiling", x => (decimal)Math.Ceiling((double)x[0])); LocalFunctions.Add("sign", x => (decimal)Math.Sign((double)x[0])); } if (loadPreDefinedVariables) { // local variables such as pi can also be added into the parser. LocalVariables.Add("pi", (decimal)3.14159265358979323846264338327950288); // the simplest variable! LocalVariables.Add("pi2", (decimal)6.28318530717958647692528676655900576); LocalVariables.Add("pi05", (decimal)1.57079632679489661923132169163975144); LocalVariables.Add("pi025", (decimal)0.78539816339744830961566084581987572); LocalVariables.Add("pi0125", (decimal)0.39269908169872415480783042290993786); LocalVariables.Add("pitograd", (decimal)57.2957795130823208767981548141051704); LocalVariables.Add("piofgrad", (decimal)0.01745329251994329576923690768488612); LocalVariables.Add("e", (decimal)2.71828182845904523536028747135266249); LocalVariables.Add("phi", (decimal)1.61803398874989484820458683436563811); LocalVariables.Add("major", (decimal)0.61803398874989484820458683436563811); LocalVariables.Add("minor", (decimal)0.38196601125010515179541316563436189); } }
/// <summary> /// This constructor will add some basic operators, functions, and variables /// to the parser. Please note that you are able to change that using /// boolean flags /// </summary> /// <param name="loadPreDefinedFunctions">This will load "abs", "cos", "cosh", "arccos", "sin", "sinh", "arcsin", "tan", "tanh", "arctan", "sqrt", "rem", "round"</param> /// <param name="loadPreDefinedOperators">This will load "%", "*", ":", "/", "+", "-", ">", "<", "="</param> /// <param name="loadPreDefinedVariables">This will load "pi"</param> public MathParser(bool loadPreDefinedFunctions = true, bool loadPreDefinedOperators = true, bool loadPreDefinedVariables = true) { if (loadPreDefinedOperators) { // by default, we will load basic arithmetic operators. // please note, its possible to do it either inside the constructor, // or outside the class. the lowest value will be executed first! OperatorList.Add("%"); // modulo OperatorList.Add("^"); // to the power of OperatorList.Add(":"); // division 1 OperatorList.Add("/"); // division 2 OperatorList.Add("*"); // multiplication OperatorList.Add("-"); // subtraction OperatorList.Add("+"); // addition OperatorList.Add(">"); // greater than OperatorList.Add("<"); // less than OperatorList.Add("="); // are equal // when an operator is executed, the parser needs to know how. // this is how you can add your own operators. note, the order // in this list does not matter. _operatorAction.Add("%", (numberA, numberB) => numberA % numberB); _operatorAction.Add("^", (numberA, numberB) => (decimal)Math.Pow((double)numberA, (double)numberB)); _operatorAction.Add(":", (numberA, numberB) => numberA / numberB); _operatorAction.Add("/", (numberA, numberB) => numberA / numberB); _operatorAction.Add("*", (numberA, numberB) => numberA * numberB); _operatorAction.Add("+", (numberA, numberB) => numberA + numberB); _operatorAction.Add("-", (numberA, numberB) => numberA - numberB); _operatorAction.Add(">", (numberA, numberB) => numberA > numberB ? 1 : 0); _operatorAction.Add("<", (numberA, numberB) => numberA < numberB ? 1 : 0); _operatorAction.Add("=", (numberA, numberB) => numberA == numberB ? 1 : 0); } if (loadPreDefinedFunctions) { // these are the basic functions you might be able to use. // as with operators, localFunctions might be adjusted, i.e. // you can add or remove a function. // please open the "MathosTest" project, and find MathParser.cs // in "CustomFunction" you will see three ways of adding // a new function to this variable! // EACH FUNCTION MAY ONLY TAKE ONE PARAMETER, AND RETURN ONE // VALUE. THESE VALUES SHOULD BE IN "DECIMAL FORMAT"! LocalFunctions.Add("abs", x => (decimal)Math.Abs((double)x[0])); LocalFunctions.Add("cos", x => (decimal)Math.Cos((double)x[0])); LocalFunctions.Add("cosh", x => (decimal)Math.Cosh((double)x[0])); LocalFunctions.Add("arccos", x => (decimal)Math.Acos((double)x[0])); LocalFunctions.Add("sin", x => (decimal)Math.Sin((double)x[0])); LocalFunctions.Add("sinh", x => (decimal)Math.Sinh((double)x[0])); LocalFunctions.Add("arcsin", x => (decimal)Math.Asin((double)x[0])); LocalFunctions.Add("tan", x => (decimal)Math.Tan((double)x[0])); LocalFunctions.Add("tanh", x => (decimal)Math.Tanh((double)x[0])); LocalFunctions.Add("arctan", x => (decimal)Math.Atan((double)x[0])); LocalFunctions.Add("sqrt", x => (decimal)Math.Sqrt((double)x[0])); LocalFunctions.Add("rem", x => (decimal)Math.IEEERemainder((double)x[0], (double)x[1])); LocalFunctions.Add("round", x => (decimal)Math.Round((double)x[0])); LocalFunctions.Add("pow", x => (decimal)Math.Pow((double)x[0], (double)x[1])); } if (loadPreDefinedVariables) { // local variables such as pi can also be added into the parser. LocalVariables.Add("pi", (decimal)Math.PI); // the simplest variable! } }