pvjc20/du5/program.c

109 lines
2.1 KiB
C
Raw Normal View History

2020-04-09 18:54:33 +00:00
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
2020-04-09 23:34:05 +00:00
#include<ctype.h>
2020-04-09 19:01:22 +00:00
2020-04-09 18:54:33 +00:00
struct person{
int score;
char name[30];
};
2020-04-09 20:12:45 +00:00
/*
2020-04-09 19:42:01 +00:00
struct person* addElement(struct person **PersonList,int score,char *name){
2020-04-09 18:54:33 +00:00
static int size = 1;
2020-04-09 19:48:54 +00:00
struct person *Personlist = calloc(size,sizeof(struct person));
2020-04-09 20:12:45 +00:00
2020-04-09 18:54:33 +00:00
size++;
2020-04-09 19:41:19 +00:00
return Personlist;
2020-04-09 18:54:33 +00:00
}
2020-04-09 20:12:45 +00:00
*/
2020-04-09 18:54:33 +00:00
int comp(const void *p1, const void *p2){
struct person* person1 = (struct person*)p1;
struct person* person2 = (struct person*)p2;
2020-04-09 22:58:14 +00:00
return strcmp(person1->name, person2->name)>0;
2020-04-09 18:54:33 +00:00
}
2020-04-09 20:29:40 +00:00
int comp_num(const void *p1, const void *p2){
struct person* person1 = (struct person*)p1;
struct person* person2 = (struct person*)p2;
return person1->score<person2->score;
}
2020-04-09 18:54:33 +00:00
int main(){
2020-04-09 20:12:45 +00:00
struct person PersonList[30];
2020-04-09 18:54:33 +00:00
char name[30];
int score=0;
int c =0;
2020-04-09 20:12:45 +00:00
char str[100];
2020-04-09 23:34:05 +00:00
char digit[5];
2020-04-09 20:12:45 +00:00
while(fgets(str,100,stdin)){
if(str[0]=='\n'){
break;
}
2020-04-09 23:34:05 +00:00
sscanf(str,"%s %[^\t\n]",digit,name);
if(isalpha(digit[0])!=0){
2020-04-09 23:35:42 +00:00
if(c==0){
printf("Nepodarilo nacitat nic\n");
return 0;
}
break;
2020-04-09 23:34:05 +00:00
}
int score = atoi(digit);
2020-04-09 20:12:45 +00:00
PersonList[c].score = score;
strcpy(PersonList[c].name ,name);
2020-04-09 18:54:33 +00:00
c++;
}
2020-04-09 20:12:45 +00:00
if(c>2){
2020-04-09 22:58:14 +00:00
qsort(PersonList,c,sizeof(struct person),comp);
2020-04-09 20:24:07 +00:00
int res_strcmp=0;
2020-04-09 18:54:33 +00:00
int idex = 0;
for(int i =0;i<c;i++){
2020-04-09 22:58:14 +00:00
res_strcmp = strcmp(PersonList[i].name,PersonList[i+1].name);
if(res_strcmp==0){
PersonList[i-idex].score += PersonList[i+1].score;
idex++;
}
else{
idex=0;
}
2020-04-09 21:11:38 +00:00
}
2020-04-09 21:10:04 +00:00
2020-04-09 22:58:14 +00:00
}
2020-04-09 21:10:04 +00:00
2020-04-09 23:22:43 +00:00
int flag=0;
2020-04-09 21:10:04 +00:00
qsort(PersonList,c,sizeof(struct person),comp_num);
2020-04-09 22:58:14 +00:00
int k =0;
2020-04-09 21:10:04 +00:00
2020-04-09 23:28:36 +00:00
for(int i=0; i<c; i++)
{
for(int j=i+1; j<c; j++)
{
if(strcmp(PersonList[i].name,PersonList[j].name)==0)
{
for(int k=j; k<c; k++)
{
PersonList[k] = PersonList[k + 1];
}
c--;
j--;
2020-04-09 22:58:14 +00:00
}
2020-04-09 23:28:36 +00:00
}
2020-04-09 23:22:43 +00:00
}
2020-04-09 23:28:36 +00:00
2020-04-09 20:30:19 +00:00
printf("Vysledky:\n");
2020-04-09 23:18:30 +00:00
for(int i =0;i<c;i++){
2020-04-09 21:10:04 +00:00
printf("%d %s\n",PersonList[i].score,PersonList[i].name);
2020-04-09 18:54:33 +00:00
}
return 0;
2020-04-09 22:58:14 +00:00
}