【问题标题】:How to shuffle a deck of struct cards without using an array in c如何在不使用 c 中的数组的情况下洗牌一副结构卡片
【发布时间】:2022-01-15 01:10:24
【问题描述】:

我有这个代码。我在函数 createDeck 中创建了 10 张卡片,我想在函数 MyDeckOutput 中洗牌而不使用数组。有人可以帮忙吗??

我不知道,我的老师想要这样。我不允许使用 c++ 或外部的东西。 :/

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

typedef struct Card
{
    char name[50];
    int maxAlter;
    float maxGewicht;
    double maxLaenge;
    struct Card* pNext;
} cards;

cards* createDeck()
{
    cards* pStart = NULL;
    cards* pLast = NULL;
    cards* pNew = (cards*)malloc(sizeof(cards));
    for (int iElm = 0; iElm < 10; iElm++) {
        cards* pNew = (cards*)malloc(sizeof(cards));
        if (iElm == 0) { strcpy_s(pNew->name, "Ameisenbaer"); pNew->maxAlter = 14; pNew->maxGewicht = 39; pNew->maxLaenge = 0.90; pNew->pNext = NULL; }
        if (iElm == 1) { strcpy_s(pNew->name, "Biber"); pNew->maxAlter = 21; pNew->maxGewicht = 30; pNew->maxLaenge = 1.02; pNew->pNext = NULL; }
        if (iElm == 2) { strcpy_s(pNew->name, "Brauenbaer"); pNew->maxAlter = 30; pNew->maxGewicht = 600; pNew->maxLaenge = 1.50; pNew->pNext = NULL; }
        if (iElm == 3) { strcpy_s(pNew->name, "Delfin"); pNew->maxAlter = 45; pNew->maxGewicht = 150; pNew->maxLaenge = 7.00; pNew->pNext = NULL; }
        if (iElm == 4) { strcpy_s(pNew->name, "Elefant"); pNew->maxAlter = 70; pNew->maxGewicht = 6000; pNew->maxLaenge = 3.00; pNew->pNext = NULL; }
        if (iElm == 5) { strcpy_s(pNew->name, "Esel"); pNew->maxAlter = 14; pNew->maxGewicht = 39; pNew->maxLaenge = 0.90; pNew->pNext = NULL; }
        if (iElm == 6) { strcpy_s(pNew->name, "Federmaus"); pNew->maxAlter = 21; pNew->maxGewicht = 30; pNew->maxLaenge = 1.02; pNew->pNext = NULL; }
        if (iElm == 7) { strcpy_s(pNew->name, "Fuchs"); pNew->maxAlter = 30; pNew->maxGewicht = 600; pNew->maxLaenge = 1.50; pNew->pNext = NULL; }
        if (iElm == 8) { strcpy_s(pNew->name, "Gorilla"); pNew->maxAlter = 45; pNew->maxGewicht = 150; pNew->maxLaenge = 7.00; pNew->pNext = NULL; }
        if (iElm == 9) { strcpy_s(pNew->name, "Giraffe"); pNew->maxAlter = 70; pNew->maxGewicht = 6000; pNew->maxLaenge = 3.20; pNew->pNext = NULL; }
        pNew->pNext = NULL;
        if (pStart == NULL) pStart = pNew;
        if (pLast != NULL) pLast->pNext = pNew;
        pLast = pNew;
    }
    return pStart;
}

/*void MyDeckOutput(cards* pStart)
{
    int iEle = 0;
    for (cards* pOut = pStart; pOut != NULL; pOut = pOut->pNext)
    {
        iEle++;
        if (iEle < 6) printf("name = %s\n", pOut->name);
    }
}*/

void MyDeckOutput(cards* pStart)
{
    for (cards* pOut = pStart; pOut != NULL; pOut = pOut->pNext) printf("name = %s\n", pOut->name);
}

void shuffleDeck(cards* pStart)
{
    cards* shuffled = NULL;
    cards* end = NULL;
    int numberOfCards = 10; // cards number
    srand(time(NULL)); // seeds the random nr generator with the current
    while (numberOfCards > 0)
    {
        int index = rand() % numberOfCards;
        cards* previousCard = NULL;
        cards* selectedCard = pStart->pNext;
        // iterate over linked list
        if (!shuffled)
            end = shuffled = selectedCard;
        else
            end->pNext = selectedCard;

        if (previousCard)
            previousCard->pNext = selectedCard->pNext;
        end->pNext = NULL;
        --numberOfCards;

        printf("name = %s%i\n", selectedCard->name, index);
    }
}

int main()
{
    cards* pStart = createDeck();
    MyDeckOutput(pStart);
    printf("\n\nShuffel:\n");
    shuffleDeck(pStart);

    system("pause");
    return 0;
};

【问题讨论】:

  • 您的老师希望您了解链表。随机选择一张卡片。将其从原始链表中删除。将其添加到混洗链表中。重复直到原始链表为空。在main 你应该有cards* pShuffled = shuffleDeck(pStart);
  • 循环中每个if 后面都缺少{ },因此if 只会影响strcpy_s 而不会影响每一行的其余部分。 (您似乎在稍后的MyDeckOutput 中使用了与for 循环类似的语法——它之所以有效,是因为循环主体中只有一个语句。请使用{} 和缩进而不是这个。)
  • 注释'迭代链表'不应该意味着'这就是这条线所做的',而是应该提示你在这个位置插入它。你知道,以支持学习过程。

标签: c function pointers struct typedef


【解决方案1】:

评论者@user3386109 很赞同他的建议。

为了帮助您入门,这里有一个小代码 sn-p 来完成您需要做的事情。

void shuffleDeck(cards *deck)
{
    cards *shuffled = NULL;
    cards *end = NULL;
    int numberOfCards = countDeck(deck); //Count number of cards in your deck
    srand(time(NULL));  //Seeds the random number generator with the current 
    while (numberOfCards > 0)
    {
        int index = rand()%numberOfCards;
        cards *previousCard = NULL;
        cards *selectedCard;
        //Iterate over linked list until you arrive at index 'x'.
        if (!shuffled)
            end = shuffled = selectedCard;
        else
            end->next = selectedCard;

        if (previousCard)
            previousCard->next = selectedCard->next;
        end->next = NULL;
        --numberOfCards;
    }
}

请注意,此代码故意不完整,只是为了让您了解如何操作。

编辑:我真的不应该这样做,但这个随机播放代码对我有用。

cards *shuffleDeck(cards *deck)
{
    cards *shuffled = NULL;
    cards *end = NULL;
    int numberOfCards = countDeck(deck); //Count number of cards in your deck
    srand(time(NULL));  //Seeds the random number generator with the current
    while (numberOfCards > 0)
    {
        int index = rand()%numberOfCards;
        cards *previousCard = NULL;
        cards *selectedCard = deck;
    
        //Iterate over linked list until you arrive at index 'x'.
        for (int i = 0; i < index && selectedCard; ++i)
        {
            previousCard = selectedCard;
            selectedCard = selectedCard->pNext;
        }
    
        if (!shuffled)
            shuffled = selectedCard;
        else if (end)
            end->pNext = selectedCard;
        end = selectedCard;
    
        if (selectedCard)
        {
            if (previousCard)
                previousCard->pNext = selectedCard->pNext;  //Closes the gap between the previous element and the next element.
            else
                deck = selectedCard->pNext;                 //Resets the anchor element.
            selectedCard->pNext = NULL;
        }
        --numberOfCards;
    }
    
    return shuffled;
}

int main()
{
    MyDeckOutput(shuffleDeck(createDeck()));
    return 0;
};

我会留给你填补空白(如何计算卡片的数量)。

还请注意,您的“创建卡片”有问题。 要么在你的作业周围使用花括号 ({}),要么更好地使用专用函数。

另外,我建议阅读“switch/case”语句。

【讨论】:

  • 我怎样才能将我的牌组放入参数“牌组”?
  • 您只需将调用createDeck 时获得的指针传递给它。函数参数不需要共享它们所传递的变量的名称。
  • 玩了 3 个小时后我不明白它并尝试了所有东西? 我只了解单独的点。我不能带它来输出洗牌的牌组。你能帮助我吗? ?
  • 问题是:我们还没有学会怎么做,我是一个 web 开发者而不是 c 开发者?
  • @GhiathSardast 我在我的解决方案中添加了更多细节。现在我已经为你完成了大约 80% 的工作。我相信你能应付剩下的 20 个?
猜你喜欢
  • 2017-04-27
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 2013-06-06
  • 1970-01-01
相关资源
最近更新 更多