posledny krat a kaslem to
This commit is contained in:
parent
088648b67c
commit
90d7159ce6
115
cv5/program.c
115
cv5/program.c
@ -1,87 +1,70 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct student {
|
||||
char *name;
|
||||
int vote_count;
|
||||
} student_t;
|
||||
#define SIZE 100
|
||||
|
||||
int compare_students(const void *a, const void *b) {
|
||||
const student_t *student_a = (const student_t *)a;
|
||||
const student_t *student_b = (const student_t *)b;
|
||||
struct student {
|
||||
char name[SIZE];
|
||||
int votes;
|
||||
};
|
||||
|
||||
if (student_a->vote_count > student_b->vote_count) {
|
||||
return -1;
|
||||
} else if (student_a->vote_count < student_b->vote_count) {
|
||||
return 1;
|
||||
int compare(const void* p1, const void* p2){
|
||||
struct student* s1 = (struct student*)p1;
|
||||
struct student* s2 = (struct student*)p2;
|
||||
|
||||
if (s1->votes != s2->votes)
|
||||
return s2->votes - s1->votes;
|
||||
else
|
||||
return strcmp(s1->name, s2->name);
|
||||
}
|
||||
|
||||
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 strcmp(student_a->name, student_b->name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
student_t *students = NULL;
|
||||
int students_capacity = 0;
|
||||
int student_count = 0;
|
||||
struct student database[SIZE];
|
||||
memset(database, 0, SIZE * sizeof(struct student));
|
||||
int size = 0;
|
||||
|
||||
char line[1024];
|
||||
int total_votes = 0; // Celkový počet hlasov, ak je iba jeden študent
|
||||
char line[SIZE];
|
||||
while (fgets(line, SIZE, stdin)) {
|
||||
char* end = NULL;
|
||||
int votes = strtol(line, &end, 10);
|
||||
if (votes == 0) {
|
||||
fprintf(stderr, "Chyba\n");
|
||||
break;
|
||||
}
|
||||
if (*end != ' ') {
|
||||
fprintf(stderr, "Chyba\n");
|
||||
break;
|
||||
}
|
||||
char name[SIZE];
|
||||
strcpy(name, end + 1);
|
||||
name[strlen(name) - 1] = '\0';
|
||||
|
||||
while (fgets(line, sizeof(line), stdin)) {
|
||||
int vote_count;
|
||||
char name[1024];
|
||||
sscanf(line, "%d %[^\n]", &vote_count, name);
|
||||
|
||||
if (student_count == 0) {
|
||||
total_votes = vote_count;
|
||||
int index = find_student(database, size, name);
|
||||
if (index == -1) {
|
||||
strcpy(database[size].name, name);
|
||||
database[size].votes = votes;
|
||||
size++;
|
||||
} else {
|
||||
int existing_student_index = -1;
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
if (strcmp(students[i].name, name) == 0) {
|
||||
existing_student_index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (existing_student_index != -1) {
|
||||
students[existing_student_index].vote_count += vote_count;
|
||||
} else {
|
||||
if (student_count >= students_capacity) {
|
||||
students_capacity = (students_capacity + 1) * 2;
|
||||
student_t *new_students = realloc(students, students_capacity * sizeof(student_t));
|
||||
if (new_students == NULL) {
|
||||
fprintf(stderr, "Error: Memory reallocation failed\n");
|
||||
return 1;
|
||||
}
|
||||
students = new_students;
|
||||
}
|
||||
|
||||
students[student_count].name = strdup(name);
|
||||
students[student_count].vote_count = vote_count;
|
||||
student_count++;
|
||||
}
|
||||
database[index].votes += votes;
|
||||
}
|
||||
}
|
||||
|
||||
if (student_count == 0) {
|
||||
printf("Žiadni študenti.\n");
|
||||
} else if (student_count == 1) {
|
||||
printf("Vysledky:\n");
|
||||
printf("%d %s\n", total_votes, students[0].name);
|
||||
} else {
|
||||
qsort(students, student_count, sizeof(student_t), compare_students);
|
||||
printf("Vysledky:\n");
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
printf("%d %s\n", students[i].vote_count, students[i].name);
|
||||
}
|
||||
}
|
||||
qsort(database, size, sizeof(struct student), compare);
|
||||
|
||||
for (int i = 0; i < student_count; i++) {
|
||||
free(students[i].name);
|
||||
printf("Vysledky:\n");
|
||||
for (int i = 0; i < size; ++i) {
|
||||
printf("%d %s\n", database[i].votes, database[i].name);
|
||||
}
|
||||
free(students);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user