I am working on math engine in C# for my teacher, and I don't know how to parse something like this string needToBeCalculated = "1/2 + (2 + 3 /2 * (3+1))";
, To an int or long. I know thatlong Calculated = long.Parse(needToBeCalculated );
this doesn't work. But is there a build in method or some kind of a design pattern that i must use ?
Asked
Active
Viewed 55 times
0

denislav licov
- 9
- 2
-
2Simple: `double result = (double)new DataTable().Compute("1/2 + (2 + 3 /2 * (3+1))", null)` ;-) – Tim Schmelter Jul 01 '15 at 10:46
-
@TimSchmelter wow, I have never seen that before. TIL. – David Pilkington Jul 01 '15 at 10:48
-
and how do i use it if i have a math.pow ? – denislav licov Jul 01 '15 at 10:50
-
You can take a look at [this](http://stackoverflow.com/a/392355/1677264) and [this](http://stackoverflow.com/a/4620486/1677264). – Cheshire Cat Jul 01 '15 at 10:54
-
@denislavlicov: exponentiation is not supported by `Compute`, look at [the remarks section of `DataColumn.Expression`](https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.90).aspx) to see what is supported. – Tim Schmelter Jul 01 '15 at 10:58
-
Reminds me of [this](http://stackoverflow.com/q/20422853/1997232) question. – Sinatr Jul 01 '15 at 11:49