可能是这样的?
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");
}
请注意,规范允许模棱两可的表述,例如:
"false and false or true"
"false and true or true"
如果首先计算和,则这两个表达式都是“真”,如果首先计算或,则这两个表达式都是“假” 。因此,在每个级别都需要括号会更好。要求从左到右求值是另一种选择,但这会使代码更复杂一些。
对于那些可能反对这种类型的问题的解决方案的人,请记住,一些数学家认为所有的数学都可能被简化为这种符号操作。据说,对罗素和怀特黑德的《数学原理》的主要批评之一是它包含了太多意义的公式。