我試圖產生可能是這樣的事情一個公式,這只是一個樣本,如何解析以下字符串
A + B + C > D - A
現在,A,B,C,d,等等都是列名一張表格(如excel表格)我將在內存中訪問。
我需要生成一個規則,就像上面的A + B + C > D - A
一樣,它將決定用戶可以在Cell中添加什麼樣的值。
目前我就是這樣開始:
string toValidate = "A + B + C > D + E - A";
string lhs = "", rhs = "";
string[] comparisonOperators = new string[] { "=", ">", "<", "<>", "!=" };
char[] arithmeticOperators = { '+', '-', '/', '*' };
toValidate = toValidate.Replace(@" ", "");
for (int i = 0; i < comparisonOperators.Length; i++)
{
if (toValidate.Contains(comparisonOperators[i]))
{
operatorIndex = toValidate.IndexOf(comparisonOperators[i]);
break;
}
}
lhs = toValidate.Substring(0, operatorIndex);
rhs = toValidate.Substring(operatorIndex + 1);
string[] columnLhsList = lhs.Split(arithmeticOperators);
string[] columnRhsList = rhs.Split(arithmeticOperators);
然而,即使我有字符串作爲lhs
和rhs
,甚至我的運營商,其>
在上面的代碼,我不能夠理解我怎麼能在工作表本身上應用公式。我只需要知道哪個Column有哪個操作員關聯。
因爲我在另一種情況下A
之前有個別的列名,但不是經營者面前,爲e.g,
+
之前A
-
。
我如何解析上述請幫助。