在测验中随机化问题并在C中计算正确的百分比

时间:2015-04-09 01:02:29

标签: c random

我正在制作一个简单的C程序,以进行多项选择测验,帮助我的学生准备考试 我使用了struct函数。到目前为止,程序运行良好

我的问题:

  1. 有没有办法使用下面的当前代码随机生成问题?

  2. 您如何建议正确跟踪百分比? (计数器+循环?while循环?

  3. 3)我做得非常低效,你能推荐一个解决方案吗? 我是一个真正的初学者,所以请不要复杂的解释。谢谢

    以下是代码:

    #include <stdio.h>
    
    
    struct question
    {
    char quiz[130]; // Question will be stored here
    char answer1[20];  // Text for multiple choice possible answer 1
    char answer2[20];  // Text for multiple choice possible answer 2 ..
    char answer3[20];
    char answer4[20];
    int correctAnswer;
    };
    
    
    
    int main (){
    
    
        struct question one={
               "Que significa la palabra 'ser'\n",
               "1. to do",
               "2. to be",
               "3. to make",
               "4. to understand",
               2,
    
        };
        printf ("%s %s %s %s %s\n",one.quiz,one.answer1,one.answer2,one.answer3,one.answer4);
        scanf ("%d",&one.correctAnswer);
        if (one.correctAnswer!=2){
                             printf("Equivocado! 'ser' significa: to be\n");
                             }
        else                 {
                             printf ("Correcto\n");
                             }
        struct question two={
               "Que significa la palabra 'poder'\n",
               "1. to do",
               "2. to read",
               "3. to make",
               "4. to be able to",
               4,
    
        };
        printf ("%s %s %s %s %s\n",two.quiz,two.answer1,two.answer2,two.answer3,two.answer4);
        scanf ("%d",&two.correctAnswer);
        if (two.correctAnswer!=4){
                             printf("Equivocado! 'ser' significa: to be able to\n");
                             }
        else                 {
                             printf ("Correcto\n");
                             }
    

4 个答案:

答案 0 :(得分:1)

您可以创建包含问题和答案以及正确答案的每个结构的数组。随机选择数组中的索引并打印其内容。如果答案是正确的,只需增加correct_answers++;之类的变量并跟踪提出的问题。

计算百分比然后变为(float)correct_answers/questions_asked *100

手动将问题和答案写入C源是一种非常低效(且速度慢)的数据编码方式。我猜测从文件中读取文件目前太先进了。但是你应该仔细研究它。

在此期间,请尝试以下模板:

struct question
{
char quiz[130]; // Question will be stored here
char answer1[20];  // Text for multiple choice possible answer 1
char answer2[20];  // Text for multiple choice possible answer 2 ..
char answer3[20];
char answer4[20];
int correctAnswer;
};

struct question Questions[] = {
{
 "Example questions",
 "Option 1",
 "other options",
...
 /* Correct Answer */ 4
},
{
 "Example question 2",
 "Option 2_1",
 "other options",
...
 /* Correct Answer */ 2
},
};

答案 1 :(得分:1)

1)是的,这完全可能而且很容易!

2)正确,您可以循环查看所有问题,并保留所有正确问题的计数器,然后计算平均值。

3)您可能希望将所有数据设置在一个位置,然后使用某种通用方式让代码运行所有问题。你可以像我在下面做的那样做。

另外,请注意,由于您以随机顺序提出问题,因此可以多次提出相同的问题。我添加了一个循环来继续尝试选择随机问题,直到你得到一个独特的问题。

有多种方法可以解决这个问题,一种方法是采用那一组问题并在循环之前将它们随机化。以下是蛮力的简单

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define arrsize(a) (sizeof(a) / sizeof(a[0]));

struct Question
{
    char quiz[130]; // Question will be stored here
    char answer1[20];  // Text for multiple choice possible answer 1
    char answer2[20];  // Text for multiple choice possible answer 2 ..
    char answer3[20];
    char answer4[20];
    int  correctAnswer;
    bool used;
};


static Question s_questions[] = {
    {
        "Que significa la palabra 'ser'\n",
        "1. to do",
        "2. to be",
        "3. to make",
        "4. to understand",
        2,
        false
    },
    {
        "Que significa la palabra 'poder'\n",
        "1. to do",
        "2. to read",
        "3. to make",
        "4. to be able to",
        4,
        false
    }

    // Add more questions below this

};

int main () {

    unsigned totalQuestions = arrsize(s_questions);
    unsigned correct = 0;

    // Set random seed
    srand((unsigned)time(0));

    for (unsigned i = 0; i < totalQuestions; ++i) {

        // Loop over questions until you get a new one
        unsigned curr = rand() % totalQuestions;
        while (s_questions[curr].used == true) {
            curr = rand() % totalQuestions;
        }
        s_questions[curr].used = true;

        printf ("%s %s %s %s %s\n", s_questions[curr].quiz, s_questions[curr].answer1,s_questions[curr].answer2,s_questions[curr].answer3,s_questions[curr].answer4);
        unsigned answer = 0;
        scanf ("%d",&answer);

        if (s_questions[curr].correctAnswer != answer){
            printf("Equivocado!");
        }
        else {
            printf ("Correcto\n");
            correct++;
        }

    }

    float percent = ((float)correct / (float)totalQuestions) * 100.0f;
    printf ("Your average score was: %f\n", percent);

    return 0;
}

答案 2 :(得分:0)

1)是的,绝对可以。有许多不同的方法可以做到这一点。这是一个。创建一个“struct question”数组,并用你的问题数据填充它,生成一个随机索引,检查索引问题是否已被使用,如果是,则继续生成随机索引,如果没有显示问题并标记它已使用。请注意,这是一种刻意简单的算法,您可以(更高)更有效。

2)您只需要两个计数器:询问的问题数量和正确答案的数量。这与循环的需要无关(无论是否跟踪正确的答案百分比,都需要进行循环)。

3)恕我直言,最好不要在现阶段解决这个问题。首先解决您的基本算法和实现。特别是在这个Stackoverflow问题中,因为有太多东西无法一次性覆盖(特别是考虑到你的解决方案不断变化)。

答案 3 :(得分:0)

  

1)有没有办法使用下面的当前代码随机生成问题?

你没有随机地解释你的意思&#34;,无论你的意思是&#34;不可预测的&#34; (意思是一个问题可以反复出现,一次又一次地出现)或者#34;频率相对统一&#34; ......我假设你的意思是后者。虽然答案是&#34;是&#34;但两者中的后者可能更容易,因为计算机不能很好地提供不可预测的数据。

在程序开始时,您需要为伪随机生成器设定种子。将时间用作种子是相当常见的,例如:srand(time(NULL));

现在,您可以通过调用RAND_MAX来提取[0 .. rand()]范围内的伪随机数。假设您有一个名为struct question的{​​{1}}数组,可以使用模q运算符%将该数字减少到您的范围内。这会引入少量偏见,但这可能不是一个大问题。

如果是个问题,您可以丢弃任何大于或等于rand() % range倍数的数字:

range

我想你会在提出问题时减少int n; do { n = rand(); } while (RAND_MAX - n <= RAND_MAX % range); ,并将range提出的问题移到q之外,以避免再次询问他们。这会引出您的下一个问题:

  

2)您如何建议正确跟踪百分比? (计数器+循环?循环?

您可以将每个问题的range成员设置为correctAnswer,如果不正确,或0如果正确,请循环1以计算a s ,然后除以问题的总数...或者你可以保持正确答案的数量,因为你问他们。在我看来,你的程序需要的内存越少越好。如有必要,您可以稍后介绍该优化。

  

3)我做得非常低效,你能推荐一个解决方案吗?我真的是一个初学者,所以请不要复杂的解释。

如果有,那么当您编写完整个程序时,您将使用您的探查器找到它...但是,考虑到您可能会遇到少量问题(&lt; l&lt; l&lt; ; 1000)和最近运行它的计算机(&gt; y2k)这个程序应该没问题。