【问题标题】:How to populate a file with random numbers?如何用随机数填充文件?
【发布时间】:2015-05-07 23:02:04
【问题描述】:

所以基本上我试图用 10^3 个完全随机数“填充”一个文件,所以我可以稍后将它们添加到二进制搜索树中。 这是我目前使用的填充函数:

void populateFile(BinarySearchTree b) {
    int random_integer;
    srand( time( NULL ) );
    std::ofstream myfile;
    string line;
    myfile.open ("output.txt");
    myfile << "Writing this to a file: ";
    for(int index=0; index<1000; index++)
    {
        random_integer = (rand()%1000)+1;
        cout << random_integer << endl;
        myfile << random_integer;
    }
    myfile.close();

    int value;
    ifstream file ("output.txt");
    if (file.is_open())
    {
        while ( getline (file,line) )
        {
            value = std::stoi(line);
            b.insert(value);
        }
        myfile.close();
    }

    else cout << "Unable to open file";

}

但我似乎无法写入文件,我只能在控制台上看到数字然后程序崩溃。

我的第二个担心如下: 我想将这些相同的数字添加到二叉搜索树中。我已经有一个类和一个 dd 函数,但我不知道如何继续。然后我希望能够完全随机地从 BST 中删除它们。

我已经编写了一个删除函数。这怎么可能? 任何想法将不胜感激。 P.S:我对 C++ 相当陌生,如果我的问题对你来说听起来很愚蠢,我很抱歉。

【问题讨论】:

  • 标题中的问题和帖子中的问题不同。真正的问题是什么?
  • “完全随机”rand 类型的冲突。 channel9.msdn.com/Events/GoingNative/2013/…
  • 问题:你想为一个文件生成 1000 个随机数---但是你将 RNG 范围保持在 1000。为什么不直接在文件中写出 1 到 1000 呢?你想在你的测试数据中重复吗?因为在您当前的生成技术中可能会有多个重复项。
  • @StarPilot 允许重复,我查看了几篇文章,指出这是我可以生成此类数字的方式。所以我实现了自己的功能。谢谢
  • 你可能想要myfile &lt;&lt; random_integer &lt;&lt; '\n';否则stoi会抛出out_of_range,这可能是导致崩溃的原因。

标签: c++ algorithm sorting binary-tree binary-search-tree


【解决方案1】:

我认为您的问题的解决方案在于@Praetorian 的评论:

你可能想要myfile &lt;&lt; random_integer &lt;&lt; '\n';。否则stoi 会抛出out_of_range,这很可能是导致崩溃的原因。

我有一些关于你的功能的通用建议。

  1. 把你的函数分成两个

    -- 用于写入文件的一种
    -- 用于从文件中读取并填充 BST。

  2. 不要在函数中使用硬编码的文件名或全局变量。将它们作为函数的参数。

  3. 始终检查 IO 操作的状态。处理失败。

  4. main 或驱动程序函数中播种随机数生成器。如果多次调用生成随机数的函数,则无需再次为随机生成器做种。

void populateFile(int count,
                  std::string const& file)
{
    std::ofstream myfile(file);
    if (!myfile )
    {
       // Deal with error.
       return;
    }

    for(int index=0; index<count; index++)
    {
        random_integer = (rand()%1000)+1;
        myfile << random_integer << "\n";
    }
}

void readFileAndBuildBST(std::string const& file,
                         BinarySearchTree& b)
{
    std::ifstream myfile(file);
    if (!myfile )
    {
       // Deal with error.
       return;
    }

    int number;
    while ( myfile >> number )
    {
       b.insert(number);
    }
}

void driver()
{
   // Seed the random number generator.
   srand( time( NULL ) );

   // Populate the file
   std::string file("output.txt");
   populateFile(1000, file);

   // Read the data from the file and flesh out the BST.
   BinarySearchTree b;
   readFileAndBuildBST(file, b);
}

将功能分为两个,您可以一次测试一个功能。如果一个函数出现问题,您可以在处理另一个函数之前调试并修复问题。

【讨论】:

  • 这真的很有帮助,我没想过将我的功能分成3。谢谢:)
  • @Predator44,将功能划分为可管理的小块需要一些经验。我很高兴能够提供帮助。
  • 正在努力! :) 再次感谢
【解决方案2】:

改变

cout << random_integer << endl;
myfile << random_integer;

到:

myfile << random_integer << endl;

顺便说一句,如果您只在程序的生命周期内需要数据,您可能想要使用缓冲区,甚至将数字直接添加到您的二叉搜索树中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-22
    • 2011-01-23
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多