diff --git a/du5/program.c b/du5/program.c index d4e9f49..1f506ad 100644 --- a/du5/program.c +++ b/du5/program.c @@ -7,16 +7,15 @@ struct person{ int score; char name[30]; }; - +/* struct person* addElement(struct person **PersonList,int score,char *name){ static int size = 1; struct person *Personlist = calloc(size,sizeof(struct person)); - Personlist[size-1].score = score; - strcpy(Personlist[size-1].name ,name); + size++; return Personlist; } - +*/ int comp(const void *p1, const void *p2){ struct person* person1 = (struct person*)p1; @@ -28,16 +27,25 @@ int comp(const void *p1, const void *p2){ int main(){ - struct person *PersonList=calloc(0,sizeof(struct person)); + struct person PersonList[30]; char name[30]; int score=0; int c =0; - while(scanf("%d %s",&score,name)!=EOF){ - PersonList = addElement(&PersonList,score,name); + char str[100]; + while(fgets(str,100,stdin)){ + if(str[0]=='\n'){ + break; + } + sscanf(str,"%d %s",&score,name); + PersonList[c].score = score; + strcpy(PersonList[c].name ,name); c++; } - qsort(PersonList,c,sizeof(struct person),comp); + if(c>2){ + qsort(PersonList,c,sizeof(struct person),comp); + } + int idex = 0; for(int i =0;i