scores
crispy-caesus crispy@crispy-caesus.eu
Mon, 19 Oct 2026 20:32:15 +0200
2 files changed,
104 insertions(+),
0 deletions(-)
A
scores/scores.txt
@@ -0,0 +1,26 @@
+90722 +16248 +10894 +10121 +8593 +7139 +4626 +4621 +4485 +3651 +2747 +2481 +989 +829 +775 +721 +710 +657 +579 +562 +555 +469 +431 +420 +309 +297
A
scores/top.c
@@ -0,0 +1,78 @@
+#include <stdio.h> +#include <stdlib.h> + +int read_input(char **bufferptr) { + FILE *fptr; + + fptr = fopen("scores.txt", "r"); + if (fptr == NULL) { + printf("ERROR: can't open file"); + return 0; + } + + fseek(fptr, 0L, SEEK_END); + size_t sz = ftell(fptr); + rewind(fptr); + *bufferptr = malloc(sz + 1); + if (*bufferptr == NULL) { + fclose(fptr); + printf("ERROR: couldn't allocate memory"); + return 0; + } + size_t bytes_read = fread(*bufferptr, 1, sz, fptr); + (*bufferptr)[bytes_read] = '\0'; + + fclose(fptr); + return sz; +} + +int total_score = 0; + +void pp(int position, int score) { + printf("Die Top %d haben %d gezählt und machen damit %f%% aus.\n", position, + score, (float)score / (float)total_score * 100); +} + +int main() { + char *buffer = NULL; + int sz = read_input(&buffer); + if (!sz) + return 1; + + int i = -1, buf2i = -1; + char *buffer2 = malloc(sz); + // read total score + while (1) { + i++, buf2i++; + if (buffer[i] == '\n') { + buffer2[buf2i] = '\0'; + total_score = atoi(buffer2); + break; + } + buffer2[buf2i] = buffer[i]; + } + + buf2i = -1; + int position = 0, score = 0; + + // read other values + while (1) { + i++, buf2i++; + if (i == sz) + break; + + if (buffer[i] == '\n') { + buffer2[buf2i] = '\0'; + score += atoi(buffer2); + buf2i = -1; + + position++; + if (position == 1 || position == 3 || position == 5 || position == 10) + pp(position, score); + + continue; + } + + buffer2[buf2i] = buffer[i]; + } +}