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