如何在以下程序中保存单词数据库?我不知道要合并文件&指针 [英] How do I save the database of words in the following program? I dont know to combine files & pointers

查看:77
本文介绍了如何在以下程序中保存单词数据库?我不知道要合并文件&指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题存在于我在网上遇到的许多代码(同一字典程序)中。即使在其他网站上使用二叉树的人也无法正常运行。所以有兴趣和有思想的人请帮助我。如果这个问题得到解决,这将是一个很好的帮助

所以请帮忙,因为我必须在几天内提交我的项目...



我的尝试:



#include< stdio.h>

#include< ; stdlib.h>

#include< string.h>

#include< conio.h>

struct BSTnode {

char word [128],意思是[256];

struct BSTnode * left,* right;

};



struct BSTnode * root = NULL;

struct BSTnode * createNode(char * word,char *含义){

struct BSTnode * newnode;

newnode =(struct BSTnode *)malloc(sizeof(struct BSTnode));

strcpy(newnode-> word,word);

strcpy(newnode->含义,含义);

newnode-> left = newnode-> right = NULL;

返回newnode;

}

void insert(char * word,char *含义){

struct BSTnode * parent = NULL,* current = NULL,* newnode = NULL;

int res = 0;

if(!root){

root = createNode(word,含义);

返回;

}

for(current = root; current!= NULL; current =(res> 0)? current-> right:current-> left)

{

res = strcmp(word,current-> word);

if(res == 0)

{

printf(Duplicate entry !! \ n);

return;

}

父=当前;

}

newnode = createNode(word,含义);

res> 0? (parent-> right = newnode):( parent-> left = newnode);

return;

}



void deleteNode(char * str){

struct BSTnode * parent = NULL,* current = NULL,* temp = NULL;

int flag = 0 ,res = 0;

if(!root){

printf(BST不存在!! \ n);

返回;

}

当前= root;

而(1)

{

res = strcmp(current-> word,str);

if(res == 0)

break;

flag = res;

parent = current;

current =(res> 0)? current-> left:current-> right;

if(current == NULL)

return;

}

/ *删除叶节点* /

if(current-> right == NULL){

if(current == root&& current - > left == NULL){

免费(当前);

root = NULL;

返回;

}

else if(current == root)

{

root = current-> left;

免费(当前);

返回;

}

flag> 0? (parent-> left = current-> left):

(parent-> right = current-> left);

}

其他

{

/ *删除带有单个孩子的节点* /

temp = current-> right;

if(!temp-> left){

temp-> left = current-> left;

if(current == root){

root = temp;

免费(当前);

返回;

}

flag> 0? (parent-> left = temp):

(parent-> right = temp);

}

else

{

/ *删除有两个孩子的节点* /

struct BSTnode * successor = NULL;

while(1)

{

successor = temp-> left;

if(!successor-> left)

break ;

temp =继承人;

}

temp-> left = successor-> right;

successor-> left = current-> left;

successor-> right = current-> right;



if( current == root)

{

root = successor;

free(current);

return;

}

(旗帜> 0)? (parent-> left = successor):

(父母 - >右=继承人);

}

}

免费(当前);

返回;

}



void findElement(char * str ){

struct BSTnode * temp = NULL;

int flag = 0,res = 0;

if(root == NULL)

{

printf(二进制搜索树不在车站!! \ n);

返回;

}

temp = root;

while(temp){

if((res = strcmp(temp-> word,str) ))== 0){

printf(Word:%s,str);

printf(含义:%s,temp->含义) ;

flag = 1;

休息;

}

temp =(res> 0)? temp-> left:temp-> right;

}

if(!flag)

printf(找不到搜索元素二元搜索树\ n);

返回;



}



void inorderTraversal(struct BSTnode * myNode)

{

if(myNode)

{

inorderTraversal( myNode-> left);

printf(Word:%s,myNode-> word);

printf(含义:%s,myNode- >含义);

printf(\ n);

inorderTraversal(myNode-> right);

}

返回;

}



int main()

{clrscr();

int ch;

char str [128],意思是[256];

而(1)

{

printf(\ n1.1.Insertion\t2。删除\ n);

printf(3.搜索\t4.Traversal\\\
);

printf(5.退出\ n输入你的选择:);

scanf(%d,& ch);

getchar();

switch(ch){

案例1:

printf(Word to insert:);

fgets(str,100,stdin);

printf (含义:);

fgets(意思是256,stdin);

insert(str,含义);

break;

案例2:

printf(输入要删除的单词:);

fgets(str,100,stdin);

deleteNode(str);

break;

case 3:

printf(输入搜索词:);

fgets(str,100,stdin);

findElement(str);

break;

case 4 :

inorderTraversal(root);

break;

case 5:

exit(0);

默认值:

printf(你输错了选项\ n);

休息;

}

}

返回0;

This problem is present in many codes(of the same dictionary program) i encountered online. Even the ones using binary tree on other websites are not functioning properly. So interested and thoughtful people pls do help me out. It wud be a great help if this problem is solved
So pls do help as i have to submit my project in a few days...

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
struct BSTnode {
char word[128], meaning[256];
struct BSTnode *left, *right;
};

struct BSTnode *root = NULL;
struct BSTnode *createNode(char *word, char *meaning) {
struct BSTnode *newnode;
newnode = (struct BSTnode *)malloc(sizeof(struct BSTnode));
strcpy(newnode->word, word);
strcpy(newnode->meaning, meaning);
newnode->left = newnode->right = NULL;
return newnode;
}
void insert(char *word, char *meaning) {
struct BSTnode *parent = NULL, *current = NULL, *newnode = NULL;
int res = 0;
if (!root) {
root = createNode(word, meaning);
return;
}
for (current = root; current !=NULL; current = (res > 0) ? current->right : current->left)
{
res = strcmp(word, current->word);
if (res == 0)
{
printf("Duplicate entry!!\n");
return;
}
parent = current;
}
newnode = createNode(word, meaning);
res > 0 ? (parent->right = newnode) : (parent->left = newnode);
return;
}

void deleteNode(char *str) {
struct BSTnode *parent = NULL, *current = NULL, *temp = NULL;
int flag = 0, res = 0;
if (!root) {
printf("BST is not present!!\n");
return;
}
current = root;
while (1)
{
res = strcmp(current->word, str);
if (res == 0)
break;
flag = res;
parent = current;
current = (res > 0) ? current->left : current->right;
if (current == NULL)
return;
}
/* deleting leaf node */
if (current->right == NULL) {
if (current == root && current->left == NULL) {
free(current);
root = NULL;
return;
}
else if (current == root)
{
root = current->left;
free (current);
return;
}
flag > 0 ? (parent->left = current->left) :
(parent->right = current->left);
}
else
{
/* delete node with single child */
temp = current->right;
if (!temp->left) {
temp->left = current->left;
if (current == root) {
root = temp;
free(current);
return;
}
flag > 0 ? (parent->left = temp) :
(parent->right = temp);
}
else
{
/* delete node with two children */
struct BSTnode *successor = NULL;
while (1)
{
successor = temp->left;
if (!successor->left)
break;
temp = successor;
}
temp->left = successor->right;
successor->left = current->left;
successor->right = current->right;

if (current == root)
{
root = successor;
free(current);
return;
}
(flag > 0) ? (parent->left = successor) :
(parent->right = successor);
}
}
free (current);
return;
}

void findElement(char *str) {
struct BSTnode *temp = NULL;
int flag = 0, res = 0;
if (root == NULL)
{
printf("Binary Search Tree is out of station!!\n");
return;
}
temp = root;
while (temp) {
if ((res = strcmp(temp->word, str)) == 0) {
printf("Word : %s", str);
printf("Meaning: %s", temp->meaning);
flag = 1;
break;
}
temp = (res > 0) ? temp->left : temp->right;
}
if (!flag)
printf("Search Element not found in Binary Search Tree\n");
return;

}

void inorderTraversal(struct BSTnode *myNode)
{
if (myNode)
{
inorderTraversal(myNode->left);
printf("Word : %s", myNode->word);
printf("Meaning : %s", myNode->meaning);
printf("\n");
inorderTraversal(myNode->right);
}
return;
}

int main()
{clrscr();
int ch;
char str[128], meaning[256];
while (1)
{
printf("\n1. Insertion\t2. Deletion\n");
printf("3. Searching\t4. Traversal\n");
printf("5. Exit\nEnter ur choice:");
scanf("%d", &ch);
getchar();
switch (ch) {
case 1:
printf("Word to insert:");
fgets(str, 100, stdin);
printf("Meaning:");
fgets(meaning, 256, stdin);
insert(str, meaning);
break;
case 2:
printf("Enter the word to delete:");
fgets(str, 100, stdin);
deleteNode(str);
break;
case 3:
printf("Enter the search word:");
fgets(str, 100, stdin);
findElement(str);
break;
case 4:
inorderTraversal(root);
break;
case 5:
exit(0);
default:
printf("You have entered wrong option\n");
break;
}
}
return 0;

推荐答案

必须为遍历所有迭代的二叉树实现迭代器函数元素并将它们写入文件。



迭代器函数已经存在( inorderTraversal )。所以使用它作为基础:

You must implement an iterator function for the binary tree that iterates over all elements and writes them to a file.

The iterator function is already there (inorderTraversal). So use that as base:
void saveToFile(const char *fileName)
{
    if (root)
    {
        FILE *f = fopen(fileName, "w");
        if (f)
        {
            saveNodeToFile(root, f);
            fclose(f);
        }
    }
    return;
}

void saveNodeToFile(const struct BSTnode *myNode, FILE *f)
{
    if (myNode)
    {
        saveNodeToFile(myNode->left, f);
        fprintf(f, "%s %s\n", myNode->word, myNode->meaning);
        saveNodeToFile(myNode->right, f);
    }
    return;
}


这篇关于如何在以下程序中保存单词数据库?我不知道要合并文件&amp;指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆