30 鱼币
问题:.h文件的第48行总是报 Segmentation fault 错误
代码清单:
.c文件如下:
#include "S1E31T0.h"
#define MAX 1024
int main()
{
int i;
char name1[MAX], name2[MAX], name3[MAX];
int played_cards[54] = {0};//初始化已消耗牌牌组
int* playing_cards1;
int* playing_cards2;
int* playing_cards3;
printf("请输入第一个参与打牌的人的名字:");
fgets(name1, MAX, stdin);
printf("请输入第二个参与打牌的人的名字:");
fgets(name2, MAX, stdin);
printf("请输入第三个参与打牌的人的名字:");
fgets(name3, MAX, stdin);
//发牌
playing_cards1 = deal_cards(played_cards, 0);
for(i = 0; i < 18; i++)
{
played_cards[i] = playing_cards1[i];
}
playing_cards2 = deal_cards(played_cards, 1);
for(i = 19; i < 37; i++)
{
played_cards[i] = playing_cards1[i];
}
playing_cards3 = deal_cards(played_cards, 2);
//打印每个人手上的牌
printf("%s手里的牌为:\n", name1);
printf_cards(playing_cards1);
printf("\n");
printf("%s手里的牌为:\n", name2);
printf_cards(playing_cards2);
printf("\n");
printf("%s手里的牌为:\n", name3);
printf_cards(playing_cards3);
printf("\n");
return 0;
}
.h文件如下:
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
//#define playing_cards[54] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54}
//其中1-13表示黑桃,14-26表示红桃,27-39表示梅花,40-52表示方片,51表示小王(joker),52表示大王(JOKER)
int random_num(int num);//用于生成随机数的函数
int* deal_cards(int sum, int played_cards[54]);//用于发牌的函数
int judge(int num, int played_cards[13]);//判断num是否是之前已经随机出来的数
void printf_cards(int played_cards[18]);//用来输出每个人手牌
//int sum = 0;
int random_num(int num)
{
int result;
time_t t;//使用time函数返回标准计时点到当前时间的秒数用于作为初始化伪随机数的种子序列
srand((unsigned int)time(&t));//使用当前时间值作为初始化伪随机数种子序列
result = rand() % num + 1;//rand() % (n-m+1)+m ---> 产生m到n之间的随机数
}
int judge(int num, int played_cards[18])
{
int i;
for(i = 0; i < 18; i++)
{
if(num == played_cards[i])
{
return 1;
}
}
return 0;
}
int* deal_cards(int sum, int played_cards[54])
{
int i, j,num, len;
static int playing_cards[18];
int played_card[54];
len = 19;
for(j = 0; j < 54; j++)
{
played_card[i] = played_cards[i];
}
for(i = 0 + len * sum; i < (18 + len * sum); i++)
{
while(judge(num, played_card) == 1)
{
num = random_num(54);
}
played_card[i] = num;
playing_cards[i - len * sum] = num;
sum++;
}
return playing_cards;
}
void printf_cards(int played_cards[18])
{
int i;
for(i = 0; i < 18; i++)
{
if(played_cards[i] > 0 && played_cards[i] < 14)
{
if(played_cards[i] == 11)
{
printf("黑桃J ");
}
else if(played_cards[i] == 12)
{
printf("黑桃Q ");
}
else if(played_cards[i] == 13)
{
printf("黑桃K ");
}
else
{
printf("黑桃%d ", played_cards[i]);
}
}
else if(played_cards[i] > 13 && played_cards[i] < 27)
{
if(played_cards[i] - 13 == 11)
{
printf("红桃J ");
}
else if(played_cards[i] - 13 == 12)
{
printf("红桃Q ");
}
else if(played_cards[i] - 13 == 13)
{
printf("红桃K ");
}
else
{
printf("红桃%d ", played_cards[i]);
}
}
else if(played_cards[i] > 26 && played_cards[i] < 40)
{
if(played_cards[i] - 26 == 11)
{
printf("梅花J ");
}
else if(played_cards[i] - 26 == 12)
{
printf("梅花Q ");
}
else if(played_cards[i] - 26 == 13)
{
printf("梅花K ");
}
else
{
printf("梅花%d ", played_cards[i]);
}
}
else
{
if(played_cards[i] - 39 == 11)
{
printf("方片J ");
}
else if(played_cards[i] - 39 == 12)
{
printf("方片Q ");
}
else if(played_cards[i] - 39 == 13)
{
printf("方片K ");
}
else
{
printf("方片%d ", played_cards[i]);
}
}
}
}
我来回答