[Solved] To check logic in statements which is in a word document [closed]


Get yourself and enum with the values. Easier to handle than strings.

    public enum Vals
    {
        IF,
        THEN,
        ENDIF,
        ELSE,
    }

We have a method for comparing if the next value is correct pair with the current.

    public static bool IsValidFollower(Vals val1, Vals val2)
    {
        if (val1 == Vals.IF)
            return val2 == Vals.THEN;
        if (val1 == Vals.THEN)
            return val2 == Vals.ENDIF;
        if (val1 == Vals.ENDIF)
            return val2 == Vals.IF || val2 == Vals.ELSE;
        if (val1 == Vals.ELSE)
            return val2 == Vals.THEN;
        return false;
    }

And then instead of stack we use a normal list. (Just a personal preference, I dont want them in reverse order)

        List<Vals> ListWords = new List<Vals>();

        foreach (string str in s.Split(' '))
        {
            if (str.Contains("ENDIF"))
                ListWords.Add(Vals.ENDIF);

            else if (str.Contains("ELSE"))
                ListWords.Add(Vals.ELSE);

            else if (str.Contains("THEN"))
                ListWords.Add(Vals.THEN);

            else if (str.Contains("IF"))
                ListWords.Add(Vals.IF);
        }

When we have the list of all the values. We check if they are logically valid.

        bool valid = true;
        for (int i = ListWords.Count - 1; i +1< ListWords.Count; i++)
        {
            if (!IsValidFollower(ListWords[i], ListWords[i + 1]))
            {
                valid = false;
                break;
            }
        }

16

solved To check logic in statements which is in a word document [closed]