洗牌程序,invalid conversion from char* to char,125行
就这一个报错了,125行#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//**************************************************************************
//实现一个洗牌程序。所谓洗牌程序,就是将所有的扑克牌放入一个数组中,
//要求把数组内的扑克牌顺序随机打乱,然后输出
//每个人获得14张牌 ,我分牌的思路是牌在数组里面随机生成0~54的不重复的54个数,规定自然数0~12为 {"方","梅","红","黑","大王","小王"}里面的方,
//13-26w为梅有,依次类推,最后剩余的52,53两个数分别对应大小王,对i循环从1-14,依次对应纸牌里面的1-13,然后成对输出
int number_of_game = 3;
int card = 13;
//int print_card(int i,char name);
//int distribution(int array,int member);
//char **get_name(int num);
int print_card(int i,char name,int people,char class_card)
{
printf("**************%d*************\n",i);
printf("the card of %s is:\n", name);
for (int i = 0; i < card; i++)
{
if(people < card - 1)//小于13
{
printf("%s%d ",class_card,i); //0~14给第一个字符
}
else if(card *2 > people >= card)
{
printf("%s%d ",class_card,i);
}
else if(card *3> people >= card *2)
{
printf("%s%d ",class_card,i);
}
else if(52 > people >= 39)
{
printf("%s%d ",class_card,i);
}
else if(people == 53)
{
printf("%s ",class_card);
}
else
{
printf("%s ",class_card);
}
}
}
char **get_name(int num)
{
char **ptr = NULL;
int i;
ptr = (char **)malloc(num *sizeof(char*));//*name;这种需要用到动态存储
for(i = 0;i <num;i++)
{
ptr = (char*)malloc(100*sizeof(char));
printf("enter %d member's name :",i);
fgets(ptr,100,stdin);
// printf("the member's name is: %s'",ptr);
}
return ptr;
}
int *distribution(int *array,int member)
{
static int someone;
for(int i =0; i<card; i++)
{
someone = array;
}
return someone;
}
int main()
{
//输入玩家的名子
int i;
char **name = NULL;
name= get_name(number_of_game);
for(i = 0; i < number_of_game; i++)
{
printf("the %d member's name is: %s'",i,name);
}
//定义纸牌
//char *num = {'J','Q','K'};
char num = {'1','2','3','4','5','6','7','8','9','10','J','Q','K'};
char *class_card = {"方","梅","红","黑","大王","小王"};
int people;
srand(time(NULL));
// printf("fang = %s\n",class_card);
//产生54乱序的数,0-30,+大小王 = 42,42/3 = 14
for (int i = 0; i < 54; i++)
{
people = rand()%54;
}
printf("\n");
//分配54张牌
int *person1,*person2,*person3;
for(int i =0;i<54;i++)
{
people = rand() % 54;
}
person1 = distribution(people,1);
person2 = distribution(people,2);
person3 = distribution(people,3);
// for(int i =0;i<14;i++)
// {
// printf("%d ",person1);
// }
//打印3个人分到的牌
print_card(1,name,person1,class_card);
print_card(2,name,person2,class_card);
print_card(3,name,person3,class_card);
free(name);
free(person1);
free(person2);
free(person3);
return 0;
}
本帖最后由 jackz007 于 2020-11-25 16:05 编辑
你的实力和这个代码的复杂程度相比,实在是不匹配,代码问题成堆,只是帮你修改到可以正常编译。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//**************************************************************************
//实现一个洗牌程序。所谓洗牌程序,就是将所有的扑克牌放入一个数组中,
//要求把数组内的扑克牌顺序随机打乱,然后输出
//每个人获得14张牌 ,我分牌的思路是牌在数组里面随机生成0~54的不重复的54个数,规定自然数0~12为 {"方","梅","红","黑","大王","小王"}里面的方,
//13-26w为梅有,依次类推,最后剩余的52,53两个数分别对应大小王,对i循环从1-14,依次对应纸牌里面的1-13,然后成对输出
int number_of_game = 3;
int card = 13;
//int print_card(int i,char name);
//int distribution(int array,int member);
//char **get_name(int num);
void print_card(int i , char name[] ,int people,char class_card[])
{
printf("**************%d*************\n",i);
printf("the card of %s is:\n", name);
for (int i = 0; i < card; i++)
{
if(people < card - 1)//小于13
{
printf("%s%d ",class_card,i); //0~14给第一个字符
}
else if(card *2 > people >= card)
{
printf("%s%d ",class_card,i);
}
else if(card *3> people >= card *2)
{
printf("%s%d ",class_card,i);
}
else if(52 > people >= 39)
{
printf("%s%d ",class_card,i);
}
else if(people == 53)
{
printf("%s ",class_card);
}
else
{
printf("%s ",class_card);
}
}
}
void get_name(char name[] , int num)
{
int i ;
for(i = 0 ; i < number_of_game ; i ++)
{
printf("enter %d member's name :",i) ;
fgets(name , 64 , stdin) ;
}
}
int *distribution(int *array,int member)
{
static int someone;
for(int i =0; i<card; i++)
{
someone = array;
}
return someone;
}
int main()
{
//输入玩家的名子
int i;
char name ;
get_name(name , number_of_game) ;
for(i = 0; i < number_of_game; i++)
{
printf("the %d member's name is: %s'",i,name);
}
//定义纸牌
//char *num = {'J','Q','K'};
char num = {"1","2","3","4","5","6","7","8","9","10","J","Q","K"} ;
char class_card[] = {"方","梅","红","黑","大王","小王"};
int people;
srand(time(NULL));
// printf("fang = %s\n",class_card);
//产生54乱序的数,0-30,+大小王 = 42,42/3 = 14
for (int i = 0; i < 54; i++)
{
people = rand()%54;
}
printf("\n");
//分配54张牌
int *person1,*person2,*person3;
for(int i =0;i<54;i++)
{
people = rand() % 54;
}
person1 = distribution(people,1);
person2 = distribution(people,2);
person3 = distribution(people,3);
// for(int i =0;i<14;i++)
// {
// printf("%d ",person1);
// }
//打印3个人分到的牌
print_card(1,name,person1,class_card);
print_card(2,name,person2,class_card);
print_card(3,name,person3,class_card);
free(person1);
free(person2);
free(person3);
return 0;
}
页:
[1]