修改学生成绩

#include<stdio.h>
struct student {
int num;
char name[10];
int computer,english,math;
double average;
};
int update_score(struct student *p,int n,int num,int course,int score);
int main(){
int course,i,n,num,pos,score;
struct student students[50];

printf("Input n:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Input the info of No.%d:\n",i+1);
printf("number:");
scanf("%d",&students[i].num);
printf("name:");
scanf("%s",&students[i].name);
printf("math score:");
scanf("%d",&students[i].math);
printf("english score:");
scanf("%d",&students[i].english);
printf("computer score:");
scanf("%d",&students[i].computer);
printf("\n");
}

printf("Input the number of the students to be updated:");
scanf("%d",&num);
printf("Choice the course:1.math 2.english 3.computer:");
scanf("%d",&course);
printf("input the new score:");
scanf("%d",&score);

pos=update_score(students,n,num,course,score);

if(pos==-1) printf("Not found!\n");
else{
printf("After update:\n");
printf("num\t math\t english \t computer\n");
printf("%d\t %d\t %d\t %d\n",students[pos].num,students[pos].math,students[pos].english,students[pos].computer);
}

return 0;
}
int update_score(struct student *p,int n,int num,int course,int score){
int i,pos;
for(i=0;i<n;i++,p++){
if(p->num==num) break;
if(i<n){
switch(course){
case1:p->math=score;break;
case2:p->english=score;break;
case3:p->computer=score;break;
}
pos=i;
}
else pos=-1;
}
return pos;
}

posted @ 2025-01-02 16:06  Grit_Doyle  阅读(27)  评论(0)    收藏  举报