From 97741f611e12bdb26d6446af2ed8002388217018 Mon Sep 17 00:00:00 2001 From: Bohdan Yanchyk Date: Thu, 9 Apr 2020 20:12:45 +0000 Subject: [PATCH] =?UTF-8?q?Aktualizovat=20=E2=80=9Edu5/program.c=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du5/program.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) 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