6-6 结构体数组中查找指定编号人员 (10分)
人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号唯一。 函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数输出,若制定编号不存在,返回数据中的编号为空串。
太坑了
错误1 如果找不到对应编号,直接给字符数组赋值
字符数组不像string类一样能直接赋一个字符串。要想实现,必须用strcpy函数
错误2 空串是“000000”,而不是NULL
注意点:
1 函数返回的是struct student类型,可以是在函数中新创建的变量 haha 也可以是std[i]
2 函数传入的是*std,故是struct student类型的数组的首地址
3 std中的字符数组串和 (*num)比大小 必须用strcmp,不能像string那样直接比较
#include
#include
#define N 8
struct student
{ char num[10];
int year,month,day ;
};
struct student fun(struct student *std, char *num);
int main()
{
struct student std[N]={ {"111111",1984,2,15},{"222222",1983,9,21},{"333333",1984,9,1},{"444444",1983,7,15},{"555555",1984,9,28},{"666666",1983,11,15},{"777777",1983,6,22},{"888888",1984,8,19}};
struct student p;
char n[10]="666666";
p=fun(std,n);
if(p.num[0]==0)
printf("Not found !\n");
else
{ printf("Succeed !\n ");
printf("%s %d-%d-%d\n",p.num,p.year,p.month,p.day);
}
return 0;
}
struct student fun(struct student *std, char *num)
{
struct student haha;
int flag=0,i;
for(i=0;i<N;i++)
{
if(strcmp(std[i].num,num)==0)
{
flag=1;
return std[i];
}
}
if(flag==0)
{
strcpy(haha.num,"000000");
return haha;
}
}