It's easy to implement a "Calculator" to parse a string (e.g., 2 ^ 3 / 2
) and compute the result of operations. But, is there a library already capable of doing this?
Asked
Active
Viewed 2,283 times
3

Ryan Emerle
- 15,461
- 8
- 52
- 69

Luca
- 11,646
- 11
- 70
- 125
-
1Do you mean (2^3)/2 or 2^(3/2)? – Gumbo Feb 11 '10 at 18:03
-
it won't be that easy, as being a compiled language c# doesn't have an eval function (unlike perl for example) – Richard H Feb 11 '10 at 18:04
-
1Do you mean `2 to the power of 3 divided by 2` or `2 XOR 3 divided by 2` ? – Earlz Feb 11 '10 at 18:13
-
1Dupe: http://stackoverflow.com/questions/53844/how-can-i-evaluate-a-c-expression-dynamically – Ryan Emerle Feb 11 '10 at 18:14
-
I mean "2 to the power of 3 divided by 2", but "2 XOR 3 divided by 2" is nice also! Nevertheless it was only an instance example, just to make title looks good. :) – Luca Feb 11 '10 at 20:52
6 Answers
2
You are going to need some kind of math parser in order to do that. I've used C# Expression Parser using RPN by DeepEddie before, or you could make your own if the complexity of the expressions you use are of more limited scope.
Don't let it scare you, it is actually quite easy to make.

Dynami Le Savard
- 4,986
- 2
- 26
- 22
-
-
1
-
1i just mean that the apparent easiness of writing a calculator that parses an input string is deceptive. It's actually a non-trivial problem. If you don't believe me, try it. – Richard H Feb 11 '10 at 18:28
1
embed ironpython in your app, you can then ask it to evaluate arbitrarily complex strings
i think they even have a sample of the same thing

pm100
- 48,078
- 23
- 82
- 145
0
Check out Reverse Polish notation. It's widely used in modern calculators

dev-null-dweller
- 29,274
- 3
- 65
- 85
-
1I don't see how this is helpful. The OP is looking for an existing library and not looking to implement his own parser (and thus reinvent the wheel) – Ryan Emerle Feb 11 '10 at 18:20
-
But knowing the name of thing you are looking for can be helpful. – dev-null-dweller Feb 11 '10 at 18:42
0
You can also use the JScript library although its deprecated. Not saying you should, just that you could.

Chris Haas
- 53,986
- 12
- 141
- 274
0
I have used this :
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;
public class JScriptEvaluator
{
public int EvalToInteger(string statement)
{
string s = EvalToString(statement);
return int.Parse(s.ToString());
}
public double EvalToDouble(string statement)
{
string s = EvalToString(statement);
return double.Parse(s);
}
public string EvalToString(string statement)
{
object o = "-1";
try
{
o= EvalToObject(statement);
}
catch { o = "-1"; }
return o.ToString();
}
public object EvalToObject(string statement)
{
return _evaluatorType.InvokeMember(
"Eval",
BindingFlags.InvokeMethod,
null,
_evaluator,
new object[] { statement }
);
}
public JScriptEvaluator()
{
CodeDomProvider provider = new Microsoft.JScript.JScriptCodeProvider();
CompilerParameters parameters;
parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
CompilerResults results;
results = provider.CompileAssemblyFromSource(parameters, _jscriptSource);
Assembly assembly = results.CompiledAssembly;
_evaluatorType = assembly.GetType("Evaluator.Evaluator");
_evaluator = Activator.CreateInstance(_evaluatorType);
}
private object _evaluator = null;
private Type _evaluatorType = null;
private readonly string _jscriptSource =
@"package Evaluator
{
class Evaluator
{
public function Eval(expr : String) : String
{
return eval(expr);
}
}
}";
}

fishhead
- 5,829
- 7
- 32
- 43