.NET C# 逻辑函数,循环函数

发布于 2024-11-09 22:45:13 字数 662 浏览 2 评论 0原文

我有一些格式类似于

XXXX-XXXX-X_X_ 的

字符串,所有“_”都应该替换为字母和数字,以产生这样的结果:

XXXX-XXXX-XAXA
XXXX-XXXX-XAXB
XXXX-XXXX-XAXC
XXXX-XXXX-XAXD
XXXX-XXXX-XAXE
XXXX-XXXX-XAXF
XXXX-XXXX-XAXG
(...)
XXXX-XXXX-XZX8
XXXX-XXXX-XZX9
XXXX-XXXX-X0XA
(...)
XXXX-XXXX-X2XA
XXXX-XXXX-X2XB

我知道用一个“_”来制作它。

                string alphaLetters = "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789ABCDEF";
                foreach (char letter in alphaLetters.ToCharArray())
                {
                    Numbers.Add(number.Replace('_', letter)));                     
                }

我希望这段代码能够处理未知数量的“_”。

你能帮忙吗?

I have some string in format like that

XXXX-XXXX-X_X_

All "_" should be replaced with Letters and numberst to prodce sth like that:

XXXX-XXXX-XAXA
XXXX-XXXX-XAXB
XXXX-XXXX-XAXC
XXXX-XXXX-XAXD
XXXX-XXXX-XAXE
XXXX-XXXX-XAXF
XXXX-XXXX-XAXG
(...)
XXXX-XXXX-XZX8
XXXX-XXXX-XZX9
XXXX-XXXX-X0XA
(...)
XXXX-XXXX-X2XA
XXXX-XXXX-X2XB

I know hoe to make it with one "_".

                string alphaLetters = "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789ABCDEF";
                foreach (char letter in alphaLetters.ToCharArray())
                {
                    Numbers.Add(number.Replace('_', letter)));                     
                }

I want this code to be working with unknown number of "_".

Can you help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2024-11-16 22:45:13

恕我直言,它必须是递归的。 (注意:这并不意味着它必须使用递归方法调用,虽然我在下面的代码中使用了递归调用,但它可以很容易地转换为内部递归堆栈。)

public static void RunSnippet()
{
    var r = new List<string>();
    Replace("asd_asd_asd_".ToCharArray(), 0, r);
    foreach(var s in r) { Console.WriteLine(s); }
}

public static char[] possibilities = new char[] { 'A', 'B', 'C' };

public static void Replace(char[] chars, int startIndex, IList<string> result)
{       
    for (int i = startIndex; i < chars.Length; i++)
    {
        if (chars[i] != '_')
        {
            continue;
        }

        // we found first '_'
        for (int j = 0; j < possibilities.Length; j++)
        {
            chars[i] = possibilities[j];
            Replace(chars, i + 1, result);              
        }

        chars[i] = '_'; // take back what we replaced
        return; //we're done here
    }

    // we didn't find any '_', so all were replaced and we have result:
    result.Add(new string(chars));
}

IMHO it must be recursive. (Note: that does not mean it must use recursive method call, although I used recursive call in the following code, it can be easily converted to internal recursion stack. )

public static void RunSnippet()
{
    var r = new List<string>();
    Replace("asd_asd_asd_".ToCharArray(), 0, r);
    foreach(var s in r) { Console.WriteLine(s); }
}

public static char[] possibilities = new char[] { 'A', 'B', 'C' };

public static void Replace(char[] chars, int startIndex, IList<string> result)
{       
    for (int i = startIndex; i < chars.Length; i++)
    {
        if (chars[i] != '_')
        {
            continue;
        }

        // we found first '_'
        for (int j = 0; j < possibilities.Length; j++)
        {
            chars[i] = possibilities[j];
            Replace(chars, i + 1, result);              
        }

        chars[i] = '_'; // take back what we replaced
        return; //we're done here
    }

    // we didn't find any '_', so all were replaced and we have result:
    result.Add(new string(chars));
}
听你说爱我 2024-11-16 22:45:13

试试这个:

var alphaIndexes = new List<int>();
string alphaLetters = "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789ABCDEF";

for(int n = 0; n<Numbers.Count; n++) {
    char[] numberLetters = Numbers[n].ToCharArray();

    int position = 0;

    for(int i = numberLetters.Length - 1; i>=0; i--) {  
        if(numberLetters[i] == '_') {
            int alphaIndex = 0;
            if(alphaIndexes.Count <= position)
                alphaIndexes.Add(0);
            else {
                alphaIndex = alphaIndexes[position];
            }

            numberLetters[i] = alphaLetters[alphaIndex];
            position++;         
        }
    }

    if(alphaIndexes.Count > 0) {
        alphaIndexes[0]++;

        for(int j = 0; j < alphaIndexes.Count; j++) {
            if(alphaIndexes[j] >= alphaLetters.Length) {
                alphaIndexes[j] = 0;
                if (j < alphaIndexes.Count)
                    alphaIndexes[j+1]++;
            }
        }
    }
    Numbers[n] = new String(numberLetters);
    Numbers[n].Dump();
}

Try this one:

var alphaIndexes = new List<int>();
string alphaLetters = "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789ABCDEF";

for(int n = 0; n<Numbers.Count; n++) {
    char[] numberLetters = Numbers[n].ToCharArray();

    int position = 0;

    for(int i = numberLetters.Length - 1; i>=0; i--) {  
        if(numberLetters[i] == '_') {
            int alphaIndex = 0;
            if(alphaIndexes.Count <= position)
                alphaIndexes.Add(0);
            else {
                alphaIndex = alphaIndexes[position];
            }

            numberLetters[i] = alphaLetters[alphaIndex];
            position++;         
        }
    }

    if(alphaIndexes.Count > 0) {
        alphaIndexes[0]++;

        for(int j = 0; j < alphaIndexes.Count; j++) {
            if(alphaIndexes[j] >= alphaLetters.Length) {
                alphaIndexes[j] = 0;
                if (j < alphaIndexes.Count)
                    alphaIndexes[j+1]++;
            }
        }
    }
    Numbers[n] = new String(numberLetters);
    Numbers[n].Dump();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文