Update du4/program.c
This commit is contained in:
parent
c2e16f54da
commit
a46ed29cd2
@ -3,77 +3,91 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#define MAX_NAME 100
|
#define MAX_NAME 100 // Maximalna dlzka mena studenta
|
||||||
#define MAX_ENTRIES 100
|
#define MAX_ENTRIES 100 // Maximalny pocet studentov
|
||||||
|
|
||||||
|
// Struktura reprezentujuca studenta a jeho pocet hlasov
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *name;
|
char *name; // Meno studenta
|
||||||
int votes;
|
int votes; // Pocet hlasov
|
||||||
} Student;
|
} Student;
|
||||||
|
|
||||||
|
// Pole ukazatelov na studentov a pocitadlo studentov
|
||||||
Student *students[MAX_ENTRIES];
|
Student *students[MAX_ENTRIES];
|
||||||
int student_count = 0;
|
int student_count = 0;
|
||||||
|
|
||||||
|
// Funkcia na najdenie existujuceho studenta alebo pridanie noveho
|
||||||
Student *find_or_add(const char *name) {
|
Student *find_or_add(const char *name) {
|
||||||
|
// Hladanie studenta v existujucom zozname
|
||||||
for (int i = 0; i < student_count; i++) {
|
for (int i = 0; i < student_count; i++) {
|
||||||
if (strcmp(students[i]->name, name) == 0) {
|
if (strcmp(students[i]->name, name) == 0) {
|
||||||
return students[i];
|
return students[i]; // Ak student existuje, vratime jeho ukazatel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Ak student nie je v zozname a je miesto, pridame ho
|
||||||
if (student_count < MAX_ENTRIES) {
|
if (student_count < MAX_ENTRIES) {
|
||||||
Student *new_student = (Student *)malloc(sizeof(Student));
|
Student *new_student = (Student *)malloc(sizeof(Student)); // Alokacia pamate pre noveho studenta
|
||||||
new_student->name = strdup(name);
|
new_student->name = strdup(name); // Kopia mena
|
||||||
new_student->votes = 0;
|
new_student->votes = 0; // Inicializacia hlasov na 0
|
||||||
students[student_count++] = new_student;
|
students[student_count++] = new_student; // Pridanie do pola
|
||||||
return new_student;
|
return new_student;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL; // Ak uz nie je miesto, vratime NULL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Funkcia na zoradenie studentov podla hlasov a mena (ak maju rovnaky pocet hlasov)
|
||||||
int compare(const void *a, const void *b) {
|
int compare(const void *a, const void *b) {
|
||||||
Student *s1 = *(Student **)a;
|
Student *s1 = *(Student **)a;
|
||||||
Student *s2 = *(Student **)b;
|
Student *s2 = *(Student **)b;
|
||||||
return (s2->votes != s1->votes) ? (s2->votes - s1->votes) : strcmp(s1->name, s2->name);
|
return (s2->votes != s1->votes) ? (s2->votes - s1->votes) : strcmp(s1->name, s2->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Funkcia na uvolnenie alokovanej pamate
|
||||||
void free_memory() {
|
void free_memory() {
|
||||||
for (int i = 0; i < student_count; i++) {
|
for (int i = 0; i < student_count; i++) {
|
||||||
free(students[i]->name);
|
free(students[i]->name); // Uvolnenie pamate mena
|
||||||
free(students[i]);
|
free(students[i]); // Uvolnenie pamate studenta
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char line[MAX_NAME + 10];
|
char line[MAX_NAME + 10]; // Buffer na citanie riadkov zo vstupu
|
||||||
int votes;
|
int votes; // Pocet hlasov
|
||||||
char name[MAX_NAME];
|
char name[MAX_NAME]; // Meno studenta
|
||||||
int has_data = 0;
|
int has_data = 0; // Indikator ci boli nacitane nejake data
|
||||||
|
|
||||||
|
// Citanie vstupu zo standardneho vstupu (stdin)
|
||||||
while (fgets(line, sizeof(line), stdin)) {
|
while (fgets(line, sizeof(line), stdin)) {
|
||||||
// Preskočí prázdne riadky
|
// Preskoci prazdne riadky
|
||||||
if (strlen(line) <= 1) continue;
|
if (strlen(line) <= 1) continue;
|
||||||
|
|
||||||
if (sscanf(line, "%d %[^\n]", &votes, name) != 2 || votes < 0) {
|
// Parsovanie vstupu: ocakava sa format "pocet_hlasov meno"
|
||||||
continue; // Namiesto prerušenia len ignoruje neplatné riadky
|
if (sscanf(line, "%d %[^"]n", &votes, name) != 2 || votes < 0) {
|
||||||
|
continue; // Ak format nesedi alebo su zaporne hlasy, ignorujeme riadok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Najdenie alebo pridanie studenta a pridanie hlasov
|
||||||
Student *student = find_or_add(name);
|
Student *student = find_or_add(name);
|
||||||
if (student) student->votes += votes;
|
if (student) student->votes += votes;
|
||||||
has_data = 1;
|
has_data = 1; // Nastavenie flagu, ze mame platne data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ak neboli nacitane platne data, vypise sa sprava a ukonci program
|
||||||
if (!has_data) {
|
if (!has_data) {
|
||||||
printf("Nepodarilo nacitat nic\n");
|
printf("Nepodarilo nacitat nic\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Zoradenie studentov podla poctu hlasov a mena
|
||||||
qsort(students, student_count, sizeof(Student *), compare);
|
qsort(students, student_count, sizeof(Student *), compare);
|
||||||
|
|
||||||
|
// Vypis vysledkov
|
||||||
printf("Vysledky:\n");
|
printf("Vysledky:\n");
|
||||||
for (int i = 0; i < student_count; i++) {
|
for (int i = 0; i < student_count; i++) {
|
||||||
printf("%d %s\n", students[i]->votes, students[i]->name);
|
printf("%d %s\n", students[i]->votes, students[i]->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Uvolnenie alokovanej pamate
|
||||||
free_memory();
|
free_memory();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user