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.

74 lines
1.5 KiB

#include <stdio.h>
typedef struct{
int Id;
int class;
float a,b,c;
float total;
}Student;
void sort(Student *person,int len)
{
int i;
int j;
for(i=0;i<len-1;i++)
{
int k=i;
for(j=i;j<len-1;j++)
{
if(person[k].class>person[j].class||(person[k].class==person[j].class&&person[k].total<person[j].total))
{
k=j;
}
if(k!=i)
{
Student temp=person[i];
person[i]=person[k];
person[k]=temp;
}
}
}
}
int newId,newClass;
float grade1,grade2,grade3;
void print(Student *person)
{
int i;
for(i=0;i<4;i++)
{
printf("%d %d %.lf %.lf %.lf",person[i].Id,person[i].class,person[i].a,person[i].b,person[i].c);
if(person[i].Id==newId)
{
printf(" inserted");
}
printf("\n");
}
}
int main()
{
Student students[4]=
{
{10001,11,99.5,88.5,89.5,99.5+88.5+89.5},
{10002,12,77.9,56.5,87.5,77.9+56.5+87.5},
{10003,11,92.5,99.0,60.5,92.5+99.0+60.5},
{10000,0,0.0,0.0,0.0,0.0}
};
scanf("%d %d %f %f %f",&newId,&newClass,&grade1,&grade2,&grade3);
int i;
for(i=0;i<3;i++)
{
if(students[i].Id==newId)
{
printf("The student id %d already exists.\n",newId);
return 0;
}
}
students[3].Id=newId;
students[3].class=newClass;
students[3].a=grade1;
students[3].b=grade2;
students[3].c=grade3;
students[3].total=grade1+grade2+grade3;
sort(students,4);
print(students);
return 0;
}