我如何为结构数组实现堆栈?

时间:2014-03-23 20:48:53

标签: c loops pointers struct stack

我在Ubuntu 10.04中使用GCC创建了一个小应用程序。我有一个头文件和一个源文件。

我的标题文件:

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

#define DECKSZ 52
#define HAND_SIZE 5

#define STACKMAX 52
#define EMPTY -1
#define FULL (STACKMAX-1)

typedef enum boolean {false, true} boolean;

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;

typedef struct stack {
    card s[STACKMAX];
    int top;
} stack;

extern card deck[];

void initDeck(card[]);
void labelCards(card[]);
void shuffleDeck(card[]);
boolean dealHand(card[], stack*);
void displayHand(card*);
void arrangeHand(card*);
void swap(card*, card*);
boolean isFlush(card[]);
boolean isStraight(card[]);
boolean isXOfAKind(card[], int, enum pip);
boolean isStraightFlush(card[]);
boolean isFullHouse(card[]);
boolean isTwoPair(card[]);
boolean isEmpty(stack*);
boolean isFull(stack*);
void push(card*, stack*);
card pop(stack*);
void reset(stack*);

我的源文件:

#include "Poker.h"

int main(void) {

    int i;
    int flushCount = 0;
    int straightCount = 0;
    int xOfAKindCount = 0;
    int straightFlushCount = 0;
    int fullHouseCount = 0;
    int isTwoPairCount = 0;

    stack *stkDeck = stack;
    stack *stkHand = stack;

    card deck[DECKSZ] = {0};

    initDeck(deck);
    labelCards(deck);

    for (i = 0; i < DECKSZ; i++) {
        push(&deck[i], stkDeck);
    }

    /*do {*/

        flushCount = 0;
        straightCount = 0;
        xOfAKindCount = 0;
        straightFlushCount = 0;
        fullHouseCount = 0;
        isTwoPairCount = 0;

        shuffleDeck(deck);

        displayHand(deck);
        arrangeHand(&deck[0]);

        flushCount = isFlush(&deck[0]);
        straightCount = isStraight(&deck[0]);
        xOfAKindCount = isXOfAKind(&deck[0], 2, 0);
        straightFlushCount = isStraightFlush(&deck[0]);
        fullHouseCount = isFullHouse(&deck[0]);
        isTwoPairCount = isTwoPair(&deck[0]);

        printf("Flush Count: %d\n", flushCount);
        printf("Straight Count: %d\n", straightCount);
        printf("X Of A Kind Count: %d\n", xOfAKindCount);
        printf("Straight Flush Count: %d\n", straightFlushCount);
        printf("Full House Count: %d\n", fullHouseCount);
        printf("Two Pair Count: %d\n", isTwoPairCount);

    /*} while (1);*/

    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[]) {
    const char *pipNames[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
    const char *suitNames[] = {" of Spades"," of Hearts"," of Diamonds"," of Clubs"};
    int i, tmpPip = 0, tmpSuit = 0;
    for (i = 0; i < DECKSZ; i++) {
        tmpPip = (deck[i].pips) - 1;
        tmpSuit = (deck[i].suits);
        strcpy(deck[i].cardName, pipNames[tmpPip]);
        strcat(deck[i].cardName, suitNames[tmpSuit]);
    }
}

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

boolean dealHand(card deck[], stack *stkHand) {
    boolean successfulDeal = ((boolean) (0));
    int i;
    for (i = 0; i < HAND_SIZE; i++) {
        push(&deck[i], stkHand);
    }
    return successfulDeal;
}

void displayHand(card hand[]) {
    int i;
    for (i = 0; i < HAND_SIZE; i++) {
        printf("%s\n", 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;
}

boolean isFlush(card hand[]) {
    int i, count = 0, result = 0;
    for (i = 0; i < HAND_SIZE-1; i++) {
        if (hand[i].suits != hand[i+1].suits) {
            count++;
        }
    }
    if (count == HAND_SIZE)
        result = 1;
    return ((boolean) (result));
}

boolean isStraight(card hand[]) {
    int i, count = 0, result = 0;
    for (i = 0; i < HAND_SIZE - 1; i++) {
        if (hand[i].pips == (hand[i+1].pips + 1)) {
            count++;
        }
    }
    if (count == HAND_SIZE)
        result = 1;
    return ((boolean) (result));
}

boolean isXOfAKind(card hand[], int x, enum pip pipsIgnored) {
    int i, count = 0, result = 0;
    for (i = 0; i < HAND_SIZE - 1; i++) {
        if (hand[i].pips == hand[i+1].pips) {
            if (hand[i].pips != pipsIgnored) {
                count++;
            }
        }
    }
    if (count == (x - 1))
        result = 1;
    return count;
}

boolean isStraightFlush(card hand[]) {
    int result = 0;
    result = isFlush(hand);
    result = isStraight(hand);
    return ((boolean) (result));
}

boolean isFullHouse(card hand[]) {
    int result = 0;
    result = isXOfAKind(hand, 3, 0);
    result = isXOfAKind(hand, 2, 0);
    return ((boolean) (result));
}

boolean isTwoPair(card hand[]) {
    int result = 0;
    result = isXOfAKind(hand, 2, hand->pips);
    result = isXOfAKind(hand, 2, hand->pips);
    return ((boolean) (result));
}

boolean isEmpty(stack *stk) {
    return ((boolean) (stk->top = EMPTY));
}

boolean isFull(stack *stk) {
    return ((boolean) (stk->top == FULL));
}

void push(card *c, stack *stk) {
    stk->top++;
    stk->s[stk -> top] = *c;
}

card pop(stack *stk) {
    return (stk->s[stk->top--]);
}

void reset(stack *stk) {
    stk->top = EMPTY;
}

我的问题与main()中的deck []数组有关。我想把它作为一个堆栈实现,这样当一只手“被绘制”时,每只手都会从堆栈中移出5个卡结构。一旦牌组缺纸,我希望它创建一个新洗牌的牌并继续交易牌(意味着筹码将重新填充到52张牌并继续允许牌从牌组中弹出)。

是否有人能够帮我实现我的卡结构阵列(我的“套牌”)的堆栈?谢谢!

2 个答案:

答案 0 :(得分:1)

您要求使用数组实现堆栈。我发现了一个使用谷歌搜索的实现(并没有说它有任何好处)。这是PUSH和POP部分。 “主要”中的东西只是形式上的东西。您应该能够操纵此代码来执行您想要的操作。

    /*STACK PUSH() AND POP() IMPLEMENTATION USING ARRAYS*/
    #include <stdio.h>
    #define MAX 52
    int top, status;

    /*PUSH FUNCTION*/
    void push (int stack[], int item)
    {   if (top == (MAX-1))
        status = 0;
        else
        {   status = 1;
            ++top;
            stack [top] = item;
        }
    }

    /*POP FUNCTION*/
    int pop (int stack[])
    {
        int ret;
        if (top == -1)
        {   ret = 0;
            status = 0;
        }
        else
        {   status = 1;
            ret = stack [top];
            --top;
        }
        return ret;
    }

    /*MAIN PROGRAM*/
    void main()
    {
        int stack [MAX], item;
        top = -1;

        push (stack, item);
        item = pop (stack);
    }

答案 1 :(得分:0)

这里有几个问题:如何表示卡片堆栈(一种简单的方法是使用数组,因为元素的数量是有界的和小的;有一个变量来告诉剩下多少张卡片)。另一个问题是获得洗牌,最简单的解决方案是Knuth shuffle。请注意,如果您将卡取出5,最后将剩下两张卡,您将不得不决定如何处理这些卡(每次只刷洗所有52张卡,或者只刷洗50张卡) )。