C#自动生成单词列表,并保存为文件

Auto-generate a list of words in C#, and save as File

我需要帮助编写接受单个参数的方法 long i:

public static string GetWord(long i)
{
    string s = "";

    //Update s using i

    return s;
}

...对于保存 ASCII 字文件的程序...

public static void main(string[] args) 
{
    try
    {
        int First = int.Parse(args[0]);
        int Last = int.Parse(args[1])
        string Filename = args[2]

        for(int i = start; i <= end; i++)
             File.AppendLine(Filename, GetWord(i));

        Console.WriteLine("Process complete");

    } 
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);

    }

}

...具有以下模式。

GetWord(0) 应该是 Encoding.ASCII.GetString(new byte[]{ 0 });

的结果

GetWord(1) 应该是 Encoding.ASCII.GetString(new byte[]{ 1 });

的结果

GetWord(2) 应该是 Encoding.ASCII.GetString(new byte[]{ 2 });

的结果

...等等,直到 GetWord(127)

GetWord(128) 应该是 GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 0 });

的结果

GetWord(129) 应该是 GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 1 });

的结果

GetWord(130) 应该是 GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 2 });

的结果

...等等,直到 GetWord(255)

GetWord(256) 应该是 GetWord(1) + Encoding.ASCII.GetString(new byte[]{ 0 });

的结果

GetWord(257) 应该是 GetWord(1) + Encoding.ASCII.GetString(new byte[]{ 1 });

的结果

GetWord(258) 应该是 GetWord(1) + Encoding.ASCII.GetString(new byte[]{ 2 });

的结果

...等等,直到 GetWord(16383)

GetWord(16384) 应该是 GetWord(0) + GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 0 });

的结果

GetWord(16385) 应该是 GetWord(0) + GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 1 });

的结果

GetWord(16386) 应该是 GetWord(0) + GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 2 });

的结果

写下模式有助于我思考代码。我希望这对每个人都有意义。我想我需要混合使用递归和模数来让它工作。

我只是给你算法。

注意每256个是一个循环。从索引 256 开始,您将追加到 Word[0],,从 512 开始,您将追加到 Word[1],从 65536 开始,您将追加到 Word[0] + Word[0],即 Word[256]

所以算法是将 index/256 - 1 作为您要追加的索引,而 index%256 将是您需要追加的字节

我想出的解决方案如下:

public void AppendFile(string filePath, long firstWord, long lastWord)
{            
    using (StreamWriter sw = File.AppendText(filePath))
    {
        for (long i = firstWord; i < lastWord; i++)
        {
            sw.WriteLine(GetWord(i));

        }

    }

}

public void AppendFile(string filePath, long lastWord)
{
    AppendFile(filePath, 0, lastWord);

}

public void AppendFile(string filePath)
{
    AppendFile(filePath, long.MaxValue);

}

public static string GetWord(long i)
{
    string s = Encoding.ASCII.GetString(new byte[] { (byte)(i % 128) });

    if (i < 128)
        return s;

    return GetWord(i / 128) + s;

}

使用方法如下:

AppendFile("words.txt"); // Will most likely fall over or at least take a long time

AppendFile("words.txt", 1000); // This is the method requested

AppendFile("words.txt", 500, 1000); // Extended functionality

Note: I chose to NOT use the algorithm from Steve's answer. The reason I did not use Steve's algorithm is because he relies on the full array of words being resident in memory during the full procedure, which restricts the output file to the amount of free available RAM. My version doesn't have this restriction, and is restricted only to the max possible length of a string.