noch bessere version

This commit is contained in:
Simon Moedl 2025-11-06 00:03:29 +01:00
parent 78f631fefb
commit ba8df4c6c8
2 changed files with 18 additions and 18 deletions

View File

@ -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
}

View File

@ -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 <Pfad zur Wörterdatei>\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);