C语言考试题大神求代码啊!!!!用随机数生成器列出所有扑克牌的名字

2024-11-20 05:07:33
推荐回答(1个)
回答1:

#include
#include
#include
#include
struct cards{
int suit;
int face;
};
char face[13][10]={" Ace","Deuce","Three"," Four"," Five"," Six","Seven","Eight"," Nine"," Ten"," Jack","Queen"," King"};
char suit[4][10]={"hearts ", "clubs ", "spades ", "diamonds"};

void shuffer(struct cards card[])
{
int i, j, a[52], b[52];
srand((int)time(0));
for(i=0; i<52; i++)
a[i] = i;
for(i=0; i<52; i++)
{
j = rand()%52;
if(a[j]!=-1)
{
b[i] = a[j];
a[j] = -1;
}
else
{
while(a[j]==-1)
j=++j%52;
b[i] = a[j];

}

}
for(i=0; i<52; i++)
{
card[i].face = b[i]%13;
card[i].suit = b[i]/13;
}

}
void deal(struct cards card[])
{
int i, j, k;
k = 1;
for(k=0, i=0; i<13; i++)
{
for(j=0; j<4; j++)
{
// printf("%d %d %d\n", k, card[k].suit, card[k++].face);
printf("%s of %s ", face[card[k].face], suit[card[k++].suit]);
}
printf("\n");
}
}
int main()
{
struct cards card[52];
shuffer(card);
deal(card);
}