diff --git a/4th.c b/4th.c new file mode 100644 index 0000000..659c826 --- /dev/null +++ b/4th.c @@ -0,0 +1,75 @@ +#include +#include +typedef struct { + int ID; + int classnumber; + float score1,score2,score3; +}student; + +void insert(student students[],int *n,student new){ + int k=0; + int j=*n-1; + for( k;k<=*n;k++){ + if(k==*n||students[k].classnumber>new.classnumber){ + for( j;j>=k;j--){ + students[j+1]=students[j]; + } + students[k]= new; + (*n)++; + break; + }else if(students[k].ID==new.ID){ + printf("已存在学号的信息\n"); + return; + } + } +} +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; +} +int main(){ + int n=3; + student students[5]={ + {10001,11,99.5,88.5,89.5}, + {10002,12,77.9,56.5,87.5}, + {10003,11,92.5,99.0,60.5}}; + student new; + scanf("%d %d %f %f %f", &new.ID, &new.classnumber,&new.score1, &new.score2, &new.score3); + insert(students,&n,new); + qsort(students,n,sizeof(student),compare); + int i=0; + for( i;i