开发者

Special Counters

I`m trying to create a special counter in C# ... I mean the counter will be consisting of characters not numbers.

I have a char[] of size 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];
            }
        }
    }
}

if the strSize < 3 the application works fine and gives accurate output. If the strSize >= 3, the application goes in infinite loop!

Need help.

if this is hard this way, I would need a way to create a numerical counter and I`ll work on it to suite my application.


You haven't shown what half of your methods or parameters are.

Personally I would take a different approach. I would use an iterator block to make it easy to return an IEnumerable<string>, and internally just keep an integer counter. Then you just need to write a method to convert a counter value and "alphabet of digits" into a string. Something like this:

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

Another alternative is to do the same thing with LINQ, if a range of int is enough:

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

In either case, you just iterate over the returned sequence to get appropriate counter values.

With those in place, you just need to implement ConvertToString - which would probably be something like this:

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

Here's a test program showing it all working:

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

Output:

AAA
AAB
AAC
AAD
ABA
ABB
ABC
ABD
ACA
ACB
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜