The development of host computer programs, sometimes require users to the logic of the known conditions to edit, and then the program to determine the authenticity of logic. For example, you already know aa,bb,cc,dd,ee,ff six conditions of true or false, but these six conditions between the logic needs of users to fill out, then how to automatically judge the final result of this expression after the user fills in.
Let's start with a simpler expression, such as now only aa,bb,cc three conditions, aa&&bb| | The CC corresponds to a bool value of {True, False, true}, where only the logical operators in the expression are extracted and then the array of bool values is evaluated through a for loop. But if the expression is more complex, for example, "(AA &&! BB) | | CC) && DD | | (EE && FF), which evaluates to {true, False, False, True, False, true}, which requires the parentheses to be processed first. Ideas are as follows:
1. Replace a condition name in an expression with a bool value
(AA &&! BB) | | CC) && DD | | (EE && FF) → ((true &&!false) | | false) && true | | (False && True)
2. Find the last opening parenthesis and its right distance to his nearest closing parenthesis, there must be no other parentheses between the brackets.
As the above example is where the (EE&&FF) position corresponds (false && true)
3. The expression in the middle bracket is calculated, and the parentheses are replaced with the result
(False && True) →false
((True &&!false) | | false) && true | | False
4. Continue to simplify this approach until the final results are achieved
The following code is implemented
Class Program {static void Main (string[] args) {bool[] Arrlogicvalue = new Bool[6] {tru
E, False, False, True, False, true}; Suppose all bool values are expressed in one letter or multiple letters, but the number of uniform letters is required String logicexpression = "(AA &&! BB) | | CC) && DD | |
(EE && FF) ";
Remove space, bracket string logicexpressiontemp = Logicexpression.replace ("", "");
Logicexpressiontemp = Logicexpressiontemp.replace ("(", "");
Logicexpressiontemp = Logicexpressiontemp.replace (")", "" ");
Replace the logical operator with a comma string logicexpressionvariable = Logicexpressiontemp.replace ("&&", ",");
logicexpressionvariable = Logicexpressionvariable.replace ("| |", ",");
Remove all the condition names in the logical expression string[] arrvariable = Logicexpressionvariable.split (', '); The condition name is replaced with its corresponding bool value for (int i = 0; i < arrvariable.length i++) {if ArrvariAble[i].
Contains ("!")) Logicexpression = Logicexpression.replace (Arrvariable[i], (!arrlogicvalue[i)).
ToString ()); else Logicexpression = Logicexpression.replace (Arrvariable[i], arrlogicvalue[i].
ToString ());
BOOL Finnalresult = Dealbrackets (logicexpression);
Console.WriteLine ("Final result: {0}", Finnalresult);
Console.readkey (); }///<summary>///handle parentheses///</summary>///<param name= "Logicexpression" & gt;</param>///<returns></returns> private static bool Dealbrackets (String logicexpress
ION) {while (Logicexpression.contains ("()) {//Last left parenthesis
int lasttleftbracketindex =-1;
Right bracket int firstrightbracketindex =-1 corresponding to the last first opening parenthesis; Find the last opening parenthesis for (int i = 0; i < LogicexpressiOn. Length; i++) {//Get the character string Tempchar = Logicexpression.substring (
I, 1); If it is an opening parenthesis, the index number of the character is given to Lasttleftbracketindex until the last if (Tempchar = = () lasttleft
Bracketindex = i; //Find the right parenthesis corresponding to the last first opening parenthesis for (int i = Lasttleftbracketindex i < logicexpression.length; i++) {//Get the character string Tempchar = Logicexpression.substring (
I, 1);
if (Tempchar = = ")" && Firstrightbracketindex = = 1) Firstrightbracketindex = i; String calculateexpression = logicexpression.substring (Lasttleftbracketindex + 1, Firstrightbra
CKETINDEX-LASTTLEFTBRACKETINDEX-1);
BOOL Logicresult = Logicoperate (calculateexpression); Logicexpression = Logicexpression.replace ("("+ calculateexpression +") ", logicresult.tostring ());
BOOL Finnalresult = Logicoperate (logicexpression);
return finnalresult; }///<summary>///operational Logic expression///</summary>///<param name= "Logicexpressio N "></param>///<returns></returns> private static bool Logicoperate (String logicexpr
ession) {//Remove space logicexpression = Logicexpression.replace ("", "");
Gets the bool value of all the conditions string logicexpressionvalue= logicexpression.replace ("&&", ",");
Logicexpressionvalue = Logicexpressionvalue.replace ("| |", ",");
string[] Arrlogicvalue = Logicexpressionvalue.split (', ');
Gets the logical operator string logicexpressionoperator = Logicexpression of an expression;
Logicexpressionoperator = Logicexpressionoperator.replace ("True", ","); Logicexpressionoperator = LogiceXpressionoperator.replace ("False", ",");
Logicexpressionoperator = Logicexpressionoperator.remove (0, 1);
Logicexpressionoperator = Logicexpressionoperator.remove (logicexpressionoperator.length-1, 1);
string[] Arroperator = Logicexpressionoperator.split (', ');
Final operation result bool Logicresult = Convert.toboolean (arrlogicvalue[0]); The number of more logical operators is calculated by looping (does not contain the "!" for (int i = 0; i < arroperator.length i++) {if (arroperator[i] = = "&&A
MP; ")
{Logicresult = Logicresult && Convert.toboolean (arrlogicvalue[i + 1]);
else if (arroperator[i] = = "| |") {logicresult = Logicresult | |
Convert.toboolean (Arrlogicvalue[i + 1]);
} return Logicresult; }
}