2024-03-20 11:41:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define SIZE 100
|
|
|
|
|
|
|
|
|
|
|
|
struct student {
|
|
|
|
char name[SIZE];
|
|
|
|
int votes;
|
|
|
|
};
|
2024-03-20 12:03:50 +00:00
|
|
|
|
|
|
|
int read_input(int* votes, char* name, int size) {
|
|
|
|
char line[SIZE];
|
|
|
|
if (fgets(line, size, stdin) != NULL) {
|
|
|
|
|
|
|
|
char* space = strchr(line, ' ');
|
|
|
|
if (space != NULL) {
|
|
|
|
*votes = atoi(line);
|
2024-03-20 12:16:08 +00:00
|
|
|
strcpy(name, space + 1);
|
|
|
|
char* newline = strchr(name, '\n');
|
2024-03-20 12:03:50 +00:00
|
|
|
if (newline)
|
|
|
|
*newline = '\0';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 12:16:08 +00:00
|
|
|
return 0;
|
2024-03-20 11:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-20 12:16:08 +00:00
|
|
|
|
2024-03-20 11:41:33 +00:00
|
|
|
int parse_int(const char* str) {
|
|
|
|
return atoi(str);
|
|
|
|
}
|
|
|
|
|
2024-03-20 12:16:08 +00:00
|
|
|
|
2024-03-20 11:41:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void add_student(struct student* students, int* size, const char* name, int votes) {
|
|
|
|
|
|
|
|
int index = find_student(students, *size, name);
|
|
|
|
if (index == -1) {
|
|
|
|
if (*size < SIZE) {
|
|
|
|
strcpy(students[*size].name, name);
|
|
|
|
students[*size].votes = votes;
|
|
|
|
(*size)++;
|
|
|
|
} else {
|
2024-03-20 12:16:08 +00:00
|
|
|
printf("Datebase is crowded!\n");
|
2024-03-20 11:41:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
students[index].votes += votes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int compare_students(const void* p1, const void* p2) {
|
|
|
|
const struct student* s1 = (const struct student*)p1;
|
|
|
|
const struct student* s2 = (const struct student*)p2;
|
|
|
|
|
|
|
|
|
|
|
|
if (s1->votes != s2->votes) {
|
2024-03-20 12:16:08 +00:00
|
|
|
return s2->votes - s1->votes;
|
2024-03-20 11:41:33 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
return strcmp(s1->name, s2->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 12:03:50 +00:00
|
|
|
int main() {
|
|
|
|
struct student database[SIZE];
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
int votes;
|
|
|
|
char name[SIZE];
|
|
|
|
|
|
|
|
|
|
|
|
while (read_input(&votes, name, SIZE)) {
|
|
|
|
add_student(database, &size, name, votes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
qsort(database, size, sizeof(struct student), compare_students);
|
|
|
|
|
|
|
|
|
2024-03-20 12:16:08 +00:00
|
|
|
printf("Vysledky:\n");
|
2024-03-20 12:03:50 +00:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
printf("%d %s\n", database[i].votes, database[i].name);
|
|
|
|
}
|
|
|
|
|
2024-03-20 11:55:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2024-03-20 12:16:08 +00:00
|
|
|
|