//输入n个学生成成绩信息,再输入一个学生的学号,课程以及成绩,修改指定课程成绩
#include<stdio.h>
struct student{
int num;
char name[10];
int a,b,c;
double ave;
};
int update_sorce(struct student *p,int n,int num,int course,int score);
int main()
{
int n,i,pos,num,course,score;
printf("请输入学生个数:");
scanf("%d",&n);
struct student s[100],temp;
for(i=0;i<n;i++){
printf("第%d个学生信息",i+1);
scanf("%d%s%d%d%d",&s[i].num,s[i].name,&s[i].a,&s[i].b,&s[i].c);
s[i].ave=(s[i].a+s[i].b+s[i].c)/3.0;
}
printf("请输入待修改学生信息:");
printf("请输入学号:");
scanf("%d",&num);
printf("请输入课程选择a-1,b-2,c-3");
scanf("%d",&course);
printf("分数为:");
scanf("%d",&score);
pos=update_sorce(s,n,num,course,score);
if(pos==-1){
printf("没有此学生");
}
else{
printf("修改后的这个学生信息为:\n");
printf("%d,%s,%d,%d,%d,%d",s[pos].num,s[pos].name,s[pos].a,s[pos].b,s[pos].c);
}
return 0;
}
int update_sorce(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)
{
case 1:p->a=score;break;
case 2:p->b=score;break;
case 3:p->c=score;break;
}
pos=i;
}
else{
pos=-1;
}
return pos;
}
.2显示所有信息
#include<stdio.h>
struct student{
int num;
char name[10];
int a,b,c;
double ave;
};
int update_sorce(struct student *p,int n,int num,int course,int score);
int main()
{
int n,i,pos,num,course,score;
printf("请输入学生个数:");
scanf("%d",&n);
struct student s[100],temp;
for(i=0;i<n;i++){
printf("第%d个学生信息",i+1);
scanf("%d%s%d%d%d",&s[i].num,s[i].name,&s[i].a,&s[i].b,&s[i].c);
s[i].ave=(s[i].a+s[i].b+s[i].c)/3.0;
}
printf("请输入待修改学生信息:");
printf("请输入学号:");
scanf("%d",&num);
printf("请输入课程选择a-1,b-2,c-3");
scanf("%d",&course);
printf("分数为:");
scanf("%d",&score);
for(i=0;i<n;i++){
printf("修改前第%d个学生信息,%d %s %d %d %d %.2f\n",i+1,s[i].num,s[i].name,s[i].a,s[i].b,s[i].c,s[i].ave);
}
pos=update_sorce(s,n,num,course,score);
if(pos==-1){
printf("没有此学生");
}
else{
printf("修改后的这个学生信息为:\n");
printf("%d,%s,%d,%d,%d,%d",s[pos].num,s[pos].name,s[pos].a,s[pos].b,s[pos].c);
}
for(i=0;i<n;i++){
s[i].ave=(s[i].a+s[i].b+s[i].c)/3.0;
printf("修改后第%d个学生信息,%d %s %d %d %d %.2f \n",i+1,s[i].num,s[i].name,s[i].a,s[i].b,s[i].c,s[i].ave);
}
return 0;
}
int update_sorce(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)
{
case 1:p->a=score;break;
case 2:p->b=score;break;
case 3:p->c=score;break;
}
pos=i;
}
else{
pos=-1;
}
return pos;
}