0

I am making a small game in ASPX with my friends. We are working on a method that will break a string into commands and variables, and then do the math, but the code is slightly difficult and we would like to see if there is a short cut.

If I have a Button and a Textbox, is there a built in method which I can use to do math with that text box in order to find the value (e.g. the computer turns ((5-4)*(2+1) +(3/2))*2 into 9.

Darokrithia
  • 118
  • 1
  • 14
  • No, you will have to make one yourself or find a library that does it for you. – Sami Kuhmonen Feb 08 '17 at 19:08
  • See the answers to this question http://stackoverflow.com/questions/234217/is-it-possible-to-compile-and-execute-new-code-at-runtime-in-net – hatchet - done with SOverflow Feb 08 '17 at 19:12
  • 1
    I would do the evaluation on the client side using javascript. There's no point doing it on the server unless you have a requirement to do so. Check out the [math.js](http://mathjs.org/docs/expressions/parsing.html) library that does exactly that. – Timo Salomäki Feb 08 '17 at 19:18
  • I need to as the math will be part of a multiplayer version of the card game krypto, and will function like Kahoot. If you think I should still use JavaScript or some other client side language, how would I get it to interact with the ASPX and the client data? – Darokrithia Feb 09 '17 at 00:00

1 Answers1

2

You can use a library called NCalc to do it for you.

Expression e = new Expression("2 + 3 * 5");
Debug.Assert(17 == e.Evaluate());

A similar question was asked Evaluating string "3*(4+2)" yield int 18

Community
  • 1
  • 1
jplindgren
  • 171
  • 2
  • 9