我有一个巨大的问题,我试图建立一些代码,我从另一个函数调用我的函数与结构在其中,但我不知道我是否已经混淆了自己,但我现在很丢失所以我会感谢som的帮助.基本上我试图做的是生成1到4之间的随机数,从那里我做了一些if语句,在if语句中我调用了我的函数.但是,它不能很好地工作. 它是最后一段错误的代码.功能"randomize()"
#include#include #include #define DIAMONDS 0 #define CLUBS 1 #define HEARTS 2 #define SPADES 3 #define JACK 11 #define QUEEN 12 #define KING 13 #define ACE 1 #define COLOR_SIZE 13 #define NR_OF_SUITS 4 #define DECK_SIZE 52 int random_card; struct Card { int suit; int value; }; void diamonds(struct Card *cardDeck); void spades(struct Card *cardDeck); void clubs(struct Card *cardDeck); void hearts(struct Card *cardDeck); void swapCards(struct Card *cardA, struct Card *cardB); void shuffleCards(struct Card *cardDeck); void randomize(); int main() { srand((unsigned)time(NULL)); struct Card * deck; //Dynamiskt allokerad array int index; int suit_index; deck = (struct Card *)malloc(sizeof(struct Card) * DECK_SIZE); for (suit_index = 0; suit_index < NR_OF_SUITS; suit_index++) /* Initiera kortleken */ for (index = 0; index < COLOR_SIZE; index++) { deck[suit_index*COLOR_SIZE + index].suit = suit_index; deck[suit_index*COLOR_SIZE + index].value = index; } shuffleCards(deck); randomize(); printf("\n\n"); system("pause"); return 0; } void swapCards(struct Card * cardA, struct Card *cardB) { struct Card temp; temp = *cardA; *cardA = *cardB; *cardB = temp; } void shuffleCards(struct Card *cardDeck) { for (int i = 0; i < COLOR_SIZE; i++) swapCards(&cardDeck[i], &cardDeck[rand() % 13]); } void diamonds(struct Card *cardDeck) { for (int i = 0; i < 1; i++) { switch (cardDeck[i].value + 1) { case ACE: printf("Ace "); break; case JACK: printf("Jack "); break; case QUEEN: printf("Queen"); break; case KING: printf("King "); break; default: printf("%d ", cardDeck[i].value + 1); break; } printf("of Diamonds"); printf("\n"); } } void spades(struct Card *cardDeck) { for (int i = 0; i < 1; i++) { switch (cardDeck[i].value + 1) { case ACE: printf("Ace "); break; case JACK: printf("Jack "); break; case QUEEN: printf("Queen"); break; case KING: printf("King "); break; default: printf("%d ", cardDeck[i].value + 1); break; } printf("of spades"); printf("\n"); } } void hearts(struct Card *cardDeck) { for (int i = 0; i < 1; i++) { switch (cardDeck[i].value + 1) { case ACE: printf("Ace "); break; case JACK: printf("Jack "); break; case QUEEN: printf("Queen"); break; case KING: printf("King "); break; default: printf("%d ", cardDeck[i].value + 1); break; } printf("of hearts"); printf("\n"); } } void clubs(struct Card *cardDeck) { for (int i = 0; i < 1; i++) { switch (cardDeck[i].value + 1) { case ACE: printf("Ace "); break; case JACK: printf("Jack "); break; case QUEEN: printf("Queen"); break; case KING: printf("King "); break; default: printf("%d ", cardDeck[i].value + 1); break; } printf("of clubs"); printf("\n"); } } void randomize() { random_card = rand() % 4 + 1; if (random_card == 1) { struct Card c1; hearts(&c1); } if (random_card == 2) { struct Card c2; diamonds(&c2); } if (random_card == 3) { struct Card c3; spades(&c3); } if (random_card == 4) { struct Card c4; clubs(&c4); } }
我得到的输出是心中的-939213(或所选颜色).我不知道如何解决这个问题,我根本没有解决方案.我会在这里得到一些帮助,并解决我的问题.而且仅供参考,我不想使用你可能看到的任何全局变量.
我看到的问题:
你是通过一个临时的地址Card
的功能diamonds
,spades
等从randomize
.
临时卡尚未初始化.
传递给其他函数的指针不是Card
s 的套牌.它只是一个单一的Card
.
建议解决问题.
传递变量deck
从main
到randomize
.
将相同的变量传递给其他函数randomize
.
首先更改函数原型:
void randomize(struct Card* deck);
更改来电main
.
shuffleCards(deck); randomize(deck); printf("\n\n");
改变实施.
void randomize(struct Card* deck) { random_card = rand() % 4 + 1; if (random_card == 1) { hearts(deck); } if (random_card == 2) { diamonds(deck); } if (random_card == 3) { spades(deck); } if (random_card == 4) { clubs(deck); } }