【问题标题】:C++ String excercise with random numbers带有随机数的 C++ 字符串练习
【发布时间】:2020-08-27 09:19:36
【问题描述】:

我必须做这个练习:

对学童的常见惩罚是多次写出同一个句子。编写一个 C++ 独立程序,将下面这句话写一百遍:“我将永远使用面向对象设计。”你的程序应该给每个句子编号,并且它应该“不小心”在列表中的不同位置出现八个不同的随机拼写错误,这样看起来就像是人类手动输入的一样。

我的知识仅限于 C 随机数。我试过这个没有成功。我不能得到8个错误。正如我们所看到的,我收到“错字”的随机错误。

这是我的错误代码:

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    string strPunish = "I will always use objectoriented design.";
    int randFrom = 1;
    int randTo = 100;
    int typoCounter = 0;
    srand(time(NULL));
    int randNumber = randFrom + ( std::rand() % ( randTo - randFrom + 1 ) );
    for (int i = 1; i <= 100; i++)
    {
        if ((i == randNumber) && (typoCounter != 8))
        {
            randFrom = i;
            randNumber = randFrom + ( std::rand() % ( randTo - randFrom + 1 ) );
            string strTypo = strPunish;
            int randTypo = 0 + ( std::rand() % ( strTypo.length() - 0 + 1 ) );
            strTypo.insert(randTypo, "TYPO");
            cout << i << ": " << strTypo << endl;
            typoCounter++;
        }   
        else
            cout << i << ": " << strPunish << endl;
    }
    return EXIT_SUCCESS;
}

【问题讨论】:

  • 建议:通过生成以最大行数为界的 8 个随机数,随机预先确定哪些行会因拼写错误而损坏。如果您随意选择行,您可能会将错误偏向列表的开头,或者在损坏所有 8 行之前用完行。
  • using namespace std; - 检查这个:stackoverflow.com/questions/1452721/…
  • 不再使用“使用命名空间”。
  • 我想知道。如果我们每场演出 100/4 = 25 行。然后我们随机选择这 25 行,然后是接下来的 25 行,依此类推……这样好吗?
  • 你要做的是在做任何事情之前随机选择8行。您的范围是 0-99,您可以从该范围中选择 8 个随机数。然后您进行打印,并在遇到预定的行时生成错字。

标签: c++ string random ctime


【解决方案1】:

如上所述,既然是你的作业,你应该自己解决。但我会给你一些关于可能改进你的代码的线索:

  1. 首先,您应该将代码分成小函数 - 这将使其更具可读性和更易于理解!
  2. 更容易创建一个包含[0, 99] 范围内所有数字的数组,将其打乱,然后只需选择第一行X 进行拆分(其中X 可以是任何东西!)。查看std::arraystd::shuffle 了解更多信息。
  3. 不要在代码中使用硬编码数字!您应该改为定义 constexpr 变量 - 它会使您的代码更具可读性(还记得 (1) 吗?)
  4. 额外:不再常用randsrand,取而代之的是使用&lt;random&gt; 中的std::mt19937。此外,使用&lt;chrono&gt; 而不是&lt;ctime&gt; 更为常见。

备注:请注意,您需要确保您选择要破坏的行号是唯一的;确保它(可能不会永远卡住)的最快方法是按照我在(2) 中的建议进行操作。

【讨论】:

    【解决方案2】:

    我已经为你制作了整个程序:

    #include <iostream>
    #include <ctime>
    
    void selectionSort(int[], int);
    
    int main(void)
    {
        std::string punishStr = "I will always use objectoriented design."; // declaration
        std::string tempStr = punishStr;
        std::string typoStr = "TYPO";
        int length = punishStr.length();
        int location = 0;
        short int count = 0;
        int randoms[8] = {0};
    
        srand(time(0)); // generates random different times
    
        for (int i = 0; i < 8; i++)
        {
            randoms[i] = (rand() % 100) + 1; // creates 8 random numbers
        }
    
        selectionSort(randoms, 8); // sorts random numbers, for sake of being unbiased
    
        for (int i = 0; i < 100; i++)
        {
            if (randoms[count] == i && count <= 8) // random number equals i
            {
                punishStr = tempStr;
                location = (rand() % length) + 1;
                std::cout << (i + 1) << ": " << punishStr.insert(location, typoStr) << std::endl;
                count++;
            }
            else
                std::cout << (i + 1) << ": " << tempStr << std::endl;
        }
    
        return 0;
    }
    
    void selectionSort(int a[], int n) // sorting algorithm
    {
        int i, j, min, temp;
    
        for (i = 0; i < n - 1; i++)
        {
            min = i;
            for (j = i + 1; j < n; j++)
                if (a[j] < a[min])
                    min = j;
    
            temp = a[i];
            a[i] = a[min];
            a[min] = temp;
        }
    }
    

    程序首先声明了一些必要的变量,包括所需的八个带零的随机数。然后,通过使用 For 循环,它重新定义了所有 8 个数组元素,范围为 1-100 之间的随机数。

    之后,它只是简单地对数组进行排序(否则它会出现偏差,例如 54、32...),为了防止它,我们只是将它排序为 32、54...。此后,它检查计数器变量是否

    它会一遍又一遍地重复,直到 i 小于 100。

    希望它对你有用。

    注意:输出这么大,最好自己检查。 :-)

    【讨论】:

    • 请不要这样做。嵌入式解释使这不会成为一个可怕的纯代码答案,但家庭作业需要更多的放手。如果你解决了整个问题,提问者就不会学到他们应该学到的东西。
    • 旁注:不需要滚动你自己的排序。除非写一个排序是作业,否则请引导提问者std::sort。还可以尝试将它们推向&lt;random&gt; 库的方向。 I find this link to be a helpful prod.
    • @user4581301 我对他的程序感到困惑。这就是为什么我重新编码而不是花很长时间。我已经在必要的地方发表了评论。
    • @user4581301 我已经为我的程序添加了解释。希望不再是可怕的纯代码答案。
    • 一开始就不是纯代码的。你已经解决了这个问题。我的抱怨是你做了太多的工作,剥夺了提问者的学习机会。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多