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

标签 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 张牌,并继续允许从牌堆中弹出牌)。

有人可以帮助我为我的卡片结构数组(我的“牌组”)实现一个堆栈吗?谢谢!

最佳答案

您要求使用数组实现堆栈。我找到了一个使用 Google 搜索的实现(并不是说它有什么好处)。这是 PUSH 和 POP 部分。 “main”中的内容只是形式上的内容。您应该能够操纵此代码来执行您想要的操作。

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

关于c - 我将如何为结构数组实现堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596874/

相关文章:

循环内的 JavaScript 闭包——简单实用的例子

用户输入的Python暂停循环

比较 C 中的字符会引发错误

memory - 指针在内存中是如何存储的?

c++ - 如何使用密码(C/C++)锁定Windows 7计算机屏幕

c++ - 对通过静态库调用的同一 .c 文件中定义的函数的 undefined reference

使用命令行参数在 C 程序中创建多个文件

c - 如何在您的 makefile 中链接静态 glibc

php - Seeder 第一次运行良好,但在 Laravel 5 的后续循环中不执行某些任务?

visual-c++ - Visual C++ 非托管和托管