How can I evaluate an equation within a String?

How can i evaluate string expression in if statement?

  • I want to evaluate string expression in if stmt. E.g. int i = 0,j = 2; char * str = "(i == 0) || (j == 2)"; if(str) /* This should execute code within if condition*/ { ... }

  • Answer:

    You will need to write a mini parse to recognize and evaluate the sequence of characters in the string as an expression belonging to some grammar. Perhaps use a parser library to do this; for example Spirit which is part of Boost. http://www.boost.org/doc/libs/1_37_0/libs/spirit/classic/index.html

Miningco.com Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.