2

I'm currently making a calculator. I know how to write the logic but I'm curious if it can be done the way I'm going to explain.

String str = "12 + 43 * (12 / 2)";
int answer;

answer = str.magic(); 

//str.magic is the same as 
answer =   12 + 43 * (12 / 2);

now what I want is how I can turn str into executible code.

4
  • Yes it can be done. Google expression parsers Commented Jan 13, 2016 at 5:12
  • have a look stackoverflow.com/questions/2853907/… Commented Jan 13, 2016 at 5:14
  • Check this stackoverflow.com/questions/5838918/… Commented Jan 13, 2016 at 5:15
  • I'm a bit confused - what are you trying to ask? You say you "know how to write the logic" but then ask how you can actually perform the task? Commented Jan 13, 2016 at 5:17

2 Answers 2

5

You can use CodeDom to get a 'native' C# parser. (You should improve the error handling.)

using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;

...

static void Main()
{
    double? result = Calculate("12 + 43 * (12 / 2)");
}

static double? Calculate(string formula)
{
    double result;
    try
    {
        CompilerParameters compilerParameters = new CompilerParameters
        {
            GenerateInMemory = true, 
            TreatWarningsAsErrors = false, 
            GenerateExecutable = false, 
        };

        string[] referencedAssemblies = { "System.dll" };
        compilerParameters.ReferencedAssemblies.AddRange(referencedAssemblies);

        const string codeTemplate = "using System;public class Dynamic {{static public double Calculate(){{return {0};}}}}";
        string code = string.Format(codeTemplate, formula);

        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerResults compilerResults = provider.CompileAssemblyFromSource(compilerParameters, new string[]{code});
        if (compilerResults.Errors.HasErrors)
            throw new Exception();

        Module module = compilerResults.CompiledAssembly.GetModules()[0];
        Type type = module.GetType("Dynamic");
        MethodInfo method = type.GetMethod("Calculate");

        result = (double)(method.Invoke(null, null));
    }
    catch (Exception)
    {
        return null;
    }

    return result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

As a note: This does not work in .net core or .net5+. For this I recommend this
1

You can simply use NCalc library Click here for more information. you can download that dll file from here

Example:

NCalc.Expression exp= new NCalc.Expression("(12*3)-29");
object obj = exp.Evaluate();
MessageBox.Show(obj.ToString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.