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.

B.K.
  • 9,982
  • 10
  • 73
  • 105
  • http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net – TVOHM Mar 09 '16 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? – Wai Ha Lee Mar 09 '16 at 20:01

5 Answers5

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);

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");
Frozenthia
  • 759
  • 3
  • 9
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).

B.K.
  • 9,982
  • 10
  • 73
  • 105
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));
}
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
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

Pawan Pal
  • 1
  • 1
0

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

AleY
  • 3
  • 3