したがって、このプログラムは、同じ長さの有効な単語を表示するために、単語をバラバラにすることになっています。これは、関数内で動的なメモリ割り当てを必要とする割り当てであり、かなり面倒でした。プログラムの最初に、長さ n の辞書にメモリを動的に割り当てる必要があります。与えられた辞書の長さは 10000 で、単語の最大長は 20 文字でした。各単語の順列の配列を動的に作成し、それらを辞書内の単語と strcmp を使用したバイナリ検索で比較することにしました (ユーザーが入力した単語と辞書に入力された単語はすべて大文字であり、これを実行可能な方法にしています)。プログラム全体は、最初の反復から、有効な単語である重複順列を差し引いて動作し、while ループが途中で終了しました。
その他の仕様:
- ユーザーは「y」または「Y」を入力して、プレイしたいことを示します
while ループが終了する理由が思いつかないため、これを解決するために何も試していません。特に、新しい値をスキャンすることが許可されていない場合でも、選択は「y」または「Y」でなければならないためです。
これがメインです。
int main() {
char** dictionary;
int num_words;
// defined FILE* outside of file read in function to avoid returning local variable address
FILE* ifp = fopen("Dictionary.txt", "r");
fscanf(ifp, "%d", &num_words);
dictionary = FileReadIn(num_words, ifp);
char choice = Welcome();
// main part of program Im unsure why it exits after its first iteration
while ((choice == 'y') || (choice == 'Y')) {
char* letters = LettersReadIn();
// Calculates number of permutations which is (numletters)!
long num_permutations = Factorial(strlen(letters));
// Sets up permutations through malloc
char** permutations = (char**)malloc((num_permutations) * sizeof(char*));
int i;
for (i = 0; i < num_permutations; i++) {
permutations[i] = (char*)malloc(MAXWORDLENGTH * sizeof(char));
}
// Creates all permutations of the letters entered in by the user in a recursive function
RecursivePermute(letters, 0, permutations);
// Created the WordIndices array in order to keep track of which indices in the permutations array are valid words
// Could not get the program to work when I created wordindices in Viable Words
// Viable Words checks each permutation against words in the dictionary using a binary search
// which compared each word lexicographically with strcmp
int* word_indices = (int*)malloc(num_permutations * sizeof(int));
ViableWords(permutations, dictionary, num_permutations, num_words, word_indices);
// Prints each index in permutations that is a valid word
// valid word indices stored in word indices
PrintWords(permutations, num_permutations, letters, word_indices);
// frees permutations and word indices
free(permutations);
free(word_indices);
choice = Welcome();
} // End While loop
return 0;
} // End main
そして、これがウェルカム機能です。
char Welcome() {
printf("Welcome to the Jumble Puzzle Solver!\n");
printf("Would you like to enter a jumbled word?\n");
char choice;
scanf("%c", &choice);
return choice;
}// End Welcome
したがって、本質的に私の質問は、なぜこれが発生するのか、そして解決策は何かということです。
他の情報が必要な場合はお知らせください。また、他に改善すべき点があればお知らせください。私はプログラミングにかなり慣れていないので、建設的な批判が大好きです。