C # resolve "(true and true) or (true or false)"

C #: I have a string variable that looks like this:

string a = "(true and true) or (true or false)"; 

It can be anything, it can become more complex, for example:

  string b = "((true and false) or (true or false) and not (true and false)) and false"; 

All I know is that it is right. It cannot be that this expression cannot be "evaluated".

Is there a way that I can somehow appreciate? I would like to know only the result (result) of this line. This means that instead of this line I need "true" or "false".

I think I can make a parsing method that does this by gradually trimming the string until we get the final value, but I was wondering if there is a better approach.

+4
source share
4 answers

Something like this maybe?

 string previous = string.Empty; while (b != previous) { previous = b; b = b.Replace("true and false", "false"); b = b.Replace("true and true", "true"); b = b.Replace("false and true", "false"); b = b.Replace("false and false", "false"); b = b.Replace("false or false", "false"); b = b.Replace("true or false", "true"); b = b.Replace("true or true", "true"); b = b.Replace("false or true", "true"); b = b.Replace("(false)", "false"); b = b.Replace("(true)", "true"); b = b.Replace("not false", "true"); b = b.Replace("not true", "false"); } 

Please note that the specification is ambiguous, such as:

 "false and false or true" "false and true or true" 

Both of these expressions are “true” if “evaluted” first and “false” if first evaluated or evaluated. Therefore, requiring brackets at each level would be better. Requiring an assessment from left to right is another option, but it makes the code more complex.

For those of you who can object to this style of solution for this style of problem, remember that some mathematicians believe that all this mathematics can be reduced to this kind of manipulation of symbols. said one of Russell and Whitehead’s main criticisms of the Mathematics Principle is that it embodies formulas with too much meaning.

+7
source

By expanding on Rob's comment, you can use runtime compilation along with C # 4.0 dynamic support and do something like this:

 var expression = "(true and false) or (true or false)"; var helper = "" + "using System; " + "public class Expression {{ public bool Eval() {{ return {0}; }} }}"; var replaced = expression.Replace("and", "&&").Replace("or", "||"); var references = new string[] { "System.dll" }; var parameters = new CompilerParameters(references, "Test.dll"); var compiler = new CSharpCodeProvider(); var results = compiler.CompileAssemblyFromSource( parameters, String.Format(helper, replaced)); dynamic exp = Activator.CreateInstance( results.CompiledAssembly.GetType("Expression")); Console.WriteLine(exp.Eval()); 
+10
source

Parsing is the best choice. If you need to check for typos, this will make it a little harder.

+2
source

C # Doesn’t have an Eval method or something like that, which allows you to simply run such an operator to get the final result. If I didn’t miss something, you will have to figure it out and reduce this path.

-1
source

All Articles