程设小白向各路大神问好,想请教大家这个洗牌程序,为啥outputCard这个函数只要一出现就returnvalue333244125
/*HW13_2. 洗牌程序. 用一个结构数组保存52张牌, 每张牌包含花色和面值. 首先按照花色和面值初始化结构数组(正如我们买到的新牌), 然后利用随机数进行洗牌. 输出洗牌结果.
要求如下:
1) 保存花色的指针数组,
char *suits[] = {"Spades", "Hearts", "Diammonds", "Clubs"};
保存面值的指针数组,
char *faces[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
2)定义初始化纸牌的函数initCards(), 按照花色和面值初始化纸牌结构数组.
3)定义输出纸牌的函数outputCards(), 输出52张纸牌的花色和面值.
4)定义洗牌函数shuffle(), 洗牌算法: 循环考察结构数组中的每个元素,
用随机数函数生成一个52以内的随机数, 将当前考察元素与下标为随机数的元素交换.
HW13_2.JPG*/
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
struct Card{
char *suits;
char *faces;
}card;
void shuffle(Card cards[][13],int n,int m)
{
srand(time(0));
Card card[52];
Card tempCard;
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
card[i*13+j]=cards[i][j];
}
}
for(i=0;i<52;i++)
{
int temp=rand()%(52-i)+i;
tempCard=card[i];
card[i]=card[temp];
card[temp]=tempCard;
}
for(i=0;i<4;i++)
{
for(j=0;j<13;j++)
{
cards[i][j]=card[i*13+j];
}
}
}
void outputCards(Card yifupai[52])
{
int i, j, k;
for(i=k=0;i<9;i++)
{
for(j=0;j<6;j++,k++)
printf("%-10s%-5s|",yifupai[k].suits,yifupai[k].faces);
printf("\n");
}
}
int main()
{
char suits[4][10] = {"Spades", "Hearts", "Diammonds", "Clubs"};
char faces[][10] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
Card cards[4][13];
int i,j, k=0;
Card pai[52];
for(i=0;i<=3;i++)
{
for(j=0;j<=12;j++)
{
cards[i][j].suits=suits[i];
cards[i][j].faces=faces[j];
}
}
for(i=0;i<4;i++)
for(j=0;j<13;j++)
{
pai[k].suits=cards[i][j].suits;
pai[k].faces=cards[i][j].faces;
k++;
}
printf(" ");
printf("/*Shuffling Card Program*/\n\n");
printf("The initial cards are:\n");
for(i=k=0;i<9;i++)
{
for(j=0;j<6;j++,k++)
printf("%-10s%-5s|",pai[k].suits,pai[k].faces);
printf("\n");
}
// outputCards(pai);
// shuffle(cards,4,13);
/* outputCards(pai);
for(i=k=0;i<4;i++)
for(j=0;j<13;j++)
{
pai[k].suits=cards[i][j].suits;
pai[k].faces=cards[i][j].faces;
k++;
}*/
printf("The shuffled cards are:\n");
}