学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:函数返回指定学号的学生数据,指定的学号在主函数中输入。若没找到指定学号,在结构体变量中给学号置空串,给成绩置-1,作为函数值返回。(用于字符串比较的函数是strcmp, strcmp(a, b)当a和b字符串相等时返回值为0)。
#include <stdio.h>
#include <string.h>
#define N 5
struct student
{
char id[20];
int grade;
};
struct student fun(struct student *s, const char *query)
{
struct student result = {"", -1};
int i = 0;
for (i; i < N; i++)
{
if (strcmp(s[i].id, query) == 0)
{
result = s[i];
break;
}
}
return result;
}
int main()
{
struct student s[N] = {"1", 60, "2", 70, "3", 80, "4", 90, "5", 100};
char query[20];
scanf("%s", query);
struct student res = fun(s, query);
printf("%s %d", res.id, res.grade);
}