private void button1_Click(object sender, EventArgs e) { var parsedFunc = new ParsedFunction(); parsedFunc.StringToParse = " "; parsedFunc.VariableNames = new List <string> { "x", "y", "z" }; parsedFunc.ParameterNames = new List <string> { "p1", "p2" }; parsedFunc.ParameterValues = new[] { 0.0, 0.0 }; var errorData = new FuncParserErrorData(); parsedFunc.Parse(errorData); if (errorData.ErrorNumber == errNumber.err_OK) { MessageBox.Show(this, "That wasn't supposed to happen"); } else { MessageBox.Show(this, $"Error is on purpose. The error is {errorData.ErrorNumber}"); } }
static void Test3() { var pf = new ParsedFunction(); var stringsToParse = new[] { "1e-3", "1.25e-4", "-2.2e-2", "1e+3", "1.25e+2", "-2.2e+2" }; foreach (var stringToParse in stringsToParse) { pf.StringToParse = stringToParse; pf.Parse(); var d = pf.CalcExpression(null); Console.WriteLine($"f({stringToParse}) = {d}"); } }
static void Test2() { double p1 = 1, p2 = 2, p3 = 3, x = 3, y = 4; var pf = new ParsedFunction(); var caseSensitive = pf.CaseSensitive; pf.CaseSensitive = !caseSensitive; caseSensitive = pf.CaseSensitive; Console.WriteLine($"CaseSensitive={caseSensitive}"); pf.SetVariableNames(new[] { "x", "y" }); pf.SetParameterNames(new[] { "p1", "p2", "p3" }); pf.SetParameterValues(new[] { p1, p2, p3 }); pf.SetParametersNotToSimplify(new[] { "p3" }); pf.SimplifyParametersAllowed = true; pf.LogicOperatorsAllowed = false; pf.LogicalNumericMixAllowed = false; pf.ComparisonTolerance = 0.1; pf.StringToParse = "2*p1+3*p2+p3+x*y"; var stringToParse = pf.StringToParse; pf.Parse(); Console.WriteLine("Parse: OK"); var value = pf.CalcExpression(new[] { x, y }); Console.WriteLine($"p1={p1} p2={p2} p3={p3} x={x} y={y}\n"); Console.WriteLine($"{stringToParse} = {value}\n"); var xmlString = pf.GetXMLString(); Console.WriteLine($"{xmlString}\n"); pf = null; pf = new ParsedFunction(); pf.SetVariableNames(new[] { "x", "y" }); pf.StringToParse = "x+t"; pf.Parse(); }