洗牌程序,25行报错,invalid types int[int] for array subscript
#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 =1; i < card+1; i++)
{
if (people < card-1)//小于13
{
printf("%s%d ",class_card,i); //0~14给第一个字符
}
else if(card *2-1 > people >= card)
{
printf("%s%d ",class_card,i);
}
else if(card *3 - 1 > 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 = {"方","梅","红","黑","大王","小王"};
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 people,*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;
}
int print_card(int i,char name,int people,char class_card)
{
. . . . . .
for (int i =1; i < card+1; i++)
{
if (people[ i ] < card-1)//小于13 jackz007 发表于 2020-11-24 21:08
int print_card(int i,char name,int people,char class_card)
{
. . . . . .
老师,这的报错还没报到超出范围呢,应该主要还是语法问题,还是报这句,invalid types int for array subscript 说的就是你把一个整型数当成数组用。别说了,对于编译器而言,这就是一个不可通融的语法错误 jackz007 发表于 2020-11-25 10:07
说的就是你把一个整型数当成数组用。别说了,对于编译器而言,这就是一个不可通融的语法错误
嗷嗷,明白了,我再改改,谢谢老师
页:
[1]