You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.1 KiB

#include <stdio.h>
#include <stdbool.h>
struct student
{
int id;
int class;
float score_math;
float score_phi;
float score_eng;
bool isinserted;
};
void sort(struct student a[],int count){
struct student temp;
for(int i = 0;i < count-1;i++){
for(int j = i+1;j<count-1;j++){
if(a[j].class > a[j+1].class){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (int m = 0; m < count-1; m++) {
for (int n = m + 1; n < count-1; n++) {
if (a[n].class == a[n+1].class) {
float sum1 = a[n].score_math + a[n].score_phi + a[n].score_eng;
float sum2 = a[n+1].score_math + a[n+1].score_phi + a[n+1].score_eng;
if (sum1 < sum2 || (sum1 == sum2 && a[m].id > a[n].id)) {
struct student temp = a[n];
a[n] = a[n+1];
a[n+1] = temp;
}
}
}
}
}
void printgrade(struct student a[],int count){
for(int i = 0;i<count;i++){
printf("%d %d %.1f %.1f %.1f",a[i].id,a[i].class,a[i].score_math,a[i].score_phi,a[i].score_eng);
if(a[i].isinserted){
printf(" inserted\n");
}
else{
printf("\n");
}
}
}
int main(){
struct student grade[4];
grade[0].id = 10001;
grade[0].class = 11;
grade[0].score_math = 99.5;
grade[0].score_phi = 88.5;
grade[0].score_eng = 89.5;
grade[0].isinserted = false;
grade[1].id = 10002;
grade[1].class = 12;
grade[1].score_math = 77.9;
grade[1].score_phi = 56.5;
grade[1].score_eng = 87.5;
grade[1].isinserted = false;
grade[2].id = 10003;
grade[2].class = 11;
grade[2].score_math = 92.5;
grade[2].score_phi = 99.0;
grade[2].score_eng = 60.5;
grade[2].isinserted = false;
scanf("%d %d %f %f %f",&grade[3].id,&grade[3].class,&grade[3].score_math,&grade[3].score_phi,&grade[3].score_eng);
grade[3].isinserted = true;
int count = 4;
sort(grade,count);
printgrade(grade,count);
return 0;
}