特别计数器

时间:2011-01-01 14:01:44

标签: c#

我试图在C#中创建一个特殊的计数器...我的意思是计数器将由字符而不是数字组成。

我的char[]大小为3:

char[] str = new char[strSize];
int i = 0;
int tmpSize = strSize - 1;
int curr;
while(!isEqual(str,finalStr,strSize))
{
    str[strSize] = element[i % element.Length];
    i++;
    if (str[strSize] == element[element.Length - 1])
    {
        int j = strSize - 1;
        if (j > 0)
        {
            j--;
            int tmpCntr = j+1;
            curr = getCurrentID(str[tmpCntr]);
            str[tmpCntr] = element[(curr + 1) % element.Length];
            while (str[tmpCntr] == element[0] && (i % element.Length > 0) && tmpCntr < 0)
            {
                tmpCntr--;
                curr = getCurrentID(str[tmpCntr]);
                str[tmpCntr] = element[(curr + 1) % element.Length];
            }
        }
    }
}

如果strSize < 3应用程序正常工作并提供准确的输出。如果strSize >= 3,应用程序进入无限循环!

需要帮助。

如果这样做很难,我需要一种方法来创建一个数字计数器,我会在它上面设置我的应用程序。

1 个答案:

答案 0 :(得分:3)

您尚未显示一半的方法或参数。

我个人会采取不同的方法。我会使用迭代器块来轻松返回IEnumerable<string>,而内部只保留一个整数计数器。然后你只需要编写一个方法来将计数器值和“数字字母”转换为字符串。像这样:

public static IEnumerable<string> Counter(string digits, int digitCount)
{
    long max = (long) Math.Pow(digits.Length, digitCount);
    for (long i = 0; i < max; i++)
    {
        yield return ConvertToString(i, digits, digitCount);
    }
}

另一种选择是使用LINQ做同样的事情,如果范围int就够了:

public static IEnumerable<string> Counter(string digits, int digitCount)
{
    int max = (int) Math.Pow(digits.Length, digitCount);
    return Enumerable.Range(0, max)
                     .Select(i => ConvertToString(i, digits, digitCount));
}

在任何一种情况下,您只需迭代返回的序列以获得适当的计数器值。

有了这些,你只需要实现ConvertToString - 这可能是这样的:

public static string ConvertToString(long value, string digits, int digitCount)
{
    char[] chars = new char[digitCount];
    for (int i = digitCount - 1 ; i >= 0; i--)
    {
        chars[i] = digits[(int)(value % digits.Length)];
        value = value / digits.Length;
    }
    return new string(chars);
}

这是一个显示所有工作的测试程序:

using System;
using System.Collections.Generic;
using System.Linq;

class Test
{
    static void Main()
    {
        // Show the first 10 values
        foreach (string value in Counter("ABCD", 3).Take(10))
        {
            Console.WriteLine(value);
        }
    }

    public static IEnumerable<string> Counter(string digits, int digitCount)
    {
        long max = (long) Math.Pow(digits.Length, digitCount);
        for (long i = 0; i < max; i++)
        {
            yield return ConvertToString(i, digits, digitCount);
        }
    }

    public static string ConvertToString(long value,
                                         string digits,
                                         int digitCount)
    {
        char[] chars = new char[digitCount];
        for (int i = digitCount - 1 ; i >= 0; i--)
        {
            chars[i] = digits[(int)(value % digits.Length)];
            value = value / digits.Length;
        }
        return new string(chars);
    }
}

输出:

AAA
AAB
AAC
AAD
ABA
ABB
ABC
ABD
ACA
ACB