检查word文档中的语句中的逻辑

时间:2013-02-22 08:19:14

标签: c# .net

如何使用c#

检查IF,然后,Else和End If单词文档中的条件

请建议我一些代码......或者帮我解释逻辑。 提前谢谢..

Stack stackWords = new Stack();
string[] words = SplitWords(sLine);

foreach(string s in words)
{
    if(s.Contains("IF") || s.Contains("ELSE") ||  (sLine.Contains("THEN")) || (sLine.Contains("ENDIF")))
    {
        for (; i < words.Length; i++)
        {
            if (words[i] == "IF" || words[i] == "ELSE" ||
            words[i] == "ELSEIF" || words[i] == "THEN" || words[i] == "END IF")
            {
                stackWords.Push(words[i]);
            }
        }
       // stackWords.Push(s);
    }
  //  MessageBox.Show(s);
}

2 个答案:

答案 0 :(得分:1)

根据@ Evelie的回答,这里是一个工作示例程序,用于检查(我相信我在验证方法中介绍了一些情况,并且我还在序列中添加了一些错误关键字的跟踪)。

备注:如果这有帮助,请将@ Evelie的回答标记为答案。我刚发布这个给你留下了整套的印象。

class Program
{
    static void Main(string[] args)
    {
        Stack stackWords = new Stack();

        var app = new Microsoft.Office.Interop.Word.Application();
        var file = Environment.CurrentDirectory + @"\doc.docx";
        var document = app.Documents.Open(file);

        try
        {
            int count = document.Words.Count;
            for (int i = 1; i <= count; i++)
            {
                string text = document.Words[i].Text.Trim().ToUpper();
                Vals val;
                if(Enum.TryParse<Vals>(text, out val))
                {
                    Console.WriteLine("Word {0} = {1}", i, text);
                    if (stackWords.Count > 0)
                    {
                        var peeked = (Vals)stackWords.Peek();
                        if (IsValidFollower(peeked, val))
                        {
                            stackWords.Push(val);
                        }
                        else
                        {
                            throw new Exception(String.Format("Exception occured at word {0}. {1} was not expected after {2}",i, val, peeked));
                        }
                    }
                    else
                    {
                        stackWords.Push(val);
                    }
                }
            }
            Console.WriteLine("Syntax sequence is valid");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Syntax sequence is invalid");
            Console.WriteLine(ex.Message);
        }
        finally
        {
            document.Close();
            app.Quit();
        }

    }

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

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

答案 1 :(得分:0)

让自己和价值观一致。比字符串更容易处理。

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

我们有一种方法可以比较下一个值是否与当前值正确配对。

    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;
    }

然后我们使用普通列表而不是堆栈。 (只是个人偏好,我不希望它们按相反的顺序排列)

        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);
        }

当我们拥有所有值的列表时。我们检查它们是否在逻辑上有效。

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