ADD file via upload

main
psc2uwxgy 5 days ago
parent 42766de5b8
commit 2ab6fdedf1

@ -0,0 +1,99 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
int ID;
char name[30];
int classnumber;
float score1,score2,score3;
}student;
int find(int ID,const char *name,student students[],int count){
int i;
for(i=0;i<count;i++){
if(students[i].ID==ID){
return i;
}
}
return -1;
}
void modifystudent(student students[],int index,float newscore1,float newscore2,float newscore3){
students[index].score1=newscore1;
students[index].score2=newscore2;
students[index].score3=newscore3;
}
int compare(const void*a,const void *b){
student s1=*(student*)a;
student s2=*(student*)b;
if(s1.classnumber!=s2.classnumber){
return s1.classnumber-s2.classnumber;
}
float total1=s1.score1+s1.score2+s1.score3;
float total2=s2.score1+s2.score2+s2.score3;
return total2-total1;
}
void print(student students[],int n,int modifiedindex){
int classn=-1;
int j;
for(j=0;j<n;j++){
if(students[j].classnumber!=classn){
classn=students[j].classnumber;
printf("%d %d %s %.1f %.1f %.1f",students[j].classnumber,students[j].ID,students[j].name,students[j].score1,students[j].score2,students[j].score3);
if(j==modifiedindex){
printf(" modified\n");
}
}else{
printf(" %d %s %.1f %.1f %.1f\n",students[j].ID,students[j].name,students[j].score1,students[j].score2,students[j].score3);
}
}
}
int main(){
student students[MAX_STUDENTS];
int n = 3;
students[0].ID= 10001;
students[0].classnumber = 11;
strcpy(students[0].name, "Zhang");
students[0].score1 = 99.5;
students[0].score2 = 88.5;
students[0].score3 = 89.5;
students[1].ID= 10002;
students[1].classnumber = 12;
strcpy(students[1].name, "Yang");
students[1].score1 = 77.9;
students[1].score2 = 56.5;
students[1].score3 = 87.5;
students[2].ID= 10003;
students[2].classnumber= 11;
strcpy(students[2].name, "Liang");
students[2].score1 = 92.5;
students[2].score2 = 99.0;
students[2].score3 = 60.5;
qsort(students,n,sizeof(student),compare);
student newstudent;
scanf("%d %d %s %f %f %f",&newstudent.ID,&newstudent.classnumber,&newstudent.name,&newstudent.score1,&newstudent.score2,&newstudent.score3);
int index=find(newstudent.ID,NULL,students,n);
int modifiedindex=-1;
if(index!=-1){
modifystudent(students,index,newstudent.score1,newstudent.score2,newstudent.score3);
modifiedindex=index;
}
print(students,n, modifiedindex);
return 0;
}
Loading…
Cancel
Save