2016-12-05 94 views
-1

译者所以我正在制作一个程序,它可以从键盘输入一个单词并输出西班牙语翻译,使用带有所有翻译的文件。现在我使用BST作为我的功能。在我的代码中,我使用strtok()来分解从文件输入的字符串。但是,每个单词前面都有四个随机字符。以下是我有...BST in C

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define SIZE 8001 

// BST code 
struct BSTnode{ 
    char engWord[128], spanWord[1000]; 
    struct BSTnode *left, *right; 
}; 

struct BSTnode *root = NULL; 

struct BSTnode *newBSTNode(char *engWord, char *spanWord){ 
    struct BSTnode *newNode; 
    newNode = (struct BSTnode*)malloc(sizeof(struct BSTnode)); 
    strcpy(newNode->engWord, engWord); 
    strcpy(newNode->spanWord, spanWord); 
    newNode->left = newNode->right = NULL; 
    return newNode; 
} 

void insert(char *engWord, char *spanWord){ 
    struct BSTnode *parent, *current, *newnode = NULL; 
    int res = 0; 
    if(root == NULL){ 
     root = newBSTNode(engWord, spanWord); 
     return; 
    } 
    for(current = root; current != NULL; 
    current = (res > 0)?current- >right:current->left){ 
     res = strcasecmp(engWord, current->engWord); 
     parent = current; 
    } 
    newnode = newBSTNode(engWord, spanWord); 
    res > 0?(parent->right = newnode):(parent->left = newnode); 
    return; 
} 

void findEngWord(char *str){ 
    struct BSTnode *temp = NULL; 
    int flag = 0, res = 0; 
    if(root == NULL){ 
     printf("FAIL!!!!!!"); 
     return; 
    } 
    temp = root; 
    int counter = 1; 
    while(temp){ 
     if((res = strcasecmp(temp->engWord, str)) == 0){ 
      printf("\t%s\n\t%d BST nodes", temp->spanWord, counter); 
      flag = 1; 
      break; 
     } 
     temp = (res > 0)?temp->left:temp->right; 
     counter++; 
    } 
    if(!flag) 
     printf("\t---NOT found (in BST)\n\t%d BST nodes", counter); 
    return; 

} 

void openFileBST(){ 
    // open file 
    FILE* filePnt = fopen("Spanish.txt", "r"); 
    char input[500], *first, *second; 

    // If file is invalid 
    if(filePnt == NULL){ 
     printf("Could not open file. Termination Program..."); 
     exit(0); 
    } 

    while(fgets(input, 500, filePnt) != NULL){ 
     first = strtok(filePnt, "\t"); 
     second = strtok(NULL, "\n"); 
     // Test prints 
     printf("%s\n", &first); 
     printf("%s\n", &second); 

     insert(&first, &second); 
    } 
    fclose(filePnt); 
} 

void search(){ 
    char *tempStr, exitStr = "-1"; 

    // Ask user to input word 
    printf("Enter a word you want to have translated. (type -1 to exit) 
     \n-------------------------\n"); 
    while(1){ 
     printf("\n- "); 
     fgets(tempStr, 99, stdin); 

     // Failed exit statement 
     //if(strcmp(tempStr, exitStr) == 0){ 
     // printf("test"); 
     // break; 
     //} 


     findEngWord(&tempStr); 
    } 
} 

int main() 
{ 
    openFileBST(); 
    search(); 
} 

这里是我的输出enter image description here

因为前四个字符中的一个图像我敢肯定,我不能正确搜索英文单词。这是我的strtok函数,还是我的代码?谢谢你们提前帮忙!

+0

请参阅[如何完成一个最小,完整和可验证的示例](/ help/mcve)。 –

+1

在'search'函数中,你有'char * tempStr'和'fgets(tempStr,99,stdin);'那将无法正常工作,因为'tempStr'在它被使用之前没有被初始化,所以它没有指向有效的记忆。要修复它,请将声明更改为'char tempStr [100]'。此外,对“findEngWord”的调用应该是“findEngWord(tempStr)”,不要使用&符号。 – user3386109

+0

_因为前四个字符,我很确定我不能正确地... ...输出不一致并不意味着搜索不起作用。如果您可以轻松测试,则不应假设。请不要发布你输出的截图。只需复制文本对任何读者来说都更方便。 – Gerhardh

回答

3

openFileBST函数中,变量firstsecond的类型为char *。现在,如果你得到一个指向这些变量的指针,例如&first,你会得到类型为char **的东西。与您的其他功能(或格式为"%s"printf)所期望的不同。

将变量传递给函数时删除运算符&的地址。

+0

我想这样做,并在程序崩溃。它甚至会让我的测试打印功能。 –

+0

@DylanForsyth可能因为你在调用findEngWord时在'search'函数中做了同样的事情。同样在'search'函数中,变量'exitStr'不是*指针变量,而是单个'char'。当然'tempStr'不是一个数组,而是一个未初始化的指针。所有这些都应该由编译器找到。它确实对你发出警告?你读过他们了吗? –

+0

啊我明白你的意思了。我从函数调用中删除了所有的地址操作符,但是当我在'openFileBST'中调用我的'insert'函数时,它会崩溃。我不确定它是否是我的其他函数,因为在它抛出适配之前它甚至不会调用“insert”函数。顺便再次感谢您的快速回答! –