C#: I have a string variable that looks like this:
string a = "(true and true) or (true or false)";
This can be anything, it can get more complex, like:
string b = "((true and false) or (true or false) and not (true and false)) and false";
All i know is that it is correct. Cannot happen that this expression cannot be “evaluated”.
Is there a way that I can somehow evaluate this? I would only like to know the outcome (result) of that string. This means I need “true” or “false” instead of this string.
I think I can make a parse method that does this, reducing the string step by step, until we got the final value, but I was wondering if there is a better approach.
Something like this maybe?
Note that the specification allows ambigious formulations, such as these:
Both of these expressions are “true” if the and is evaluted first, and “false” if the or is evaluated first. Therefore, requireing parenthesis at every level would be better. Requiring left-to-right evaluation is another option, but that makes the code a bit more complex.
For those of you who may object to this style of solution for this style of problem, remember that some mathematicians believe that all of mathematics may be reduced to this sort of symbol manipulation. It is said that one of the main criticisms of Russell and Whitehead’s Principia Mathematica is that it embues the formulas with too much meaning.