简体   繁体   中英

Converting if statement to conditional statement

I am trying to convert an IF statement as below

SET RESULT = IF(C>(X-2), (A+B), C) //IF C< X-2 return A+B otherwise return C

to a conditional statement as below

SET RESULT = C>(X-2)?(A+B):C

I have wrote a code which scan the whole string and look of occurrences of IF , ( and , . My algorithm is not working when there more than 1 IF statement such as below

SET RESULT = IF(C>(X-2), IF(P>C,2,3),C)

Here is the code...

        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
                    {

                    }
                }

            }

        }

I am not sure if this will work for complex scenarios. Is there any better other way of doing this? Perhaps using regex or any other string replacement operation..

You are on the right track.

I would:

  • Read each character into a small buffer
  • evaluate the buffer to look for the word "IF"
  • set a flag so you know you are in an IF statement
  • clear the buffer
  • evaluate for parenthesis
  • keep track of opening/closing of parenthesis
  • perform any additional logic (such as comma parsing)
  • continue until the end of the string

Perhaps using regex or any other string replacement operation..

(IF.*?(?=IF|$)) breaks out multiple IF statements but it doesn't handle the additional parsing and requires that the input data follow a strict structure. Keeping track of parenthesis is tricky/impossible (see comments on this answer) and more easily handled by a state machine.

Personally, I like the control/flexibility of a character-by-character parse. Tools like Code Mirror use this approach for parsing source code.

You may find that a regular expression is useful once you have successfully extracted a portion of the string (eg to deconstruct a formula like A + B > C into its parts).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM