输入n(n<50)个学生的成绩信息,再输入一个学生的学号、课程以及成绩,在自定义函数update_score()中修改该学生指定课程的成绩。
函数接口定义:
int update_score(struct student *p, int n, int num, int course, int score);
其中p是结构指针,n是学生个数,course是课程序号,score是修改后的课程成绩。函数须返回指定学生的顺序位置,如果查无此人,返回-1。
裁判测试程序样例:
#include <stdio.h> struct student{ /*学生信息结构定义*/ int num; /* 学号 */ char name[10]; /* 姓名 */ int math, english, computer; /* 三门课程成绩 */ }; int update_score(struct student *p, int n, int num, int course, int score); /*函数声明*/ int main(void) { int i, pos, n, num, course, score; struct student students[50]; /* 定义结构数组 */ scanf("%d", &n); for(i = 0; i < n; i++){ scanf("%d", &students[i].num); scanf("%s", students[i].name); scanf("%d", &students[i].math); scanf("%d", &students[i].english); scanf("%d", &students[i].computer); } /* 输入待修改学生信息 */ scanf("%d", &num); scanf("%d", &course); scanf("%d", &score); /*调用函数,修改学生成绩*/ pos = update_score(students, n, num, course, score); /*输出修改后的学生信息*/ if(pos == -1) printf("Not found!\n"); else { printf("%d,%d,%d,%d\n", students[pos].num, students[pos].math, students[pos].english, students[pos].computer); } return 0; } /* 请在这里填写答案 */
输入样例:
3
101 Zhang 78 87 85
102 Wang 91 88 90
103 Li 75 90 84
102 2 89
输出样例:
在这里给出相应的输出。例如:
102,91,89,90
int update_score(struct student *p,int n,int num,int c,int s){
for(int i=0;i<n;i++,p++){
if(p->num==num){
if(c==1) p->math=s;
if(c==2) p->english=s;
if(c==3) p->computer=s;
return 1;
}
}
return -1;
}