85 lines
1.8 KiB
C
85 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
#define namemax 100
|
|
#define maxstudents 1000
|
|
|
|
typedef struct {
|
|
char name[namemax];
|
|
int golosa;
|
|
} Student;
|
|
|
|
int find_student(Student students[], int count, char *name) {
|
|
for (int i = 0; i < count; i++) {
|
|
if (strcmp(students[i].name, name) == 0) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int compare(const void *a, const void *b) {
|
|
Student *studentA = (Student *)a;
|
|
Student *studentB = (Student *)b;
|
|
|
|
if (studentA->golosa != studentB->golosa) {
|
|
return studentB->golosa - studentA->golosa;
|
|
}
|
|
return strcmp(studentA->name, studentB->name);
|
|
}
|
|
|
|
int main() {
|
|
Student students[maxstudents];
|
|
int count = 0;
|
|
int valid_data_found = 0;
|
|
char buffer[200];
|
|
|
|
while (fgets(buffer, sizeof(buffer), stdin)) {
|
|
buffer[strcspn(buffer, "\n")] = '\0';
|
|
|
|
if (strlen(buffer) == 0) {
|
|
break;
|
|
}
|
|
|
|
int golosa;
|
|
char name[namemax];
|
|
|
|
if (sscanf(buffer, "%d %99[^\n]", &golosa, name) != 2 || golosa <= 0) {
|
|
break;
|
|
}
|
|
|
|
valid_data_found = 1;
|
|
|
|
int index = find_student(students, count, name);
|
|
|
|
if (index != -1) {
|
|
students[index].golosa += golosa;
|
|
} else {
|
|
strcpy(students[count].name, name);
|
|
students[count].golosa = golosa;
|
|
count++;
|
|
}
|
|
}
|
|
|
|
if (!valid_data_found) {
|
|
printf("Nepodarilo nacitat nic\n");
|
|
return 0;
|
|
}
|
|
|
|
if (count == 0) {
|
|
printf("Chyba: Nepodarilo sa nacitat ziadny zaznam.\n");
|
|
return 0;
|
|
}
|
|
|
|
qsort(students, count, sizeof(Student), compare);
|
|
|
|
printf("Vysledky:\n");
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
printf("%d %s\n", students[i].golosa, students[i].name);
|
|
}
|
|
|
|
return 0;
|
|
} |