pvjc25/du4/program.c
2025-03-20 22:17:09 +01:00

88 lines
2.1 KiB
C

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int compare_names(const void *a, const void *b){
return strcasecmp((const char*)a, (const char*)b);
}
int compare_int(const void* a, const void* b) {
return (*(int*)b - *(int*)a);
}
int main() {
char buffer[999];
int voting[99];
char names[99][50];
int count = 0;
while(fgets(buffer, sizeof(buffer), stdin) != NULL) {
int current_votes = 0;
char current_name[50] = "";
sscanf(buffer, "%d %[^\n]", &current_votes, current_name);
int is_contained = 0;
for (int i=0; i<count; i++) {
if (strcmp(names[i], current_name) == 0) {
voting[i] = voting[i] + current_votes;
is_contained = 1;
break;
}/*
else {
strcpy(names[count], current_name);
strcpy(voting[count], current_votes);
count++;
}*/
}
if (!is_contained) {
strcpy(names[count], current_name);
voting[count] = current_votes;
count++;
}
}
if (count == 0) {
return 1;
}
int sorted_voting[99];
char names_after_sorting[99][50];
char names_while_sorting[99][50];
int number_count = 0;
int name_index = 0;
int last_number = -1;
for (int i=0;i<count;i++) {
sorted_voting[i] = voting[i];
}
qsort(sorted_voting, count, sizeof(int), compare_int);
for (int i=0;i<count;i++) {
memset(names_while_sorting, 0, sizeof(names_while_sorting));
int names_loop_index = 0;
if (sorted_voting[i] == last_number) continue;
for (int l=0;l<count;l++) {
if (sorted_voting[i] == voting[l]) {
strcpy(names_while_sorting[names_loop_index], names[l]);
number_count++;
names_loop_index++;
}
}
qsort(names_while_sorting, number_count-name_index, sizeof(char[50]), compare_names);
for (int l=0;l<number_count-name_index;l++){
strcpy(names_after_sorting[name_index+l], names_while_sorting[l]);
}
name_index = number_count;
last_number = sorted_voting[i];
}
printf("Vysledky:\n");
for (int i=0;i<count;i++) {
//printf("%d %s-------\n", voting[i], names[i]);
printf("%d %s\n", sorted_voting[i], names_after_sorting[i]);
}
return 0;
}