I am working on converting the boolean algebra expression something like this
NOT(a AND b) = (NOT a) OR (NOT b) NOT (a OR b) = (NOT a) AND (NOT b) a AND NOT (b AND c) = (EQ a) AND ( ( NOT b) OR ( NOT c) )
But, somehow it is not working for certain conditions like
NOT a AND b = (NOT a) AND (EQ b) a AND NOT b AND NOT c = (EQ a) AND (NOT b) AND (NOT c)
Following is the logic for conversion. Am i doing anything wrong?
public string ExpressionConversion(string expression) { string finalVal = null; string currentToken = ""; bool isNotFound = false; List<string> strList = new List<string>(); StringHelper stringHelper = new StringHelper(); var values = stringHelper.CleverSplit(expression, ' ', false, true); //function which splits all the elements in the expression for (int j = 0; j < values.Length; j++) { currentToken = values[j].Trim(); if (string.IsNullOrEmpty(currentToken.Trim())) continue; if ((j > 0) && currentToken.StartsWith("AND") && values[j - 1].StartsWith("AND")) continue; if (currentToken.Contains("NOT")) { isNotFound = true; continue; } if (currentToken.StartsWith("(")) strList.Add(currentToken); else if (currentToken.StartsWith(")")) { strList.Add(currentToken); isNotFound = false; } else if (currentToken.StartsWith("AND")) { if (isNotFound) strList.Add(" OR "); else strList.Add(" AND "); } else if (currentToken.StartsWith("OR")) { if (isNotFound) strList.Add(" AND "); else strList.Add(" OR "); } else { if (isNotFound) { strList.Add("( NOT " + currentToken + " )"); if (!expression.Contains("(")) isNotFound = false; } else strList.Add("( EQ " + currentToken + " )"); } } if (strList.Count > 0) finalVal = string.Join(" ", strList); return finalVal; }