C语言中带BST的翻译器

C语言中带BST的翻译器,c,binary-search-tree,C,Binary Search Tree,所以我正在制作一个程序,可以从键盘上提取一个单词,并使用一个包含所有翻译的文件输出它的西班牙语翻译。现在我使用BST作为我的函数。在我的代码中,我使用strtok()来分解从文件输入的字符串。但是,每个单词前面都有四个随机字符。这是我所拥有的 #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 8001 // BST code struct BSTnode{

所以我正在制作一个程序,可以从键盘上提取一个单词,并使用一个包含所有翻译的文件输出它的西班牙语翻译。现在我使用BST作为我的函数。在我的代码中,我使用strtok()来分解从文件输入的字符串。但是,每个单词前面都有四个随机字符。这是我所拥有的

#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();
}
#包括
#包括
#包括
#定义尺寸8001
//BST代码
结构节点{
char engWord[128],spanWord[1000];
结构节点*左,*右;
};
struct BSTnode*root=NULL;
结构BSTnode*newBSTNode(char*engWord,char*spanWord){
结构BSTnode*newNode;
newNode=(struct BSTnode*)malloc(sizeof(struct BSTnode));
strcpy(newNode->engWord,engWord);
strcpy(newNode->spanWord,spanWord);
newNode->left=newNode->right=NULL;
返回newNode;
}
无效插入(字符*英语单词,字符*西班牙语单词){
结构BSTnode*parent,*current,*newnode=NULL;
int res=0;
if(root==NULL){
root=newBSTNode(英语单词、西班牙语单词);
回来
}
for(current=root;current!=NULL;
当前=(分辨率>0)?当前->右侧:当前->左侧){
res=strcasecmp(英语单词,当前->英语单词);
父项=当前;
}
newnode=newBSTNode(英语单词,西班牙语单词);
res>0?(父级->右=新节点):(父级->左=新节点);
回来
}
void findEngWord(char*str){
结构BSTnode*temp=NULL;
int标志=0,res=0;
if(root==NULL){
printf(“失败!!!!!!!”);
回来
}
温度=根;
int计数器=1;
while(临时){
如果((res=strcasecmp(temp->engWord,str))=0){
printf(“\t%s\n\t%d个BST节点”,temp->spanWord,计数器);
flag=1;
打破
}
温度=(分辨率>0)?温度->左:温度->右;
计数器++;
}
如果(!标志)
printf(“\t--未找到(在BST中)\n\t%d个BST节点”,计数器);
回来
}
void openFileBST(){
//打开文件
FILE*filePnt=fopen(“西班牙语.txt”,“r”);
字符输入[500],*第一,*第二;
//如果文件无效
if(filePnt==NULL){
printf(“无法打开文件。终止程序…”);
出口(0);
}
while(fgets(input,500,filePnt)!=NULL){
first=strtok(filePnt,“\t”);
second=strtok(空,“\n”);
//测试打印
printf(“%s\n”,&first);
printf(“%s\n”,秒(&s);
插入(&第一个,&第二个);
}
fclose(filePnt);
}
无效搜索(){
char*tempStr,exitStr=“-1”;
//要求用户输入单词
printf(“输入要翻译的单词。(键入-1退出)
\n-------------------\n“;
而(1){
printf(“\n-”);
fgets(tempStr,99,stdin);
//失败的退出语句
//if(strcmp(tempStr,exitStr)==0){
//printf(“测试”);
//中断;
//}
findEngWord(和tempStr);
}
}
int main()
{
openFileBST();
搜索();
}
这是我的输出图片


因为前四个字符,我很确定我不能正确地搜索英语单词。是我的strtok函数还是我的代码?提前谢谢你们的帮助

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


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

请参阅。在
搜索
函数中,您有
char*tempStr
fgets(tempStr,99,stdin)
无法正常工作,因为
tempStr
在使用前未初始化,因此它不指向有效内存。要修复它,请将声明更改为
char tempStr[100]
。另外,对
findEngWord
的调用应该是
findEngWord(tempStr)
而不带符号。由于前四个字符,我很确定我不能正确。。。输出中断并不意味着搜索不起作用。如果你能很容易地测试它,你就不应该假设它。请不要发布输出的屏幕截图。只是复制文本对任何读者来说都更方便。很抱歉,我不知道如何从命令提示符中复制文本。我尝试从tempStr的指针进行交换,它将遍历testprint语句,但在搜索提示下崩溃。谢谢你的回答!你的文本文件是什么样子的?我试着这么做,程序崩溃了。它甚至会进入我的测试打印功能。@DylanForsyth可能是因为调用
findEngWord
时,在
search
功能中也会这样做。在
搜索
功能中,变量
exitStr
不是指针变量,而是单个
char
。当然,
tempStr
不是数组,而是未初始化的指针。所有这些都应该被编译器找到。它对你发出警告了吗?你读了吗?啊,我明白你的意思了。我从函数调用中删除了所有的地址运算符,但是当我在
openFileBST
中调用我的
insert
函数时,它会崩溃。我不确定它是否是我的任何其他函数,因为它甚至不会在抛出拟合之前进行
insert
函数调用。顺便说一下,再次感谢您的快速回答@DylanForsyth如果发生崩溃,应该使用调试器找出崩溃的位置。当调试器捕捉到崩溃,并且您在代码中时,您可以检查变量及其值,看看它们是否正常。