pvjc25/du4/program.c

70 lines
1.6 KiB
C
Raw Normal View History

2025-03-11 13:39:10 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 256
#define MAX_NAME_LEN 100
typedef struct {
2025-03-11 13:40:18 +00:00
char name[MAX_NAME_LEN];
int votes;
2025-03-11 13:39:10 +00:00
} Student;
int compare_students(const void *a, const void *b) {
Student *student_a = (Student *)a;
Student *student_b = (Student *)b;
if (student_b->votes != student_a->votes) {
return student_b->votes - student_a->votes;
}
return strcmp(student_a->name, student_b->name);
}
int main() {
2025-03-11 13:40:18 +00:00
Student students[MAX_LEN];
int student_count = 0;
2025-03-11 13:39:10 +00:00
char line[MAX_LEN];
while (fgets(line, MAX_LEN, stdin)) {
int votes;
char name[MAX_NAME_LEN];
if (sscanf(line, "%d %[^\n]", &votes, name) != 2) {
if (student_count == 0) {
printf("CHYBA\n");
return 0;
}
break;
}
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].name, name) == 0) {
students[i].votes += votes;
found = 1;
break;
}
}
if (!found) {
if (student_count < MAX_LEN) {
strcpy(students[student_count].name, name);
students[student_count].votes = votes;
student_count++;
} else {
printf("CHYBA\n");
return 0;
}
}
}
printf("Vysledky:\n");
for (int i = 0; i < student_count; i++) {
printf("%d %s\n", students[i].votes, students[i].name);
}
return 0;
}