Answered by:
string formula to real formula

Question
-
Hi,
I'd like to convert a string to a variable formula in C#. I mean that I wanna write as the following.
string formula="a*b";
int a;
int b;
int ans= formula //here I want to convet formula to.........
Is there any way to solve my problem? Please help me.
REgard.
Tuesday, July 29, 2008 10:35 AM
Answers
-
Here is one way I wrote, using dynamic code generation and compilation. It's a bit of a sledgehammer to crack a nut though - but you might find it of interest. It's a console app:
using System; using System.Reflection; using System.CodeDom.Compiler; using Microsoft.CSharp; /// <summary>This simple program demonstrates how to dynamically compile code.</summary> class Program { static void Main() { TestExpression("2+1-(3*2)+8/2"); TestExpression("1*2*3*4*5*6"); TestExpression("Invalid expression"); } static void TestExpression(string expression) { try { int result = EvaluateExpression(expression); Console.WriteLine("'" + expression + "' = " + result); } catch (Exception) { Console.WriteLine("Expression is invalid: '" + expression + "'"); } } public static int EvaluateExpression(string expression) { string code = string.Format // Note: Use "{{" to denote a single "{" ( "public static class Func{{ public static int func(){{ return {0};}}}}", expression ); CompilerResults compilerResults = CompileScript(code); if (compilerResults.Errors.HasErrors) { throw new InvalidOperationException("Expression has a syntax error."); } Assembly assembly = compilerResults.CompiledAssembly; MethodInfo method = assembly.GetType("Func").GetMethod("func"); return (int)method.Invoke(null, null); } public static CompilerResults CompileScript(string source) { CompilerParameters parms = new CompilerParameters(); parms.GenerateExecutable = false; parms.GenerateInMemory = true; parms.IncludeDebugInformation = false; CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp"); return compiler.CompileAssemblyFromSource(parms, source); } }
- Proposed as answer by Matthew Watson Tuesday, July 29, 2008 2:44 PM
- Marked as answer by Naythu Wednesday, July 30, 2008 5:25 AM
Tuesday, July 29, 2008 2:43 PM -
I think you are trying to evaluate the expressions.
Check these link
http://blog.lozanotek.com/archive/2004/12/31/180.aspx
http://www.codeproject.com/KB/recipes/matheval.aspx
Pradeep Sethi- Marked as answer by Naythu Wednesday, July 30, 2008 5:25 AM
Tuesday, July 29, 2008 10:55 AM
All replies
-
You can convert integer which are represented as strings to int variables using the int.Parse() method. But, Converting string "a*b" to int doesn't make any sense.Tuesday, July 29, 2008 10:49 AM
-
I think you are trying to evaluate the expressions.
Check these link
http://blog.lozanotek.com/archive/2004/12/31/180.aspx
http://www.codeproject.com/KB/recipes/matheval.aspx
Pradeep Sethi- Marked as answer by Naythu Wednesday, July 30, 2008 5:25 AM
Tuesday, July 29, 2008 10:55 AM -
Here is one way I wrote, using dynamic code generation and compilation. It's a bit of a sledgehammer to crack a nut though - but you might find it of interest. It's a console app:
using System; using System.Reflection; using System.CodeDom.Compiler; using Microsoft.CSharp; /// <summary>This simple program demonstrates how to dynamically compile code.</summary> class Program { static void Main() { TestExpression("2+1-(3*2)+8/2"); TestExpression("1*2*3*4*5*6"); TestExpression("Invalid expression"); } static void TestExpression(string expression) { try { int result = EvaluateExpression(expression); Console.WriteLine("'" + expression + "' = " + result); } catch (Exception) { Console.WriteLine("Expression is invalid: '" + expression + "'"); } } public static int EvaluateExpression(string expression) { string code = string.Format // Note: Use "{{" to denote a single "{" ( "public static class Func{{ public static int func(){{ return {0};}}}}", expression ); CompilerResults compilerResults = CompileScript(code); if (compilerResults.Errors.HasErrors) { throw new InvalidOperationException("Expression has a syntax error."); } Assembly assembly = compilerResults.CompiledAssembly; MethodInfo method = assembly.GetType("Func").GetMethod("func"); return (int)method.Invoke(null, null); } public static CompilerResults CompileScript(string source) { CompilerParameters parms = new CompilerParameters(); parms.GenerateExecutable = false; parms.GenerateInMemory = true; parms.IncludeDebugInformation = false; CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp"); return compiler.CompileAssemblyFromSource(parms, source); } }
- Proposed as answer by Matthew Watson Tuesday, July 29, 2008 2:44 PM
- Marked as answer by Naythu Wednesday, July 30, 2008 5:25 AM
Tuesday, July 29, 2008 2:43 PM -
Is it possible to get some precision with this code? like 1+1/2 = 1.5 ?
Saturday, January 29, 2011 5:29 AM -
Yes, change the "int" to "double" in the code, and enter numbers that contain decimal points, e.g. 1.0+1.0/2.0
Saturday, January 29, 2011 3:31 PM