随机化链表中的节点,C程序

时间:2016-05-29 18:13:46

标签: c list random

以下功能是带有问题和答案的游戏的一部分。我对问题随机化的问题有一个问题 - 一些问题出现不止一次,第一个也出现在每个级别,这不应该发生。请帮忙!

typedef struct
{
    char question[300];
    char date[30], author[30], ansr1[80], ansr2[80], ansr3[80], ansr4[80];
    int id, correctAnsr, level;
} game;

typedef struct Node
{
    game data;
    struct Node* next;
}  node;

void printRandom1(node *head)
{
    if (isEmpty(head))
        return;

    srand(time(NULL));

    node *result = head->data.question;

    node *current = head;
    int n;
    for (n = 17; current != NULL; n++)
    {
        if (rand() % n == 0 && current->data.level == 0)
            result = current->data.question;
        current = current->next;
    }
    printf("%s\n", result);
    int i, ans;

    printf("1.-%s\n", result->data.ansr1);
    printf("2.-%s\n", result->data.ansr2);
    printf("3.-%s\n", result->data.ansr3);
    printf("4.-%s\n", result->data.ansr4);

    printf("Enter the correct answer: ");
    scanf("%d", &ans);
    if (ans == result->data.correctAnsr)
        printf("CORRECT ANSWER!\n");
    else
    {
        printf("WRONG ANSWER\n");
        printf("GAME OVER");
        printf("The correct answer is: %d\n", result->data.correctAnsr);
        return menu();
        exit(1);
    }
}

1 个答案:

答案 0 :(得分:1)

广告“某些问题不止一次出现”:这是因为您无论如何都不会跟踪使用的问题 - 您的随机选择方法始终从所有问题的列表中选择(无论他们是否已经有人问过。)

广告“首先也出现在每个级别”:我的赌注是你的随机选择方法(这很奇怪)不能保证选择一个问题(即result = current->data.question部分可能不会以很高的概率执行)。在这种情况下保留result的初始值(这恰好是第一个问题)。

以下是您的代码的修改版本。一些评论:

  • 添加链接列表中的问题数量。随机选择需要选择具有相同概率的答案(正确 - 存在一些可忽略的偏差,但在这里可能不重要)

  • 在新的链接列表中跟踪使用的答案

  • 没有实现级别逻辑。您可能希望从游戏开头的链接列表中删除不适当级别的问题

  • current变量是一个指向指针的指针,它简化了取消链接过程(你不需要以这种方式保留前一个入口指针)

代码:

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

typedef struct
    {
    char question[300];
    char date[30], author[30], ansr1[80], ansr2[80], ansr3[80], ansr4[80];
    int id, correctAnsr, level;
} question;

typedef struct questionListNode {
    question data;
    struct questionListNode* next;
} questionListNode;

typedef struct {
    questionListNode* headAvailable; // Your former head variable
    questionListNode* headUsed; // Initialize this to NULL
    int numberOfAvailableNodes; // Number of nodes in headAvailable
    int numberOfCorrectAnswers; // Number of nodes in headUsed
} game;

void printRandom1(game* currentGame)
{
    if (currentGame->headAvailable == NULL || currentGame->numberOfAvailableNodes <= 0) {
        printf("No more questions, you've won!\n");
        exit(1);
    }

    srand(time(NULL)); // Consider moving this to the start of main()

    int chosenIndex = rand() % currentGame->numberOfAvailableNodes;
    questionListNode** current = &(currentGame->headAvailable);
    while ((chosenIndex > 0) && (*current != NULL)) { // the second check is for safety
        current = &((*current)->next);
        chosenIndex--;
    }

    questionListNode* currentQuestion = (*current);
    if (currentQuestion == NULL) {
        printf("Internal error: available count mismatch!\n");
        exit(1);
    }
    printf("%s\n", currentQuestion->data.question);
    int i, ans;

    printf("1.-%s\n", currentQuestion->data.ansr1);
    printf("2.-%s\n", currentQuestion->data.ansr2);
    printf("3.-%s\n", currentQuestion->data.ansr3);
    printf("4.-%s\n", currentQuestion->data.ansr4);

    printf("Enter the correct answer: ");
    scanf("%d", &ans);
    if (ans != currentQuestion->data.correctAnsr) {
        printf("WRONG ANSWER\n");
        printf("GAME OVER\n");
        printf("The correct answer is: %d\n", currentQuestion->data.correctAnsr);
        exit(1);
    }
    printf("CORRECT ANSWER!\n");
    // Remove currentQuestion from the available list
    (*current) = currentQuestion->next;
    // Put currentQuestion into used list
    currentQuestion->next = currentGame->headUsed;
    currentGame->headUsed = currentQuestion;
    // Update counters
    currentGame->numberOfAvailableNodes--;
    currentGame->numberOfCorrectAnswers++;
}

int main(int c, char** t)
{
    game g;
    g.headAvailable = NULL;
    g.headUsed = NULL;
    g.numberOfAvailableNodes = 0;
    g.numberOfCorrectAnswers = 0;

    questionListNode q1 = { { "Question 1", "", "", "A1*", "B1", "C1", "D1", 1, 1, 0 }, NULL };
    questionListNode q2 = { { "Question 2", "", "", "A2", "B2*", "C2", "D2", 2, 2, 0 }, &q1 };
    questionListNode q3 = { { "Question 3", "", "", "A3", "B3*", "C3", "D3", 3, 2, 0 }, &q2 };

    g.headAvailable = &q3;
    g.numberOfAvailableNodes = 3;

    while (1)
        printRandom1(&g);
}

一些额外的(随机)笔记:

  • 我不确定link-list是此任务的最佳数据结构

  • 考虑使用一些前缀命名您的typedef(例如t_gamet_node

  • 如果您想重新开始游戏(而不是exit()),则需要将两个链接列表重新加入并重置计数器

祝你好运!

免责声明:我没有花太多时间查看代码,所以请以此为例......