0

私のプログラムは、特別な数式 (例を参照) を明示的な数式に拡張しようとしています。フルムラを表す用語がいくつかあります。 "(,)" およびすべての角かっこが "{}[]" の形をしているわけではありません - 数式には、文字 (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:line 156 の myProject.Formula.PopulateStartEnd() C:\Projects_2012\Project_Noam\Project\myProject\ の myProject.Formula.FigureInput(String st) myProject\Formula.cs:C:\Projects_2012\Project_Noam\Project\myProject\myProject\Formula.cs:line 36 の myProject.Formula.generateButton_Click(Object sender, EventArgs e) の行 135

4

1 に答える 1

0

の最も内側のループでは、closeがより小さいことを確認することはありません。注: C# の文字列は null で終了しません。end.LengthforFigureInput

ループを次のように変更します。

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

初期化して適切なサイズで表示することはありませendstart。それらが十分な大きさであることを確認するかList<int>、 でインデックスを追加できる を使用することをお勧めしますAdd

于 2012-04-11T11:13:45.500 に答える