I want To do something like this: have a string "5+5" and when I convert it to integer, expected output want to be 10, but I get runtime error. Would appreciate your opinion.
Asked
Active
Viewed 78 times
-2
-
1You'll need to implement (or find implementation) of https://en.wikipedia.org/wiki/Shunting-yard_algorithm. – Lukasz Nowakowski Sep 29 '20 at 06:04
-
1_"...but I get runtime error"_ - what error? – Sep 29 '20 at 06:05
-
To "calculate the string", you can try [Reverse Polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation) via `stack`. – 大陸北方網友 Sep 29 '20 at 06:09
-
I will recommend reading https://medium.com/@toptensoftware/writing-a-simple-math-expression-engine-in-c-d414de18d4ce – xdtTransform Sep 29 '20 at 06:09
-
1Question as asked is duplicate of many "parse math expression in C#" questions. Based on comment you left on an answer what is asked here and what you want are somewhat different things. If you really want "sum of integers separated by +" - [edit] question accordingly, otherwise do nothing here and for future questions demonstrate the effort (ideally with [MCVE]) rather than just "get error". – Alexei Levenkov Sep 29 '20 at 06:14
-
You can use `new DataTable.Compute("5+5")` – Muaath Sep 29 '20 at 06:30
1 Answers
1
You can .Split()
string by +
, Convert it to integer and Calculate .Sum()
var result = "5+5".Split('+').Select(int.Parse).Sum();

Prasad Telkikar
- 15,207
- 5
- 21
- 44