Aktualizovat „du5/program.c“

This commit is contained in:
Bohdan Yanchyk 2020-04-09 20:12:45 +00:00
parent 9ba158588e
commit 97741f611e

View File

@ -7,16 +7,15 @@ struct person{
int score; int score;
char name[30]; char name[30];
}; };
/*
struct person* addElement(struct person **PersonList,int score,char *name){ struct person* addElement(struct person **PersonList,int score,char *name){
static int size = 1; static int size = 1;
struct person *Personlist = calloc(size,sizeof(struct person)); struct person *Personlist = calloc(size,sizeof(struct person));
Personlist[size-1].score = score;
strcpy(Personlist[size-1].name ,name);
size++; size++;
return Personlist; return Personlist;
} }
*/
int comp(const void *p1, const void *p2){ int comp(const void *p1, const void *p2){
struct person* person1 = (struct person*)p1; struct person* person1 = (struct person*)p1;
@ -28,16 +27,25 @@ int comp(const void *p1, const void *p2){
int main(){ int main(){
struct person *PersonList=calloc(0,sizeof(struct person)); struct person PersonList[30];
char name[30]; char name[30];
int score=0; int score=0;
int c =0; int c =0;
while(scanf("%d %s",&score,name)!=EOF){ char str[100];
PersonList = addElement(&PersonList,score,name); 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++; c++;
} }
qsort(PersonList,c,sizeof(struct person),comp); if(c>2){
qsort(PersonList,c,sizeof(struct person),comp);
}
int idex = 0; int idex = 0;
for(int i =0;i<c;i++){ for(int i =0;i<c;i++){
if(strcmp(PersonList[i].name,PersonList[i+1].name)==0){ if(strcmp(PersonList[i].name,PersonList[i+1].name)==0){
@ -48,17 +56,20 @@ int main(){
idex=0; idex=0;
} }
} }
int flag=0; int flag=0;
printf("Vysledok\n"); printf("Vysledok\n");
for(int i =0;i<c;i++){ for(int i =0;i<c;i++){
if((strcmp(PersonList[i].name,PersonList[i+1].name)==0||strcmp(PersonList[i].name,PersonList[i+1].name)!=0)&&flag==0){ if(strcmp(PersonList[i].name,PersonList[i+1].name)!=0){
flag=0;
printf("%d %s\n",PersonList[i].score,PersonList[i].name); printf("%d %s\n",PersonList[i].score,PersonList[i].name);
flag=1; }
} else if(strcmp(PersonList[i].name,PersonList[i+1].name)==0&&flag==0){
else{ flag=1;
flag=0; printf("%d %s\n",PersonList[i].score,PersonList[i].name);
} }
} }
free(PersonList);
return 0; return 0;
} }