From ba8df4c6c8c2bdd3ef3002997e05fd2159655d7e Mon Sep 17 00:00:00 2001 From: Simon Moedl Date: Thu, 6 Nov 2025 00:03:29 +0100 Subject: [PATCH] noch bessere version --- Start_Windows/input.c | 20 ++++++++++---------- Start_Windows/main.c | 16 ++++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Start_Windows/input.c b/Start_Windows/input.c index 2d8778f..736270b 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -6,28 +6,28 @@ // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. -// Read words from file and store in 'words' array + int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { - unsigned int count = 0; - char line[MAX_LINE_LEN]; + unsigned int count = 0; // wie viele Wörter bisher gespeichert wurden + char line[MAX_LINE_LEN]; // Puffer für EINE gelesene Textzeile - while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount) + while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount) //fgets liest eine komplette Zeile wenn es noch eine Zeile gibt und noch Platz für mehr Wörter ist { - const char *delims = "\t\n\r ,;"; - char *token = strtok(line, delims); + const char *delims = "\t\n\r ,;"; //Liste der Trenner (z.B. Komma) + char *token = strtok(line, delims); //strok sucht in line das erste Wort, indem es an Trennzeichen teilt. Wenn kein Wort gefunden, wird NULL zurück gegeben - while (token != NULL && count < maxWordCount) + while (token != NULL && count < maxWordCount) //Solange es noch ein Wort (token) gibt und noch Platz in words ist { for(unsigned int i = 0; token[i] != '\0'; i++) { - token[i] = toupper((unsigned char)token[i]); + token[i] = toupper((unsigned char)token[i]); //toupper macht jeden Buchstaben groß. } - strncpy(words[count], token, MAX_WORD_LEN -1); + strncpy(words[count], token, MAX_WORD_LEN -1); //Kopiert das Wort in den nächsten freien Slot words[count]. words[count][MAX_WORD_LEN -1] = '\0'; - count++; + count++; //Wir habenein Wort abgespeichert count + 1 token = strtok(NULL, delims); //hier } diff --git a/Start_Windows/main.c b/Start_Windows/main.c index d685cb1..0240803 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -11,17 +11,17 @@ int main(int argc, char *argv[]) { int exitCode = EXIT_SUCCESS; - if (argc != 2) + if (argc != 2) //wenn keine zwei argumente gefunde -> alarm { fprintf(stderr, "Benutzung: %s \n", argv[0]); return EXIT_FAILURE; } - char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; + char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; //array mit eingelesenen wörtern unsigned int wordCount = 0; - FILE *file = fopen(argv[1], "r"); - if (!file) + FILE *file = fopen(argv[1], "r"); //schaut in die words.txt datei + if (!file) //wenn keine datei -> alarm { fprintf(stderr, "Konnte Datei %s nicht öffnen.\n", argv[1]); return EXIT_FAILURE; @@ -30,17 +30,17 @@ int main(int argc, char *argv[]) unsigned int placedWords = 0; char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; - wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); + wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); //holt sich aus readwords funktion die wortanzahl fclose(file); - placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); + placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); //holt sich anzahl der platzierten wörter von createwordsalad funktion - printf("\nWortsalat-Initialisierung\n"); + printf("\nWortsalat-Initialisierung\n"); //ganz viel printen printf("-------------------------\n"); printf("Gelesene Woerter: %u\n", wordCount); printf("Platzierte Woerter: %u\n", placedWords); - if (placedWords < wordCount) + if (placedWords < wordCount) //macht checker, ob alle wörter salatiert wurden printf("Hinweis: %u Woerter konnten nicht platziert werden.\n", wordCount - placedWords); printf("\nSpielfeld (%dx%d):\n\n", SALAD_SIZE, SALAD_SIZE);