Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我得到以下代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <dirent.h>
#include <stdlib.h>
#define W 1031
#define B 256

struct position {
    int x;
    int y;
    struct position *next;
};

struct wordFiles {
    char *fileName;
    struct position *cor;
    struct wordFiles *next;
};

struct wordTree {
    char *word;
    struct wordFiles *files;
    struct wordTree *left;
    struct wordTree *right;
};

struct wordTree *hashTable[W];

typedef struct wordFiles *files_Ptr;

typedef struct position *pos_ptr;

typedef struct wordTree *wordTreePtr;

long int power(int a, long b){
    long int value,i;
    value = 1;
    for (i = 0; i < b; i++)
        value *= a;
    return value;
}

int hashValue (char *word){
    long int i=0,s=0,n;
    n = strlen(word);
    for (i=0; i<n; i++){
        s += power(B,n-i-1) * word[i];
    }
    return (s%W);
}

void readword(char *word , FILE *curr_file, int *x_axis, int *y_axis, int *newline, int *endfile){
    char c;
    c = (char) malloc(sizeof(char));
    if ((fscanf(curr_file, "%s", word))!=1 || fscanf(curr_file, "%c", &c)!=1){
        *endfile=1;
    }
    *x_axis += strlen(word);
    if (strlen(word)==1 && c=='\n'){
        *newline = 1;
        return;
    }
    if (c==' ') {
        *x_axis +=1;
    }
    else if (c=='\n') {
        *newline = 1;
    }
    return;
}

void coordinateslistInsert (pos_ptr *lp,int x, int y){
    pos_ptr prev,curr;
    prev = NULL;
    curr = *lp;
    while (curr != NULL){
        prev = curr;
        curr = curr->next;
    }
    pos_ptr n = (pos_ptr) malloc(sizeof(struct position));
    if (n == NULL) {
        printf("Out of memory\n");
        return;
    }
    n->next = NULL;
    n->x = x;
    n->y = y;
    if (prev==NULL) {
        *lp = n;
    }
    else {
        prev->next = n;
    }
    return;
}

void filelistInsert (files_Ptr *lp, char *filename, int x, int y, int k) {
    files_Ptr prev, curr;
    prev = NULL;
    curr = *lp;
    if ( curr!=NULL && k == 1 && strcmp(curr->fileName, filename) == 0 ){
        coordinateslistInsert(&(*lp)->cor, x, y);
        return;
    }
    while (curr != NULL){
        prev = curr;
        curr = curr->next;
    }
    files_Ptr n = (files_Ptr)malloc(sizeof(struct wordFiles));
    if (n == NULL) {
        printf("Out of memory\n");
        return;
    }
    n->fileName = filename;
    n->next = NULL;
    coordinateslistInsert (&(*n).cor , x ,y);
    if (prev==NULL) {
        *lp = n;
    }
    else {
        prev->next = n;
    }
    return;
}

void treeBalancedInsert (wordTreePtr *curr_tree, char *word, char *filename, int x, int y) {
    int k=0;
    if (*curr_tree == NULL) {
        *curr_tree =(wordTreePtr) malloc(sizeof(struct wordTree));
        if (*curr_tree == NULL) {
            printf("Out of memory\n");
            exit(1);
        }
        (*curr_tree)->word=malloc(30*sizeof(char));
        (*curr_tree)->left = (*curr_tree)->right = NULL;
        strcpy((*curr_tree)->word,word);
        filelistInsert (&(*curr_tree)->files , filename,x,y,k);
    }
    else {
        if (strcmp((*curr_tree)->word,word) == 0){
            k=1;
            filelistInsert (&(*curr_tree)->files , filename,x,y,k);
            return;
        }
        else if (strcmp((*curr_tree)->word,word) < 0)
            treeBalancedInsert(&(((*curr_tree)->left)), word, filename, x, y);
        else
            treeBalancedInsert(&(((*curr_tree)->right)), word, filename,x ,y);
    }

}

void search (char *word, int h_value, struct wordTree *hashtable[]){
    wordTreePtr n = hashTable[h_value];
    while(n!=NULL && strcmp ( n->word , word ) !=0){
        if (strcmp ( n->word , word ) > 0 ){
            n = n->right;
        }
        else if(strcmp ( n->word , word ) < 0){
            n = n->left;
        }
    }
    if (n==NULL){
        printf("NOT FOUND");
        return;
    }
    printf("%s\n",n->word);
    files_Ptr k = n->files;
    while (k!=NULL) {
        pos_ptr q = k->cor ;
        while (q!=NULL) {
            printf("%s(%d,%d)\n",k->fileName,q->y,q->x);
            q = q->next;
        }
        k = k->next;
    }
    return;
}


int main(int argc, char *argv[])
{
    int j,i;
    for (i=0; i<W; i++){
        hashTable[i] = NULL;
    }
    for (j=1; j<argc; j++){
        FILE *curr_file=fopen(argv[j], "r+");
        int h_value = 0, x_axis = 1, y_axis=1, newline=0,endfile=0;
        if (curr_file == NULL) {
            perror("Error: ");
            return (-1);
        }
        char *word=NULL , *filename;
        filename = (char *) malloc(30*sizeof(char));
        filename = argv[j];
        while (endfile!=1){
            word = (char *) malloc(20*sizeof(char));
            readword(word, curr_file, &x_axis, &y_axis, &newline, &endfile);
            h_value = hashValue(word);
            treeBalancedInsert(&hashTable[h_value], word, filename, x_axis-(unsigned)strlen(word)-1, y_axis);
            if (newline==1){
                y_axis +=1;
                x_axis=1;
                newline=0;
            }
        }
        fclose(curr_file);
        free(word);
    }
    char *wordToSearch;
    wordToSearch = (char *) malloc(20*sizeof(char));
    scanf("%s",wordToSearch);
    search(wordToSearch,hashValue(wordToSearch),hashTable);
    return 0;
}


它是在Mac上编写的,据说可以正常工作。但是当我编译并在我的机器上运行时,它就不会。
它的作用是将文本文件作为参数,并根据单词的哈希值将单词按二进制树排序,这些树放在哈希表中。然后您可以输入一个单词,它告诉您出现的坐标以及哪些文件。

无论如何,在eclipse上逐步调试停留在filelistInsert和代码块的(curr = curr-> next)上,这显示出另外两个问题,一个在treebalancedinsert函数上,它需要filelistinsert函数,另一个在主菜单上,它需要treebalancedinert功能。
我无法在filelistinsert中找到问题所在,而且时间紧迫。 (我知道这是一个可怕的问题,但我绝望了)

最佳答案

更改:

char c;
c = (char) malloc(sizeof(char));




char c;


注意:这是您的程序中的错误,但不能解释为什么程序崩溃。

别处:

    filename = (char *) malloc(30*sizeof(char));
    filename = argv[j];


这是内存泄漏,而且如果您然后假设filename30字符数组,则可能会遇到麻烦。

08-27 21:02