我有這個代碼。我在函式中創建了 10 張卡片,createDeck我想在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;
};
uj5u.com熱心網友回復:
評論員@user3386109 很贊同他的建議。
為了幫助您入門,這里有一個小代碼片段來完成您需要做的事情。
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;
}
}
請注意,此代碼故意不完整,只是為了讓您了解如何執行此操作。
編輯:我真的不應該這樣做,但是這個 shuffle 代碼對我有用。
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”陳述句的內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/384337.html
下一篇:在字典中存盤值
