【问题标题】:c++ math game hex to decimal issuec ++数学游戏十六进制到十进制问题
【发布时间】:2014-05-10 07:01:14
【问题描述】:

我已经成功完成了我正在开发的这款数学游戏的前两种模式。我虽然在十六进制到小数部分的问题上苦苦挣扎。随机并不像我希望的那样随机。我希望十六进制代码可以在不同的位置找到更多随机字母:而不是在同一个地方只有一个字母。此外,我希望将字符输出量保持在 7 以内。

有人可以向我解释我的代码中的缺陷,给我一个工作示例,或一些正确方向的指导吗?

请帮我stackoverflow,

你是我唯一的希望!

这是到目前为止的游戏图片,

以下是我遇到问题的代码部分。它对类或任何东西都不感兴趣,只是一个简单的函数原型,因为我对 c++ 和编程还很陌生。

void gamethree()
{

    float secs;
    secs = 33;
    clock_t delay = secs * CLOCKS_PER_SEC;              
    clock_t start = clock();
    int correct = 0;                                    

while (clock() - start < delay )
{
    srand(time(NULL));

    int hexdigits[15] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    char hexletters[7] = {'a', 'b', 'c', 'd', 'e', 'f'};
    //
    //enum letters{

    //  hexa = 10, 
    //  hexb = 11, 
    //  hexc = 12, 
    //  hexd = 13, 
    //  hexe = 14, 
    //  hexf = 15
    //};
    //hexa == hexletters[0];
    //hexb == hexletters[1];
    //hexc == hexletters[2];
    //hexd == hexletters[3];
    //hexe == hexletters[4];
    //hexf == hexletters[5];

    int randindex = rand() % 14;
    int randindexx = rand() % 6;
    cout << hexdigits[randindex];
    cout << hexletters[randindexx];


    int hexf[4] = {
        hexdigits[randindexx], 
        hexdigits[randindex], 
        hexletters[randindexx], 
        hexdigits[randindex],
    };


    random_shuffle(begin(hexf), end(hexf));

    for(int hi = 0; hi < 4; ++hi)
        cout << hexf[hi];
        char hexy = 0;
    cin >> hexy;

    //int hexy = 0;
    //cin >> hexy;



    ////int c = (a * b);
    //int d = 0;
    //char choice = 0;

    //cout <<"What does " << a << " * " << b << " equal?" << endl << endl << endl;
    //cout << "\n";

    //do
    //{
    //  cout << "Please enter a number: ";
    //  cin >> d;       
    //  if(d == c)
    //       ++correct;
    //}while(d != c);

    //cout << "\n\nCorrect! " << (a * b) << " is the answer!" << endl << endl;  
}
    cout << "Your score is: " << correct << "\n\n" <<endl;
cout << "Would you like to play again (Y) or (N)?\n\n\n";
}

void hextodec()
{
        float secs;
        secs = 1;
        clock_t delay = secs * CLOCKS_PER_SEC;              
        clock_t start = clock();
        while (clock() - start < delay )
            ;


        char choice = 'z';
        gamethree();
        while(choice != 'n')
        {

        cin >> choice;

        if (choice == 'y')
        {
            cout << "\n\n";
            game();
        }
        else
            choice = 'n';

    }


}

【问题讨论】:

  • 您通常应该在程序开始时调用一次srand,而不是在生成随机数的函数内部。

标签: c++ math random hex decimal


【解决方案1】:

如果这是我的问题,我会做的是有一个包含所有可用字符的数组,然后有一个函数可以从中生成一个最大长度的字符串,就像这样。

std::string rand_string(const char* possible, int options, int max_length){
    std::string result; //we're using strings just so we don't have to pass a char*
    int length = rand()%max_length+1; //You may want to try to biase this towards bigger values or just set it at some value

    for (int i=0; i<length; ++i) {
        result[i]=possible[rand()%options]; //simple really
    }

    return result;
}

基本上只是将所有字符放在一个数组中并循环遍历。

几个旁注。为什么您的 hexdigits 数组会上升到 15。15 不是十六进制的有效字符,原因很简单,它无法与 1,5 区分开来。还有这是为了什么。我不想帮助你在作业中作弊,虽然这还没有经过测试,而且很可能显然是抄袭,如果你只是在整个销售中使用它,我不太担心。

【讨论】:

  • 我在任何机构之外自学 C++。我编写了整个程序,这只是我正在努力的项目的一部分。
猜你喜欢
  • 2011-09-22
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 2021-08-30
  • 2018-07-26
  • 2014-04-18
相关资源
最近更新 更多