나는 Intro to C 클래스에 대한 프로그램을 작성하고 있습니다.이 프로그램을 사용하면 jumble puzzles를 해결하는 프로그램을 작성해야합니다 (아는 퍼즐은 신문에서 볼 수 있습니다). 교수가 준 사전 텍스트 파일. 사전에서 단어를 알파벳순으로 정렬하고 텍스트 파일 (jumble.txt라고 함)에서 뒤죽박죽을 들여 알파벳순으로 정렬 한 다음 문자열을 비교하여 일치하는 것을 찾습니다. 필자는 모든 코드를 작성했지만 즉시 실행하려고하면 충돌이 발생하며 이유를 파악할 수 없습니다. 아마도 Stackoverflow 사용자가 나를 도울 수 있을지도 모른다고 생각했습니다. 모든 도움에 미리Jumble 해결 프로그램이 충돌 할 때 충돌이 발생합니다.
#include <stdio.h>
#include <strings.h>
#define MAX_WORD_LEN 6
#define MAX_NUM_WORDS 30000
typedef struct {
char word[MAX_WORD_LEN+1];
char sort[MAX_WORD_LEN+1];
} jumble_type;
void bubblesort(char letters[], int length);
int main() {
int words, jumbles;
int j, q;
jumble_type list[MAX_NUM_WORDS];
jumble_type list2[MAX_NUM_WORDS];
// Creating file pointers
FILE *ifp;
FILE *ifp2;
//Opens Jumble and dictionary files and reads the info from them
ifp = fopen("jumbledct.txt", "r");
ifp2 = fopen("jumble.txt", "r");
//Assigns the value of "words" to the first line of jumbledct.txt
fscanf(ifp, "%d", words);
//Assigns the value of "jumbles" to the first line of jumble.txt
fscanf(ifp2, "%d", jumbles);
// Reads the words from the dictionary into the "word" section of our
// list structure.
int i;
for (i = 0; i < words; i++){
fscanf(ifp, "%s", &list[i].word);
strcpy(list[i].sort, list[i].word);
bubblesort(list[i].sort, strlen(list[i].sort));
printf("%s\n", list[i].sort);
}
//Reads from Jumble.txt
for (i = 0; i < jumbles; i++){
fscanf (ifp2, "%s", &list2[i].word);
strcpy(list2[i].sort, list2[i].word);
bubblesort(list2[i].sort, strlen(list2[i].sort));
//printf("%s\n", list2[i].sort);
}
for(j=0;j<jumbles; j++){
printf("JUMBLE PUZZLE # %d: %s\n", j+1, list2[j].word);
int x=0;
for (q = 0; q < words; q++){
if(strcmp(list2[j].sort, list[q].sort)==0){
printf("%s\n", list[q].word);
x++;
}
}
if (x == 0){
printf("Sorry, this puzzle has no solutions. \n\n");
}
printf("\n");
}
return 0;
}
void bubblesort(char letters[], int length) {
char temp;
int x, y;
for(x = 0; x < length; x++){
for (y = x; y < length; y++){
if (letters[x] > letters[y]){
temp = letters[y];
letters[y] = letters[x];
letters[x] = temp;
}
}
}
}
감사 :
여기 내가 가지고있는 코드입니다.
디버거를 사용하여 (또는 일부 printfs 추가) 충돌이 발생하는 곳을 파악하십시오. – John3136
귀하의 의견은 꺼져 있습니다; 당신은'int '가'jumbledct.txt'에서'단어들'로 스캔 될 수있는 것을 할당합니다. 다른 방법은 아닙니다. (또는 양자 택일로 '단어의 ** 이름 **을 ...을 (를)'읽을 수 있습니다.) 또한 @Nick Atoms는 정확합니다. * scanf 포인터를 전달해야합니다. – aib