|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Compute expressionI need to compute a expression that user set as string in my project. It will
look like: string s = "2*(a+5)/6"; and I need to compute a value of expression in string s. Is there some class in framework that solves this problem? Is a way how solve it to use dynamic method? Thanks Ivana Nope. No simple way to do this. In fact, most programmers with a degree
have had to implement this kind of program as an excercise in both parsing and state machines. Its so common, I think if you do some research on Google you'll find tons of discussion about this kind of parsing of simple mathematical functions. Show quote "Ivana" wrote: > I need to compute a expression that user set as string in my project. It will > look like: > > string s = "2*(a+5)/6"; > and I need to compute a value of expression in string s. > > Is there some class in framework that solves this problem? Is a way how > solve it to use dynamic method? > > Thanks > Ivana There's an Eval function in JScript that will evaluate text math formulas. I
don't think it does variables; but there's an article about how to invoke it from C# here: http://www.odetocode.com/Code/80.aspx -- Show quoteBrowse http://connect.microsoft.com/VisualStudio/feedback/ and vote. http://www.peterRitchie.com/blog/ Microsoft MVP, Visual Developer - Visual C# "Ivana" wrote: > I need to compute a expression that user set as string in my project. It will > look like: > > string s = "2*(a+5)/6"; > and I need to compute a value of expression in string s. > > Is there some class in framework that solves this problem? Is a way how > solve it to use dynamic method? > > Thanks > Ivana If you will be doing a lot of this and need state vars set and read by user,
you might concider the Powershell api and setup a workspace contained by your code (i.e. form). Psh can do the math as well as any other script logic you may need. -- Show quoteWilliam Stacey [C# MVP] "Ivana" <Iv***@discussions.microsoft.com> wrote in message news:05E34681-156E-41DA-B811-5159174C12E0@microsoft.com... |I need to compute a expression that user set as string in my project. It will | look like: | | string s = "2*(a+5)/6"; | and I need to compute a value of expression in string s. | | Is there some class in framework that solves this problem? Is a way how | solve it to use dynamic method? | | Thanks | Ivana You could use System.CodeDom to build the code dynamically, compile it and
execute it. Or you could use CSharpCodeProvider or VBCodeProvider to compile and execute dynamically created code. Not very efficient but it would work. Show quote "Ivana" <Iv***@discussions.microsoft.com> wrote in message news:05E34681-156E-41DA-B811-5159174C12E0@microsoft.com... >I need to compute a expression that user set as string in my project. It >will > look like: > > string s = "2*(a+5)/6"; > and I need to compute a value of expression in string s. > > Is there some class in framework that solves this problem? Is a way how > solve it to use dynamic method? > > Thanks > Ivana Here's an example:
using System; using System.Collections.Generic; using System.Text; using Microsoft.CSharp; using System.CodeDom; using System.CodeDom.Compiler; namespace ComputeExpression { public interface IDynamicExpression { int Compute(); } class Program { static void Main(string[] args) { CSharpCodeProvider provider = new CSharpCodeProvider(); CompilerParameters options = new CompilerParameters(); options.GenerateInMemory = true; options.ReferencedAssemblies.Add("ComputeExpression.exe"); string source = @"namespace ComputeExpression { class Exp : IDynamicExpression { public int Compute() { return " + args[0] + @"; } } }"; CompilerResults result = provider.CompileAssemblyFromSource(options, source); if(result.Errors.Count == 0) { IDynamicExpression de = (IDynamicExpression)result.CompiledAssembly.CreateInstance("ComputeExpression.Exp"); Console.WriteLine(string.Format("{0} = {1}", args[0], de.Compute())); } else { foreach(CompilerError err in result.Errors) { Console.WriteLine("#{0} Line {1}: {2}", err.ErrorNumber, err.Line, err.ErrorText); } } } } } Show quote "Richard Taylor" <rich***@msdnnews.com> wrote in message news:OUBMXrvGHHA.1816@TK2MSFTNGP06.phx.gbl... > You could use System.CodeDom to build the code dynamically, compile it and > execute it. Or you could use CSharpCodeProvider or VBCodeProvider to > compile and execute dynamically created code. Not very efficient but it > would work. > > "Ivana" <Iv***@discussions.microsoft.com> wrote in message > news:05E34681-156E-41DA-B811-5159174C12E0@microsoft.com... >>I need to compute a expression that user set as string in my project. It >>will >> look like: >> >> string s = "2*(a+5)/6"; >> and I need to compute a value of expression in string s. >> >> Is there some class in framework that solves this problem? Is a way how >> solve it to use dynamic method? >> >> Thanks >> Ivana > > |
|||||||||||||||||||||||