Make an abstract class Using the methods you want to use to evaluate expressions. At run time, create a new class that inherits this one. I will send the code soon.
as promised, this is the code for this:
public class BaseClass { public BaseClass(){} public virtual double Eval(double x,double y){return 0;} } public class MathExpressionParser { private BaseClass Evalulator=null; public MathExpressionParser(){} public bool Intialize(string equation) { Microsoft.CSharp.CSharpCodeProvider cp=new Microsoft.CSharp.CSharpCodeProvider(); System.CodeDom.Compiler.ICodeCompiler comp=cp.CreateCompiler(); System.CodeDom.Compiler.CompilerParameters cpar=new CompilerParameters(); cpar.GenerateInMemory=true; cpar.GenerateExecutable=false; cpar.ReferencedAssemblies.Add("system.dll"); cpar.ReferencedAssemblies.Add("EquationsParser.exe");
and you will need to run the following simple test to make sure it works:
private void button1_Click(object sender, System.EventArgs e) { if(parser.Intialize(textBox1.Text)==false) { MessageBox.Show("Check equation"); return; } textBox4.Text=(parser.evaluate(double.Parse(textBox2.Text),double.Parse(textBox3.Text))).ToString(); }
source share