I am trying to build a calculator in C#. Now I would like to know whether it is possible to do a calculation, which is inside a textfield. For example a user enters into a textfield (2*3)+6
. Now how would I tell my script to calculate this and then output the result?
Asked
Active
Viewed 4,416 times
4

The Unfun Cat
- 29,987
- 31
- 114
- 156

Chris
- 6,093
- 11
- 42
- 55
-
1If you want to create your own solution from scratch, then look up "Reverse polish notation" – Chris Laplante Mar 20 '12 at 22:14
-
1Shunting yard algorithm seems to be pretty common aswell: http://en.wikipedia.org/wiki/Shunting-yard_algorithm – Mar 20 '12 at 22:15
-
There are about a dozen duplicate question on SO. [1](http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net) [2](http://stackoverflow.com/questions/7727933/is-there-any-other-tools-like-ncalc-available-for-net-development) [3](http://stackoverflow.com/questions/3972854/c-sharp-parse-math-expression) – CodesInChaos Mar 20 '12 at 22:32
2 Answers
13
You could use the Compute method:
using System;
using System.Data;
class Program
{
static void Main()
{
var result = new DataTable().Compute("(2*3)+6", null);
Console.WriteLine(result);
}
}
prints:
12
Of course don't expect to be able to calculate any complex functions with this method. You are limited to basic arithmetic.
And if you wanted to handle more complex expressions you could use CodeDOM.

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
3
-
Have no previous experience of this method, so I might be on thin ice here, but the documentation states `The expression parameter requires an aggregate function.` - So would this really work? – Christofer Eliasson Mar 20 '12 at 22:17
-
Hahaha cool! However if he is doing homework or trying to learn this trick will not do it. Nice to learn this though :) – Stilgar Mar 20 '12 at 22:18
-
-
@DarinDimitrov No haven't had the time yet, read it in documentation just now. Cool if it works though, which I suspect it does, based on the upvotes. – Christofer Eliasson Mar 20 '12 at 22:19
-
-
+1 I've often used `Compute` but not even i would have remembered to use it therefor. – Tim Schmelter Mar 20 '12 at 22:28
-
Yes, pretty limited as I stated in my answer. The problem is that the OP didn't describe his context and requirements in terms of expression complexity he wants to handle. It was just the first thing that came to my mind when I saw this (undefined) question. He talked about some text field, then he talked about some user, then he showed some arithmetic expression and that's where hos question ended. Poor question, I must admit. The problem when you don't specify the requirements of what you are trying to design is that you usually end up with something that ..., well, you get the point. – Darin Dimitrov Mar 20 '12 at 22:28
-
Didnt know you wanted to hear about my life! So the context would be: its a homework to create some sort of calculator, which a user (for example my teacher) should be able to use. – Chris Mar 20 '12 at 22:33
-
+1: yeah, it is a solution - just not one I'd expect to see. But it does answer his question. :) – IAbstract Mar 20 '12 at 22:34
-
@phpheini, no, I don't want to hear about your life. I don't care about your life. It's just that this is an extremely poorly defined homework assignment. Sorry to say it, but if that's the exact statement of the homework assignment you could ask your teacher to better define it. Who are the users of this system? Are they going to calculate the rate of the expansion of the universe with this calculator, or are they accountants that need to sum a few numbers to get the monthly balance correct? Do you get the point? Ask your teacher to define the word *calculator*. – Darin Dimitrov Mar 20 '12 at 22:35
-
Well my teacher didnt exactly define whether users should be able to calculate the rate of the expansion of the universe, but I guess addition, subtraction, multiplication and division should do it. No squareroots, fractions or so... – Chris Mar 20 '12 at 22:39
-
1@phpheini just implement the Shunting yard algorithm. This is what your teacher expects and this is how you will learn the most from the assignment. This answer is kind of a troll answer. Still it is remarkably cool, a sort of internal developer humor. – Stilgar Mar 20 '12 at 22:49
-
But Darin´s answer works fine, I will try to leave it this way and see what my teacher says :D – Chris Mar 20 '12 at 22:52
-
1Are you prepared to argue with your teacher over the definition of the question? I am sure Darin is but are you? :) Also he can easily expand the question by adding a single operator not supported by the compute method. This will weed out trick solutions like this but will be really easy to add to the real solutions. – Stilgar Mar 20 '12 at 23:00
1
You can use the System.Linq.Dynamic
library to do this:
`
static void Main(string[] args)
{
const string exp = "(A*B) + C";
var p0 = Expression.Parameter(typeof(int), "A");
var p1 = Expression.Parameter(typeof(int), "B");
var p2 = Expression.Parameter(typeof(int), "C");
var e = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { p0, p1, p2 }, typeof(int), exp);
var result = e.Compile().DynamicInvoke(2, 3, 6);
Console.WriteLine(result);
Console.ReadKey();
}
`
You can download a copy of it here.
N.B. the string could've just been "(2 * 3) + 6", but this method has the bonus that you can pass in values to the equation too.

markmuetz
- 9,334
- 2
- 32
- 33