私はIF
ステートメントを以下のように変換しようとしています
SET RESULT = IF(C>(X-2), (A+B), C) //IF C< X-2 return A+B otherwise return C
以下のような条件文に
SET RESULT = C>(X-2)?(A+B):C
文字列全体をスキャンし、、、およびの出現を確認するコードを作成しIF
まし(
た,
。IF
以下のようなステートメントが複数ある場合、私のアルゴリズムは機能しません
SET RESULT = IF(C>(X-2), IF(P>C,2,3),C)
これがコードです...
string data = "SET RESULT=IF(C>(X-2), (A+B), C)";
string output = string.Empty;
int indexofIF = data.IndexOf("IF");
int obCount = 0;
int cbCount = 0;
if (indexofIF > -1)
{
string script = data.Substring(indexOfIF, (data.Length-indexOfIF-1))
for(int index=0; index<script.Length; index++)
{
int obIndex = data.IndexOf('(', index);
if(obIndex>-1)
obCount++;
int cbIndex = data.IndexOf(')', index);
if(cbIndex>-1)
cbCount++;
if(obCount==cbCount)//Found the end of If statement
{
string tempData = data.Substring(0, index);
int count = tempData.Count(f => f == ',');//Get the number of occurences of ','
if (count == 2)//There are only 2 commas
{
int firstIndex = tempData.IndexOf(',');
int lastIndex = tempData.LastIndexOf(',');
string condtion = tempData.Substring(3, (firstIndex - 4));
string trueCond = tempData.Substring(firstIndex + 1, (lastIndex - firstIndex - 1));
string falseCond = tempData.Substring(lastIndex + 1, (index - lastIndex - 1));
output = condtion + "?" + trueCond + ":" + falseCond;
}
else //More than 2 commas
{
}
}
}
}
これが複雑なシナリオで機能するかどうかはわかりません。これを行う他のより良い方法はありますか?おそらく、regex
または他の文字列置換操作を使用しています。