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

时间:2014-03-22 01:21:03

标签: c struct enums typedef

我有以下功能:

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

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

1 个答案:

答案 0 :(得分:2)

更新

您根据初始答案对代码进行了正确的修改,但是您只需要一个拼写错误,一旦修复就会给您提供干净的代码:

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

如果我们转到6.7.2.3部分struct-or-union identifieropt { struct-declaration-list } 标签段落 6 所说的draft C99 standard强调我的 ):

  

表单的类型说明符

 enum identifieropt { enumerator-list }
     

  enum identifieropt { enumerator-list , }
     

130
     

声明结构,联合或枚举类型。该列表定义了结构内容,   联合内容或枚举内容。如果提供了标识符, 130),则类型说明符也将标识符声明为该类型的标记。

脚注{{1}}说:

  

如果没有标识符,则翻译单元中的类型只能通过声明属于的声明来引用。当然,当声明是typedef名称时,后续声明可以使用该typedef名称来声明具有指定结构,联合或枚举类型的对象。