Aktualizovat „du5/program.c“

This commit is contained in:
Bohdan Yanchyk 2020-04-09 18:54:33 +00:00
parent f583b1cadc
commit f2a7087010

View File

@ -1,59 +1,60 @@
#include<stdio.h> #include<stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
struct person{
int score; struct person{
char name[30]; int score;
} char name[30];
};
void addElement(struct person PersonList*,int score,char *name){
static int size = 1; void addElement(struct person* PersonList,int score,char *name){
PersonList = realloc(PersonList,size*sizeof(struct person); static int size = 1;
PersonList[size-1]->score = score; PersonList = realloc(PersonList,size*sizeof(struct person);
PersonList[size-1]->name = name; PersonList[size-1]->score = score;
size++; PersonList[size-1]->name = name;
} size++;
}
int comp(const void *p1, const void *p2){
struct person* person1 = (struct person*)p1; int comp(const void *p1, const void *p2){
struct person* person2 = (struct person*)p2; struct person* person1 = (struct person*)p1;
struct person* person2 = (struct person*)p2;
return strcmp(person1->name, person2->name);
} return strcmp(person1->name, person2->name);
}
int main(){
struct person PersonList*=(struct person *)calloc(0,sizeof(struct person)); int main(){
int count = 0; struct person *PersonList=(struct person *)calloc(0,sizeof(struct person));
char name[30]; int count = 0;
int score=0; char name[30];
int c =0; int score=0;
while(scanf("%d %s",&score,name)!=EOF){ int c =0;
addElement(&PersonList,score,name); while(scanf("%d %s",&score,name)!=EOF){
c++; addElement(&PersonList,score,name);
} c++;
}
qsort(PersonList,c,sizeof(struct person),comp);
int idex = 0; qsort(PersonList,c,sizeof(struct person),comp);
for(int i =0;i<c;i++){ int idex = 0;
if(strcmp(PersonList[i].name,PersonList[i+1].name)==0){ for(int i =0;i<c;i++){
PersonList[i-idex].score += PersonList[i+1].score; if(strcmp(PersonList[i].name,PersonList[i+1].name)==0){
idex++; PersonList[i-idex].score += PersonList[i+1].score;
} idex++;
else{ }
idex=0; else{
} idex=0;
} }
}
printf("Vysledok\n");
for(int i =0;i<c;i++){ printf("Vysledok\n");
if(strcmp(PersonList[i].name,PersonList[i+1].name)!=0){ for(int i =0;i<c;i++){
printf("%d %s\n",PersonList.score,PersonList.name); if(strcmp(PersonList[i].name,PersonList[i+1].name)!=0){
} printf("%d %s\n",PersonList.score,PersonList.name);
}
}
}
return 0;
return 0;
} }