2

I'm making a calculator in c# (winforms) and I was wondering if there is a simple way to calculate a string with multiple calculations in it so for example: if you have the string "124+241/2*5" let the program calculate it and then get an int output.

Thanks in advance.

2
  • stackoverflow.com/questions/355062/… Commented Mar 9, 2016 at 19:07
  • Two things to think about: a) is 124+241/2*5 = (124+241)/2*5 or 124+(241/2*5)? b) either way, the result is not a whole number, what do you want the int to be? Round up? Round down? Error? Commented Mar 9, 2016 at 20:01

5 Answers 5

4

Well, you're basically looking for the C# equivalent of Javascript's eval() function.

I recommend the library NCalc.

var result = new Expression("124+241/2*5").Evaluate()

Another calculating engine would be Jace.NET.

Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2.5);
variables.Add("var2", 3.4);

CalculationEngine engine = new CalculationEngine();
double result = engine.Calculate("var1*var2", variables);

As of 2024, you may also look at Adletec.Sonic (sonic) which started as a fork of Jace.NET, which is no longer maintained. It can compile expressions dynamically for native execution for high performance.

var expression = "var1*var2";

var variables = new Dictionary<string, double>();
variables.Add("var1", 2.5);
variables.Add("var2", 3.4);

var engine = Evaluator.CreateWithDefaults();
double result = engine.Evaluate(expression, variables); // 8.5

There's also Dynamic Expresso.

var result = new Interpreter().Eval("124+241/2*5");

And you can use Roslyn's ScriptingEngine to evaluate:

Roslyn.Scripting.Session session = roslynEngine.CreateSession();
session.Execute("124+241/2*5");
Sign up to request clarification or add additional context in comments.

1 Comment

As of the time of writing, Jace.NET is unmaintained or at least dormant. You can use sonic (github.com/adletec/sonic), which is basically a maintained fork with performance improvements and bug fixes. Disclosure: I'm affiliated with the project.
1

Well, "simple" is actually a pretty broad term... relative, I should say. Anyways, there are a few libraries that have proved themselves to be very robust and are very popular for things such as this.

The first one is NCALC (http://ncalc.codeplex.com/):

// One of the examples.
Expression e = new Expression("2 + 3 * 5");

The second one is Jace.NET (https://github.com/pieterderycke/Jace):

// One of the examples.
CalculationEngine engine = new CalculationEngine();
Func<Dictionary<string, double>, double> formula = engine.Build("var1+2/(3*otherVariable)");

Dictionary<string, double> variables = new Dictionary<string, double>();
variables.Add("var1", 2);
variables.Add("otherVariable", 4.2);

double result = formula(variables);

Both are fantastic, but from previous experience, I found that Jace.NET performs a bit better on more complex expressions (shouldn't be an issue for you here).

Comments

0

There is not automatic way to do it, you'll either have to parse it manually or use some kind of functionality equivalent to JavaScript's eval().

A nice alternative which doesn't require to use external libraries is System.Data.DataTable.Compute:

public double Evaluate(string expr)
{
    System.Data.DataTable table = new System.Data.DataTable();
    return Convert.ToDouble(table.Compute(expr, String.Empty));
}

Comments

0

CodeDom and Reflection give you the ability to dynamically build C# Code into a string, compile it, and run it all inside your program. This very powerful feature in .NET allows us to create the CodeDom calculator, a calculator that evaluates expressions (and even lines of C# code) inside a Windows Form.

http://www.c-sharpcorner.com/UploadFile/mgold/CodeDomCalculator08082005003253AM/CodeDomCalculator.aspx

Comments

0

If I understand you right you need something like that: Dynamic Expresso You can add it to your program with NuGet

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.