bubbleSort扑克牌

时间:2018-10-18 02:37:59

标签: c sorting

我正在为我的项目对我的扑克牌进行冒泡排序,但是我被卡住了,并且不能正常工作。基本上,卡片是价值和适合的枚举。一手是7张牌的阵列。当我运行此代码时,输​​出根本没有排序,它仍然按main中声明的原始顺序进行排序。如何实现bubbleSort或如何打印它有问题吗?任何帮助表示赞赏。

#include <stdio.h>
#include "poker.h"

typedef enum suit{C,D,H,S} suit;
typedef enum value{TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE} value;

typedef struct card {
    value cValue; 
    suit cSuit; 
} card;

typedef struct Hand {
    card cards[7];
} Hand; 

void swap(card * a, card * b)
{
    card t = *a;
    *a = *b;
    *b = t; 
}

void bubbleSort(Hand h, int n)
{
    int i,j; 
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-i-1;j++)
        {
            if(h.cards[j].cValue > h.cards[j+1].cValue)
            {
                swap(&h.cards[j], &h.cards[j+1]); //changed from h.cards[j].cValue                      
            }
        }
    }
}

void printHand(Hand h, int size
{
    for(int i=0; i<size; i++)
    {
        printf("%i ", h.cards[i].cValue);
    } printf("\n");
}


int main()
{
card c0,c1,c2,c3,c4,c5,c6;

c0.cValue = TEN;
c1.cValue = EIGHT;
c2.cValue = QUEEN;
c3.cValue = FOUR;
c4.cValue = FIVE;
c5.cValue = SEVEN;
c6.cValue = TWO;

Hand h; 
h.cards[0]= c0;
h.cards[1]=c1;
h.cards[2]=c2;
h.cards[3]=c3;
h.cards[4]=c4;
h.cards[5]=c5;
h.cards[6]=c6;

bubbleSort(h,7);
printHand(h,7);
return 0; 


}

0 个答案:

没有答案