Upload files to 'du5'

This commit is contained in:
Patrik Seman 2023-03-25 11:52:14 +00:00
parent bd3bbb3219
commit 5f0ad4f343

87
du5/program.c Normal file
View File

@ -0,0 +1,87 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
struct student{
int votes;
char name[SIZE];
};
int search_index(int size, const struct student list[SIZE], const char student[SIZE]){
for(int x = 0; x < size; x++){
if(strcmp(list[x].name, student) == 0){
return x;
}
}
return -1;
}
int sort_by_votes(const void* p1, const void* p2){
struct student* student1 = (struct student*) p1;
struct student* student2 = (struct student*) p2;
if(student1->votes != student2->votes){
if(student1->votes < student2->votes){
return 1;
}else{
return -1;
}
}else{
return strcmp(student1->name, student2->name);
}
}
int main(){
int votes;
char name[SIZE];
char line[SIZE];
char *end;
int counter = 0;
struct student list[SIZE];
while(fgets(line, SIZE, stdin) != NULL){
if(line[0] == '\n' || !isdigit(line[0])){
break;
}
votes = strtol(line, &end, 10);
if(end[0] != ' ' || !isalpha(end[1])){
break;
}
if(end[strlen(end) - 1] == '\n'){
end[strlen(end) - 1] = '\0';
}
strcpy(name, end + 1);
int stud_index = search_index(counter, list, name);
if(stud_index == -1){
counter++;
list[counter - 1].votes = votes;
strcpy(list[counter - 1].name, name);
}else{
list[stud_index].votes += votes;
}
}
if(counter == 0){
printf("Nepodarilo sa nacitat ziaden vstup\n");
return 0;
}
qsort(list, counter, sizeof(list[0]), sort_by_votes);
printf("Vysledky:\n");
for(int x = 0; x < counter; x++){
printf("%d %s\n", list[x].votes, list[x].name);
}
}