C 如何为结构数组实现堆栈?
我在Ubuntu 10.04中使用GCC创建了一个小应用程序。我有一个头文件和一个源文件 我的头文件:C 如何为结构数组实现堆栈?,c,loops,pointers,struct,stack,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 bool
#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*);
#包括
#包括
#包括
#定义SZ 52
#定义手部尺寸5
#定义STACKMAX 52
#定义空-1
#定义完整(STACKMAX-1)
typedef枚举布尔值{false,true}布尔值;
typedef结构卡{
Enm PIP {ACE=1、2、三、四、五、六、七、八、九、十、杰克、皇后、King } PIP;
enum套装{黑桃、梅花、红桃、钻石}套装;
charcardname[20];
}卡片;
typedef结构堆栈{
卡s[STACKMAX];
int top;
}堆叠;
外部卡组[];
无效(卡片[]);
作废标签卡(卡片[]);
无效洗牌(卡片[]);
布尔数据处理(卡片[],堆栈*);
空手(卡片*);
作废安排(卡片*);
无效掉期(卡*,卡*);
布尔isFlush(card[]);
布尔isStraight(卡[]);
布尔ISXOFAGIND(卡片[],整数,枚举pip);
布尔isStraightFlush(卡[]);
布尔值为fullhouse(卡片[]);
布尔对(卡片[]);
布尔等空(堆栈*);
布尔值为满(堆栈*);
无效推送(卡片*,堆栈*);
卡片弹出(堆栈*);
无效重置(堆栈*);
我的源文件:
#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;
}
#包括“Poker.h”
内部主(空){
int i;
int flushCount=0;
整数计数=0;
int xOfAKindCount=0;
int flushcount=0;
int fullhousecont=0;
int isTwoPairCount=0;
stack*stkDeck=stack;
堆栈*stkHand=堆栈;
卡片组[DECKSZ]={0};
甲板(甲板);
标签卡(甲板);
对于(i=0;i=0;i--){
对于(j=0;j点数>(手牌+j+1)->点数)
互换(手牌+j,手牌+j+1);
}
}
}
无效掉期(卡*c1,卡*c2){
卡片温度;
温度=*c1;
*c1=*c2;
*c2=温度;
}
布尔isFlush(牌手[]){
int i,计数=0,结果=0;
对于(i=0;ipips);
结果=isXOfAKind(手,2,手->pips);
返回((布尔值)(结果));
}
布尔isEmpty(堆栈*stk){
返回((布尔值)(stk->top=EMPTY));
}
布尔值已满(堆栈*stk){
返回((布尔值)(stk->top==FULL));
}
无效推送(卡*c,堆栈*stk){
stk->top++;
stk->s[stk->top]=*c;
}
卡片弹出(堆栈*stk){
返回(stk->s[stk->top--]);
}
无效重置(堆栈*stk){
stk->top=空;
}
我的问题与main()中的deck[]数组有关。我想将其实现为一个堆栈,这样当一只手被激活时,每只手都会作为5个卡结构从堆栈中退出
/*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);
}