2024-03-21 22:47:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
#define MAX_SIZE 100
|
|
|
|
#define MAX_STUDENTS 100
|
|
|
|
|
|
|
|
// Štruktúra pre reprezentáciu študenta
|
|
|
|
struct Student {
|
|
|
|
char name[MAX_SIZE];
|
|
|
|
int votes;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Porovnávacia funkcia pre triedenie študentov
|
|
|
|
int compare(const void *p1, const void *p2) {
|
|
|
|
const struct Student *s1 = (const struct Student *)p1;
|
|
|
|
const struct Student *s2 = (const struct Student *)p2;
|
|
|
|
// Porovnanie počtu hlasov
|
|
|
|
if (s1->votes != s2->votes) {
|
|
|
|
return s2->votes - s1->votes; // Zoradenie zostupne podľa hlasov
|
2024-03-21 23:09:53 +00:00
|
|
|
}
|
2024-03-21 23:18:00 +00:00
|
|
|
// Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
|
|
|
|
return strcmp(s1->name, s2->name);
|
2024-03-21 23:09:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 23:20:05 +00:00
|
|
|
// Načíta študentov zo súboru do pola
|
|
|
|
int read_students_from_file(struct Student students[], int max_students) {
|
2024-03-21 23:18:00 +00:00
|
|
|
FILE *file = fopen("nazov_suboru.txt", "r");
|
|
|
|
if (file == NULL) {
|
|
|
|
perror("Chyba pri otváraní súboru");
|
2024-03-21 23:20:05 +00:00
|
|
|
return -1;
|
2024-03-21 23:13:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 23:04:41 +00:00
|
|
|
int num_students = 0;
|
2024-03-21 23:18:00 +00:00
|
|
|
char line[MAX_SIZE];
|
|
|
|
while (fgets(line, sizeof(line), file) != NULL) {
|
|
|
|
int votes;
|
|
|
|
char name[MAX_SIZE];
|
|
|
|
if (sscanf(line, "%d %99[^\n]", &votes, name) == 2) {
|
|
|
|
strcpy(students[num_students].name, name);
|
|
|
|
students[num_students].votes = votes;
|
2024-03-21 23:09:53 +00:00
|
|
|
num_students++;
|
2024-03-21 23:20:05 +00:00
|
|
|
if (num_students >= max_students) {
|
|
|
|
break; // Prekročený maximálny počet študentov
|
|
|
|
}
|
2024-03-21 23:04:41 +00:00
|
|
|
}
|
2024-03-21 22:47:06 +00:00
|
|
|
}
|
2024-03-21 23:01:55 +00:00
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
fclose(file);
|
2024-03-21 23:20:05 +00:00
|
|
|
return num_students;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pridá nového študenta do súboru
|
|
|
|
void add_student_to_file(const struct Student *student) {
|
|
|
|
FILE *file = fopen("nazov_suboru.txt", "a");
|
|
|
|
if (file == NULL) {
|
|
|
|
perror("Chyba pri otváraní súboru");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(file, "%d %s\n", student->votes, student->name);
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
struct Student students[MAX_STUDENTS];
|
|
|
|
int num_students = read_students_from_file(students, MAX_STUDENTS);
|
|
|
|
if (num_students == -1) {
|
|
|
|
return 1; // Chyba pri čítaní zo súboru
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pridáme nového študenta
|
|
|
|
struct Student new_student;
|
|
|
|
strcpy(new_student.name, "Novy Student");
|
|
|
|
new_student.votes = 5;
|
|
|
|
add_student_to_file(&new_student);
|
2024-03-21 23:13:59 +00:00
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
// Zoradíme študentov podľa počtu hlasov
|
|
|
|
qsort(students, num_students, sizeof(struct Student), compare);
|
2024-03-21 23:04:41 +00:00
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
// Vypíšeme výsledky
|
|
|
|
printf("Výsledky:\n");
|
2024-03-21 23:04:41 +00:00
|
|
|
for (int i = 0; i < num_students; i++) {
|
2024-03-21 23:18:00 +00:00
|
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
2024-03-21 22:58:42 +00:00
|
|
|
}
|
2024-03-21 23:01:55 +00:00
|
|
|
|
2024-03-21 22:47:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|