如何使用c#增加char数组的最大大小?

时间:2015-06-05 13:57:44

标签: c# arrays

这是我的代码。问题是增加char数组的大小。它只适用于index = 4.它不会转到index = 5或forward。

    protected void Button2_Click(object sender, EventArgs e)
    {


        var guesser = new BrutePasswordGuesser();

        var guess = new String(guesser.CurrentGuess);
        while (textBox1.Text != guess)
        {
            textBox2.Text = guess;
            if (!guesser.NextGuess())
            {
                Label2.Text = "Maximum guess size reached.";
                break;
            }
            guess = new String(guesser.CurrentGuess);
        }

        if (textBox1.Text == guess)
  {
            textBox2.Text = guess;
            Label1.Text = "Password Correct";
        }
    }
    private class BrutePasswordGuesser
    {
        private const int MaxAscii = 126;
 //i have to use 13 here
        private const int MaxSize = 4;

        private const int MinAscii = 33;

        private int _currentLength;

        public BrutePasswordGuesser()
        {
            //Init the length, and current guess array.
            _currentLength = 0;
            CurrentGuess = new char[MaxSize];
            CurrentGuess[0] = (char)MinAscii;
        }

        public char[] CurrentGuess { get; private set; }

        public bool NextGuess()
        {
            if (_currentLength >= MaxSize)
            {
                return false;
            }

            //Increment the previous digit (Uses recursion!)
            IncrementDigit(_currentLength);

            return true;
        }

        /// <summary>
        /// Increment the character at the index by one. If the character is at the maximum 
        /// ASCII value, set it back to the minimum, and increment the previous character.
        /// Use recursion to do this, so that the proggy will step all the way back as needed.
        /// If the very bottom of the string is reached, add another character to the guess.
        /// </summary>
        /// <param name="digitIndex"></param>
        private void IncrementDigit(int digitIndex)
        {
            //Don't fall out the bottom of the array.
            //If we're at the bottom of the array, add another character
            if (digitIndex < 0)
            {
                AddCharacter();
            }
            else
            {
                //If the current character is max ASCII, set to min ASCII, and increment the previous char.

digitIndex达到4时,代码开始迭代,不会转到5或更远。

                if (CurrentGuess[digitIndex] == (char)MaxAscii)
                {

                    CurrentGuess[digitIndex] = (char)MinAscii;
                    IncrementDigit(digitIndex - 1);
                }
                else
                {

                    CurrentGuess[digitIndex]++;
                }
            }
        }

        private void AddCharacter()
        {
            _currentLength++;
            //If we've reached our maximum guess size, leave now and don't come back.
            if (_currentLength >= MaxSize)
            {
                return;
            }
            //Initialis as min ASCII.
            CurrentGuess[_currentLength] = (char)(MinAscii);
        }
    }

它仅适用于maxsize 0-4但我必须使用它为13和14.任何人都可以解决这个暴力代码来增加char数组的大小。

1 个答案:

答案 0 :(得分:0)

首次分配内存后,无法增加阵列的大小。在这段代码中,MaxSize等于4,因此当增加时它会从0到4运行良好,因为数组的大小有限。在您的情况下,MaxSize表示系统可以使用的最大大小,如果最大值为14,则只需更改一行,如下所示:

private const int MaxSize = 14;
相关问题