gpt4 book ai didi

c - 尝试从另一个包含结构的函数调用一个函数

转载 作者:行者123 更新时间:2023-11-30 18:21:53 26 4
gpt4 key购买 nike

我遇到了一个大问题,我试图构建一些代码,从另一个带有结构的函数中调用我的函数,但是我不知道我是否混淆了自己,但我现在很迷茫,所以我希望得到一些帮助。基本上我想做的是生成一个 1 到 4 之间的随机数,从那里我做了一些 if 语句,并在 if 语句中我调用了我的函数。然而,它的效果并不那么好。最后一段代码是错误的。函数“randomize()”

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

#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 颗红心(或所选颜色)。我不知道如何解决这个问题,我根本没有解决方案。希望在这里得到一些帮助并解决我的问题。仅供引用,我不想使用您可能会看到的任何全局变量。

最佳答案

我看到的问题:

  1. 您正在将临时 Card 的地址从 randomize 传递给函数 diamondsspades 等>.
  2. 临时卡尚未初始化。
  3. 传递给其他函数的指针不是一副Card。它只是一张

解决问题的建议。

  1. 将变量 deckmain 传递到 randomize
  2. 将相同的变量从 randomize 传递给其他函数。

先更改函数原型(prototype):

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);
}
}

关于c - 尝试从另一个包含结构的函数调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com