2 Commits
Author SHA1 Message Date
Gade 1cb18630be Input Aufgaben bearbeitet 2026-10-27 21:51:27 +01:00
Gade 24466628da main.c ohne //a 2026-10-27 21:47:10 +01:00
3 changed files with 25 additions and 55 deletions

No files matched your search

+2 -54
View File
@@ -11,59 +11,7 @@
* restliche Felder mit zufälligen Buchstaben füllen */ * restliche Felder mit zufälligen Buchstaben füllen */
// Creates the word salad by placing words randomly and filling empty spaces // Creates the word salad by placing words randomly and filling empty spaces
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
const char words[][MAX_WORD_LEN], unsigned int wordCount)
{ {
srand(time(NULL));
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
salad[i][j] = 1;
}
}
int failed = 0;
for(int i = 0; i < wordCount; i++){
int wordlen = strlen(words[i]);
if(wordlen >= searchFieldLen){
failed++;
}
else{
if(rand() % 2){
//place horizontal
while(wordlen){
int row = rand() % searchFieldLen;
int col = rand() % (searchFieldLen - wordlen);
int j = 0;
for(; (j <= wordlen) && (salad[row][col + j] == 1); j++){}
if(j == wordlen + 1){
//word can fit
wordlen = 0;
strcpy(&(salad[row][col]), words[i]);
}
}
}
else{
//place vertical
while(wordlen){
int row = rand() % (searchFieldLen - wordlen);
int col = rand() % searchFieldLen;
int j = 0;
for(; (j <= wordlen) && (salad[row + j][col] == 1); j++){}
if(j == wordlen + 1){
//word can fit
for(int k = 0; k <= wordlen; k++){
salad[row + k][col] = words[i][k];
}
wordlen = 0;
}
}
}
}
}
//fill remaining spots with random chars
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
salad[i][j] = (salad[i][j] == 1) || (salad[i][j] == 0) ? (rand() % 25) + 65 : salad[i][j];
}
}
return wordCount - failed;
} }
+22
View File
@@ -8,5 +8,27 @@
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{ {
char line[MAX_LINE_LEN];
int count = 0;
while (fgets(line, MAX_LINE_LEN, file) != NULL &&
count < maxWordCount)
{
// Zeilenumbruch entfernen
line[strcspn(line, "\n")] = '\0';
// Wort ins Array kopieren
strncpy(words[count], line, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0';
// In Großbuchstaben umwandeln
for (int i = 0; words[count][i] != '\0'; i++)
{
words[count][i] = toupper((unsigned char)words[count][i]);
}
count++;
}
return count;
} }
+1 -1
View File
@@ -10,7 +10,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
//a
// Check if the correct number of arguments is provided // Check if the correct number of arguments is provided
if(argc != 2) if(argc != 2)
{ {