gpt4 book ai didi

c - 如何使用结构体中包含的枚举作为函数的参数?

转载 作者:行者123 更新时间:2023-12-01 16:04:26 25 4
gpt4 key购买 nike

我有以下功能:

int isXOfAKind(card *hand, int x, enum pips pip) {
//... do something
}

我希望第三个参数是以下结构中的点:

typedef struct card {
enum {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} pips;
enum {SPADES, CLUBS, HEARTS, DIAMONDS} suit;
char cardName[20];
} card;

我的头文件:

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

#define DECKSZ 52
#define HAND_SIZE 5

typedef struct card {
enum pip {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} pips;
enum suit {SPADES, CLUBS, HEARTS, DIAMONDS} suits;
char cardName[20];
} card;

extern card deck[];

void initDeck(card[]);
void labelCards(card[]);
void shuffleDeck(card[]);
void displayHand(card*);
void arrangeHand(card*);
void swap(card*, card*);
int isFlush(card*);
int isStraight(card*);
int isXOfAKind(card*, int, enum pips);
int isStraightFlush(card*);
int isFullHouse(card*);
int isTwoPair(card*);

我的源文件:

#include "Poker.h"

int main(void) {
card deck[DECKSZ];
card *pDeck = &deck[0];
initDeck(deck);
labelCards(deck);
shuffleDeck(deck);
displayHand(deck);
return EXIT_SUCCESS;
}

void initDeck(card deck[]) {
int counter;
for (counter = 0; counter < DECKSZ; counter++) {
deck[counter].pips = (const)((counter % 13) + 1);
deck[counter].suits = (const)(counter / 13);
}
}

void labelCards(card deck[]) {
static const char *pipNames[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
static const char *suitNames[] = {"Spades","Hearts","Diamonds","Clubs"};
int i;
for (i = 0; i < DECKSZ; i++) {
sprintf(deck[i].cardName, "%s of %s\n", pipNames[i % 13], suitNames[i / 13]);
}
}

void shuffleDeck(card deck[]) {
int i, j;
for (i = 0; i < DECKSZ; i++) {
j = rand() % DECKSZ;
swap(&deck[i], &deck[j]);
}
}

void displayHand(card hand[]) {
int i;
for (i = 0; i < HAND_SIZE; i++) {
printf("%s", hand[i].cardName);
}
}

void arrangeHand(card *hand) {
int i, j;
for (i = HAND_SIZE-1; i >= 0; i--) {
for (j = 0; j < i; j++) {
if ((hand+j)->pips > (hand+j+1)->pips)
swap(hand+j, hand+j+1);
}
}
}

void swap(card *c1, card *c2) {
card temp;
temp = *c1;
*c1 = *c2;
*c2 = temp;
}

int isFlush(card *hand) {
int i, result = 0;
for (i = 0; i < HAND_SIZE - 1; i++) {
if ((hand+i)->suits != (hand+i+1)->suits) {
result = 1;
break;
}
}
return result;
}

int isStraight(card *hand) {
int i, result = 0;
for (i = 0; i < HAND_SIZE - 1; i++) {
if ((hand+i)->pips != (hand+i+1)->pips + 1) {
result = 1;
break;
}
}
return result;
}

int isXOfAKind(card *hand, int x, enum pips pip) {
int result = 0;
return result;
}

int isStraightFlush(card *hand) {
int result = 0;
result += isFlush(hand);
result += isStraight(hand);
return result;
}

int isFullHouse(card *hand) {
int result = 0;
result += isXOfAKind(hand, 3, hand->pips);
result += isXOfAKind(hand, 2, hand->pips);
return result;
}

int isTwoPair(card *hand) {
int result = 0;
result += isXOfAKind(hand, 2, hand->pips);
result += isXOfAKind(hand, 2, hand->pips);
return result;
}

如何使用点枚举作为我的函数的参数?我在 Ubuntu 中使用 GCC,并使用 ANSI C 进行编码。谢谢!

最佳答案

更新

您根据最初的答案正确地修复了代码,但您只是有一个拼写错误,修复后将为您提供干净的代码:

int isXOfAKind(card*, int, enum pips);
^^^^

应该是:

int isXOfAKind(card*, int, enum pip);
^^^

这里也是:

int isXOfAKind(card *hand, int x, enum pips pip) {
^^^^^^^^

应该是:

int isXOfAKind(card *hand, int x, enum pip pips) {
^^^^^^^^

原始答案

问题是 pips 是一个变量,而不是这里的类型:

enum {ACE=1, TWO, ... } pips;
^^^^

这将创建一个点类型:

enum pips {ACE=1, TWO, } myPip;
^^^^

请参阅以下示例 live :

#include <stdio.h>

typedef struct card {
enum pips {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} myPip;
enum {SPADES, CLUBS, HEARTS, DIAMONDS} suit;
char cardName[20];
} card;

void test( enum pips pip) {
printf( "pip = %d\n", pip ) ;
}

int main()
{
test( TWO ) ;
test( KING ) ;
}

如果我们转到draft C99 standard我们在 6.7.2.3 Tags 部分看到 6 说(强调我的):

type specifier of the form

struct-or-union identifieropt { struct-declaration-list }

or

 enum identifieropt { enumerator-list }

or

  enum identifieropt { enumerator-list , }

declares a structure, union, or enumerated type. The list defines the structure content, union content, or enumeration content. If an identifier is provided,130) the type specifier also declares the identifier to be the tag of that type.

其中脚注130说:

If there is no identifier, the type can, within the translation unit, only be referred to by the declaration of which it is a part. Of course, when the declaration is of a typedef name, subsequent declarations can make use of that typedef name to declare objects having the specified structure, union, or enumerated type.

关于c - 如何使用结构体中包含的枚举作为函数的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22572208/

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