我有一个下面的函数:
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;
My header file:
#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 %sn", 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;
}
如何使用pips枚举作为函数的参数?我在Ubuntu中使用GCC,在ANSI c中编码。谢谢!
update
你根据最初的答案正确地修复了你的代码,但你只是有一个错别字,一旦你修复了,就会给你干净的代码:
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;
^^^^
这将创建一个类型pip:
enum pips {ACE=1, TWO, } myPip;
^^^^
查看下面的示例:
#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 = %dn", pip ) ;
}
int main()
{
test( TWO ) ;
test( KING ) ;
}
如果我们去看C99标准草案我们看到6.7.2.3
部分Tags段落6说(强调mine):
形式
的类型说明符struct-or-union identifieropt { struct-declaration-list }
或
enum identifieropt { enumerator-list }
或
enum identifieropt { enumerator-list , }
声明结构、联合或枚举类型。列表定义结构内容,联合内容或枚举内容。如果提供了标识符,130)类型说明符也声明该标识符为该类型的标记。
脚注130
说:
如果没有标识符,则该类型在翻译单元内只能由其所属的声明引用。当然,当声明的类型是一个pedef名称时,后续声明可以使用该类型名称来声明具有指定结构、联合或枚举类型的对象。