0

我的程序试图将特殊公式(参见示例)扩展为明确的公式。furmula 必须代表的术语很少:-整个公式中没有空格(“”)-它的公式只有圆括号"(,)" 而不是所有的括号形式"{}[]" - 公式只包含字母(az,AZ)、数字(也包括数字)和圆括号。- 对于每个左括号,必须是合适的右括号。-to 公式不能以括号开头。

这里有一些例子: * 输入:'abz2(3(a)2(ab))a 输出:'abzaaaababaaaababa

  • 输入:'a2(A)6(g2(a))' 输出:'aAAgagagagagaga'

这是代码:

        bool ExistBrackets(string st)
    {
        for (int i = 0; i < st.Length; i++)
        {
            if (IsBracket(st[i]))
                return false;
        }
        return true;
    }

    string AddChainDeleteBracks(int open, int close, string input)
    {
        string to="",from="";
        //get the local chain multipule the number in input[open-1]

        //the number of the times the chain should be multiplied
        for (int i = input[open - 1]; i > 0; i--)
        {
            //the content
            for (int m = open + 1; m < close; m++)
            {
                to = to + input[m];
            }
        }

        //get the chain i want to replace with "to"
        for (int j = open - 1; j <= close; j++)
        {
            from = from + input[j];
        }
        String output = input.Replace(from, to);
        return output;
    }

    private void PopulateStartEnd()
   {
       //assuming that start and end are empty
       int cS=0,cE=0;
       //populate start
       for (int i = 0; i < (textBox1.Text.Length); i++)
       {
           if (textBox1.Text[i] == '(')
           {
               start[cS] = i;
               cS++;
           }
           if (textBox1.Text[i] == ')')
           {
               end[cE] = i;
               cE++;
           }
       }

       }


    private string FigureInput(string st)
    {
        int i,close;
        PopulateStartEnd();
        //get the index in the start array which is populated
        for (i = start.Length - 1; start[i] != 0; i--) ;
        //populate the last letters if there is outside the brackets
        while (!ExistBrackets(st))
        {
            //loop on the latest opening brackets in start array
            for (; i >= 0; i++)
            {
                //find the suitable closing brackets in the end array
                for (close = 0; ((end[close] > start[i]) && (end[close] != null)); close++) ;
                st=AddChainDeleteBracks(i, close, st);
            }
        }

        return st;

    }

主要方法是FigureInput

我得到的错误:

** * **异常文本** * **** System.IndexOutOfRangeException:索引超出了数组的范围。在 C:\Projects_2012\Project_Noam\Project\myProject\myProject\Formula.cs:myProject.Formula.FigureInput(String st) 在 C:\Projects_2012\Project_Noam\Project\myProject\ 的 myProject.Formula.FigureInput(String st) 中的 myProject.Formula.PopulateStartEnd() myProject\Formula.cs:位于 C:\Projects_2012\Project_Noam\Project\myProject\myProject\Formula.cs 中 myProject.Formula.generateButton_Click(Object sender, EventArgs e) 的第 135 行:第 36 行

4

1 回答 1

0

您永远不会检查close小于end.Length在. 注意:C# 中的字符串不是以空值结尾的。forFigureInput

将循环更改为:

for (close = 0; (close < end.Length && (end[close] > start[i])); close++) ;

我从来没有看到你初始化endstart有足够的大小。确保它们足够大或更好地使用List<int>可以添加索引的Add.

于 2012-04-11T11:13:45.500 回答