6-6 结构体数组中查找指定编号人员 (10分)

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 <stdio.h>
#include <string.h>
#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;
	 }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 函数fun的实现可以参考以下代码: ``` #include <stdio.h> #include <string.h> #define MAX_N 100 struct Person { char id[20]; int year, month, day; }; struct Person std[MAX_N]; struct Person find_person(char* id, int n) { for (int i = ; i < n; i++) { if (strcmp(std[i].id, id) == ) { return std[i]; } } struct Person empty_person = {"", , , }; return empty_person; } int main() { // 假设已经从文件或键盘读入了n个人员的数据,存入std数组 int n = 10; for (int i = ; i < n; i++) { sprintf(std[i].id, "ID%03d", i+1); std[i].year = 199 + i % 10; std[i].month = 1 + i % 12; std[i].day = 1 + i % 28; } // 在主函数调用find_person函数查找指定编号人员数据 char id[20]; printf("请输入要查找人员编号:"); scanf("%s", id); struct Person p = find_person(id, n); if (strlen(p.id) == ) { printf("编号为%s的人员不存在\n", id); } else { printf("编号为%s的人员信息如下:\n", id); printf("出生日期:%d年%d月%d日\n", p.year, p.month, p.day); } return ; } ``` 在上述代码,我们定义了一个结构Person,用于存储每个人员编号和出生日期。然后,我们在主函数模拟了一些人员数据,并调用了find_person函数来查找指定编号人员数据。如果找到了对应的人员数据,就将其返回;否则,返回一个空的Person结构,其编号为空串。最后,在主函数根据返回值输出相应的信息。 ### 回答2: 这道题要求我们写一个函数,根据给定的编号查找某个人员的数据。函数的输入是一个结构数组和一个所需查找编号,输出是该编号对应的人员数据。如果该编号不存在,则输出一个空字符串。 我们可以通过遍历结构数组找到对应编号人员数据,找到后将其返回即可。具代码如下: ```c #include <stdio.h> #include <string.h> // 定义结构,包括编号、出生年、月、日 struct Person { char id[10]; int year; int month; int day; }; // 查找函数,输入结构数组和所需查找编号,输出对应的人员数据 struct Person find_person(struct Person std[], int n, char id[]) { int i; for (i = 0; i < n; i++) { if (strcmp(std[i].id, id) == 0) { // 判断编号是否相等 return std[i]; // 返回对应的人员数据 } } struct Person empty = {"", 0, 0, 0}; // 定义一个空结构返回空字符串 return empty; } int main() { struct Person std[3] = { // 定义结构数组 {"001", 1998, 1, 1}, {"002", 1999, 2, 2}, {"003", 2000, 3, 3} }; struct Person p1 = find_person(std, 3, "002"); // 查找编号为"002"的人员数据 if (strcmp(p1.id, "") == 0) { // 判断是否为空字符串 printf("该编号不存在\n"); } else { // 输出对应的人员数据 printf("编号:%s,出生日期:%d-%02d-%02d\n", p1.id, p1.year, p1.month, p1.day); } struct Person p2 = find_person(std, 3, "004"); // 查找编号为"004"的人员数据 if (strcmp(p2.id, "") == 0) { // 判断是否为空字符串 printf("该编号不存在\n"); } else { // 输出对应的人员数据 printf("编号:%s,出生日期:%d-%02d-%02d\n", p2.id, p2.year, p2.month, p2.day); } return 0; } ``` 以上代码,我们先定义了一个结构,包括编号、出生年、月、日四个属性,然后在主函数定义了一个结构数组,用于存储人员数据。接着,在`find_person`函数,我们使用了一个`for`循环来遍历结构数组,判断输入的编号是否等于结构数组编号。如果相等,则返回对应的人员数据;如果不相等,则返回一个空结构,表示该编号不存在。在主函数,我们分别调用`find_person`函数查找编号为"002"和"004"的人员数据,并输出对应的结果。 最后需要注意的是,如果在主函数直接使用`strcmp`函数来判断返回的结构是否为空结构,会出现一个“段错误”(segmentation fault)的错误,因为返回的空结构并没有初始化,其的字符串id并没有被赋值为空字符串。为了解决这个问题,我们需要在函数先定义一个空结构,并将其的字符串id赋值为空字符串。 ### 回答3: 本题是一个涉及结构数组的问题。结构数组是一种将多个不同数据类型的数据打包存储的一种方式,其可视化表示如下: ``` struct Person { char id[20]; int birth_year; int birth_month; int birth_day; }; Person std[N]; // 结构数组 ``` 其,`Person`被定义为一个结构,包含了编号、出生年、月、日等成员变量。我们需要在 `fun` 函数实现指定编号人员查找功能,并将其作为函数返回值返回。 题目要求函数返回的是一个结构类型的值,因此我们可以先定义一个空的 `Person` 类型的数据变量,用于保存查找结果。在查找过程,我们需要遍历结构数组 `std`,比较每个人员编号是否和给定编号相同。如果找到了与给定编号相同的人员,我们就可以将该人员的全部信息(包括编号、出生年、月、日)赋值给结果变量,并将其返回。 如果遍历了所有人员还没有找到目标编号,我们就需要根据题目要求返回一个空串。这可以通过将结果变量的编号成员变量置为空串(即赋值为 `""`)来实现。 完整的 `fun` 函数实现如下: ``` Person fun(char* target_id, Person* std, int n) { // 定义结果变量 Person result = {"", 0, 0, 0}; // 遍历结构数组,比较编号 for (int i = 0; i < n; i++) { if (strcmp(target_id, std[i].id) == 0) { // 找到目标编号 result = std[i]; // 将该人员信息赋值给结果变量 break; } } return result; } ``` 在主函数调用 `fun` 函数,并输出结果,代码如下: ``` int main() { // 假设给定的人员编号为"001",std为预定义的结构数组,n为其长度 char target_id[20] = "001"; Person result = fun(target_id, std, n); if (strcmp(result.id, "") == 0) { // 返回的编号为空串,说明未找到目标编号 printf("未找到该编号人员!"); } else { // 找到了目标编号,输出该人员信息 printf("编号:%s,出生日期:%d年%d月%d日\n", result.id, result.birth_year, result.birth_month, result.birth_day); } return 0; } ``` 以上代码实现了结构数组指定编号人员查找功能,并根据题目要求输出了对应信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值