C# 创建基本文本文件中每个特定行组合的文本文件

C# 创建基本文本文件中每个特定行组合的文本文件,c#,file,text,permutation,combinations,C#,File,Text,Permutation,Combinations,好的,希望我能解释得足够详细,以便有人能帮助我。。我正在用C#编写一个程序,它应该获取一个文本文件并替换特定的文本(碰巧是文件名),然后为给定文件名的每个组合打印一个新的文本文件。更改文件名文本的特定位置有自己的一组可能的文件名,如下所述的数组所示。无论每个位置有多少个文件名以及文件名的总位置有多少,程序都应该运行。如果您真的想让它很棒,可以稍微优化一下,因为您知道在任何单个文本文件中都不应该复制任何文件名 text是组成整个文件基础的行数组 lineNum保存文件名条目的行位置数组 previ

好的,希望我能解释得足够详细,以便有人能帮助我。。我正在用C#编写一个程序,它应该获取一个文本文件并替换特定的文本(碰巧是文件名),然后为给定文件名的每个组合打印一个新的文本文件。更改文件名文本的特定位置有自己的一组可能的文件名,如下所述的数组所示。无论每个位置有多少个文件名以及文件名的总位置有多少,程序都应该运行。如果您真的想让它很棒,可以稍微优化一下,因为您知道在任何单个文本文件中都不应该复制任何文件名

text
是组成整个文件基础的行数组

lineNum
保存文件名条目的行位置数组

previousFiles
是以前使用的文件名数组,从文件中已有的文件名开始

files
是可能的文件名的交错二维数组,其中
files[1]
将是第二个位置所有可能的文件名的数组

下面是一个示例,它将如何处理3个单独的文件名位置,第一个位置给出3个可能的文件名,第二个位置给出8个可能的文件名,第三个位置给出3个可能的文件名

哦,假设buildNewFile可以工作

        int iterator = 0;
        for (int a = 0; a < 3; a++)
        {
            for (int b = 0; b < 8; b++)
            {
                for (int c = 0; c < 3; c++)
                {
                    iterator++;
                    text[lineNums[0]] = text[lineNums[0]].Replace(previousFiles[0], files[0][a]);
                    text[lineNums[1]] = text[lineNums[1]].Replace(previousFiles[0], files[0][a]);
                    text[lineNums[2]] = text[lineNums[2]].Replace(previousFiles[1], files[1][b]);
                    text[lineNums[3]] = text[lineNums[3]].Replace(previousFiles[1], files[1][b]);
                    text[lineNums[4]] = text[lineNums[4]].Replace(previousFiles[2], files[2][c]);
                    text[lineNums[5]] = text[lineNums[5]].Replace(previousFiles[2], files[2][c]);
                    previousFiles = new string[] { files[0][a], files[1][b], files[2][c] };
                    buildNewFile(text, Info.baseFolder + "networks\\" + Info.dsnFilename + iterator + ".dsn");
                }
            }
        }
int迭代器=0;
对于(int a=0;a<3;a++)
{
对于(int b=0;b<8;b++)
{
对于(int c=0;c<3;c++)
{
迭代器++;
text[lineNums[0]]=text[lineNums[0]]。替换(以前的文件[0],文件[0][a]);
text[lineNums[1]]=text[lineNums[1]]。替换(以前的文件[0],文件[0][a]);
text[lineNums[2]]=text[lineNums[2]]。替换(以前的文件[1],文件[1][b]);
text[lineNums[3]]=text[lineNums[3]]。替换(以前的文件[1],文件[1][b]);
text[lineNums[4]]=text[lineNums[4]]。替换(以前的文件[2],文件[2][c]);
text[lineNums[5]]=text[lineNums[5]]。替换(以前的文件[2],文件[2][c]);
previousFiles=新字符串[]{files[0][a],files[1][b],files[2][c]};
buildNewFile(text,Info.baseFolder+“networks\\”+Info.dsnFilename+iterator+“.dsn”);
}
}
}

如果你们能帮我的话,非常感谢你们,我就是不知道如何递归地做。如果你有任何问题,我会回答并在这里编辑以反映出来。

我花了一点时间才弄明白你真正想做什么。这个问题不需要递归就可以解决,诀窍是查看您拥有的数据并将其转换为更可用的格式

您的“文件”数组是最不方便的。诀窍是将数据转换成可用的排列。为此,我建议利用收益率并使用返回IEnumerable的方法。代码如下:

public IEnumerable<string[]> GenerateFileNameStream(string[][] files)
{
    int[] current_indices = new int[files.Length];
    current_indices.Initialize();
    List<string> file_names = new List<string>();

    while (current_indices[0] < files[0].Length)
    {
        file_names.Clear();

        for (var index_index = 0; index_index < current_indices.Length; index_index++)
        {
            file_names.Add(files[index_index][current_indices[index_index]]);
        }

        yield return file_names.ToArray();

        // increment the indices, trickle down as needed
        for (var check_index = 0; check_index < current_indices.Length; check_index++)
        {
            current_indices[check_index]++;

            // if the index hasn't rolled over, we're done here
            if (current_indices[check_index] < files[check_index].Length) break;

            // if the last location rolls over, then we are totally done
            if (check_index == current_indices.Length - 1) yield break;

            // reset this index, increment the next one in the next iteration
            current_indices[check_index] = 0;
        }
    }
}
这段代码只是从枚举器获取结果并为您生成文件。基于示例代码的假设是每个文件名位置在每个文件中使用两次(因为
lineNum
数组的长度是
files
位置计数的两倍)

我还没有完全测试所有的代码,但算法的关键就在那里。关键是将数据转换成更有用的形式,然后进行处理。我在这里提出问题时的另一个建议是将问题更多地描述为“问题”如果你详细描述了你想要达到的目标,而不是展示代码,你可以对问题有更多的了解

public void MakeItWork(string[][] files, int[] lineNum, string[] text, string[] previousFiles)
{
    var iterator = 0;
    var filenames = GenerateFileNameStream(files);

    // work a copy of the text, assume the "previousFiles" are in this text
    var text_copy = new string[text.Length];

    foreach (var filenameset in filenames)
    {
        iterator++;
        Array.Copy(text, text_copy, text.Length);

        for (var line_index = 0; line_index < lineNum.Length; line_index++)
        {
            var line_number = lineNum[line_index];
            text[line_number] = text[line_number].Replace(previousFiles[line_index], filenameset[line_index / 2]);
        }

        buildNewFile(text_copy, Info.baseFolder + "networks\\" + Info.dsnFilename + iterator + ".dsn");
    }
}