디버깅에 어려움을 겪고 있습니다. 구조를 인쇄하려고 할 때 나는 마지막 단어 만 얻습니다. 기억을 넘어서 쓰고 있습니까? 누군가 나를 도울 수 있습니까? insert_hash
에서왜 마지막 단어 만 인쇄합니까?
typedef struct hash_table_ {
void **order;
int *number_next_calls;
int *number_buckets;
int *buckets_size;
int *worst;
int *total;
float *average;
int (*hash_func)(char *);
data_el **buckets_array;
} hash_table, *Phash_table;
typedef struct data_{
char *key;
void *data;
struct data_ *next;
}data_el;
main(){
while ((c = getchar()) != EOF) {
if ((c == ' ') || (c == ',') || (c == '.') || (c == '!') || (c == '"') ||
(c == ':') || (c == '\n')) {
/* End of a word */
if (char_index) {
/* Word is not empty */
word[char_index] = '\0';
lower_case_word(word);
if(!find_hash(dictionary,word)){
insert_hash(dictionary,word,frequency[hash_function(word)]);
}
printf("%s\n", dictionary -> buckets_array[hash_function(word)] -> key);
printf("%d \n",hash_function(word));
frequency[hash_function(word)]++;
char_index = 0;
num_words++;
}
}else{
word[char_index++] = c;
}
}
/*This is when it prints*/
printf("%s\n", dictionary -> buckets_array[337] -> key);
printf("%s\n", dictionary -> buckets_array[532] -> key);
printf("%s\n", dictionary -> buckets_array[93] -> key);
}
int hash_function(char *word){
int sum,i;
i = 0;
sum = 0;
while(word[i] != '\0'){
sum = sum + word[i];
i++;
}
return sum%1000;
}
void insert_hash(Phash_table table, char *key, void *data){
int index;
data_el *p, *cur;
index = table -> hash_func(key);
/*Head insertion*/
if(table -> buckets_array[index] == NULL){
table -> buckets_array[index] = (data_el *)malloc(sizeof(data_el));
table -> buckets_array[index] -> data = data;
table -> buckets_array[index] -> next = NULL;
table -> buckets_array[index] -> key = key;
}else{
printf("%s",table -> buckets_array[index] -> key);
cur = table -> buckets_array[index];
p = (data_el *)malloc(sizeof(data_el));
p -> key = key;
p -> data = data;
p -> next = cur;
cur = p;
/*
table -> buckets_array[index] = cur;
*/
}
}
이것은 많은 코드이며, 프로그램의 특정 부분에 문제를 격리 시키십시오. 디버거를 사용하거나 명령문을 인쇄해야하며, 샘플 입력 및 출력 (예상 및 실제)을 제공해야합니다 우리는 문제를 더 잘 이해합니다. – amit
글쎄, 필자는 while 함수를 사용할 때 printf를 사용하여 저장중인 데이터를 보았고 방금 스캔 된 내용을 인쇄하고 있습니다. while 루프로 처리가 끝나면 printf ("% s \ n", 사전 -> buckets_array [337] -> key)를 인쇄 해 보았습니다. printf ("% s \ n", 사전 -> buckets_array [532] -> key); printf ("% s \ n", 사전 -> buckets_array [93] -> key);'그러나 스캔 한 내용 중 마지막으로 내게주는 것입니다. –
스타일주의 : 연산자 우선 순위가 유리한 경우가 있습니다. * table (> total) = * (table> total) + 1;''table-> total = * table-> total + 1; > total + = 1;''->'는 매우 빡빡합니다. – wildplasser