92 lines
1.9 KiB
C
92 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define SIZE 100
|
|
|
|
struct student {
|
|
char name[SIZE];
|
|
int votes;
|
|
};
|
|
|
|
int find_student(struct student* students, int size, const char* name) {
|
|
for (int i = 0; i < size; i++) {
|
|
if (strcmp(students[i].name, name) == 0) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int compare(const void* p1, const void* p2) {
|
|
struct student* s1 = (struct student*)p1;
|
|
struct student* s2 = (struct student*)p2;
|
|
|
|
if (s2->votes != s1->votes) {
|
|
return s2->votes - s1->votes;
|
|
}
|
|
|
|
return strcmp(s1->name, s2->name);
|
|
}
|
|
|
|
int main() {
|
|
struct student databaza[SIZE];
|
|
memset(databaza, 0, SIZE * sizeof(struct student));
|
|
int size = 0;
|
|
int any_valid_input = 0;
|
|
|
|
char line[SIZE];
|
|
|
|
while (fgets(line, SIZE, stdin) != NULL);
|
|
char* newline = strchr(line, '\n');
|
|
if (newline) *newline = '\0';
|
|
|
|
char* end = NULL;
|
|
int value = strtol(line, &end, 10);
|
|
|
|
if (value <= 0) {
|
|
break;
|
|
}
|
|
|
|
if (*end != ' ') {
|
|
break;
|
|
}
|
|
|
|
char* zaciatok_mena = end + 1;
|
|
int velkost_mena = strlen(zaciatok_mena);
|
|
|
|
if (velkost_mena <= 0) {
|
|
break;
|
|
}
|
|
|
|
char name[SIZE];
|
|
memset(name, 0, SIZE);
|
|
memcpy(name, zaciatok_mena, velkost_mena);
|
|
|
|
int id = find_student(databaza, size, name);
|
|
|
|
if (id < 0) {
|
|
strcpy(databaza[size].name, name);
|
|
databaza[size].votes = value;
|
|
size++;
|
|
} else {
|
|
databaza[id].votes += value;
|
|
}
|
|
|
|
any_valid_input = 1;
|
|
}
|
|
|
|
if (!any_valid_input) {
|
|
printf("Chyba: Nepodarilo sa načítať žiadny platný záznam.\n");
|
|
return 1;
|
|
}
|
|
qsort(databaza, size, sizeof(struct student), compare);
|
|
|
|
printf("Vysledky:\n");
|
|
for (int i = 0; i < size; i++) {
|
|
printf("%d %s\n", databaza[i].votes, databaza[i].name);
|
|
}
|
|
|
|
return 0;
|
|
}
|