简体   繁体   English

C# 解析“(真和真)或(真或假)”

[英]C# resolve “(true and true) or (true or false)”

C#: I have a string variable that looks like this: C#:我有一个字符串变量,如下所示:

 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.这意味着我需要“true”或“false”而不是这个字符串。

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.我想我可以制作一个解析方法来执行此操作,逐步减少字符串,直到我们得到最终值,但我想知道是否有更好的方法。

Expanding on Rob's comment, you can use runtime compilation in conjunction with C# 4.0 dynamic support and do something like this:扩展 Rob 的评论,您可以将运行时编译与 C# 4.0 dynamic支持结合使用,并执行以下操作:

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());

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");
 }

Note that the specification allows ambigious formulations, such as these:请注意,规范允许模棱两可的表述,例如:

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

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. 据说,对罗素和怀特黑德的《数学原理》的主要批评之一是它包含了太多意义的公式。

Parsing is your best bet.解析是你最好的选择。 If you have to check for typos, that would make it a bit harder.如果您必须检查拼写错误,那将变得更加困难。

C# Doesn't have an Eval method or something similar that would allow you to just run a statement like this to gain the final resultant. C# 没有 Eval 方法或类似的方法可以让您运行这样的语句来获得最终结果。 Unless I'm missing something, you'll have to parse through and reduce that way.除非我遗漏了一些东西,否则你将不得不解析并减少这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM