영어단어장....
헛매질
질문 제목 : c 트리로 영어단어장 만들기인데요
영어단어장을 구현해봤는데 뭐가 틀린지 모르겠어요 ㅠㅠ
아직 부가적인 몇개는 구현 안했는데요
신텍스오러는 안뜨는거 같은데 반복문을 2번이상 작동되면
오류뜨면서 안되요 ㅜㅜ
질문 내용 :
#includestdio.h
#includestdlib.h
#includestring.h
#define MAX_SIZE 100
typedef struct TreeNode
{
char word[MAX_SIZE];
char mean[MAX_SIZE];
struct TreeNode *left;
struct TreeNode *right;
}tree;
tree *searchWord(tree *Node, char *word);
tree *insertWord(tree *Node, char *word, char *mean);
tree *deleteWord(tree *Node, char *word);
tree *createTree();
void printWord(tree *Node, int recursive);
void initTree(tree *head);
tree *head = NULL;
int selectMenu()
{
int select;
scanf(%d, &select);
if(select == 1)
return 1;
else if(select ==2)
return 2;
else if(select ==3)
return 3;
else if(select ==4)
return 4;
else
return 5;
}
void main()
{
int input;
char word[MAX_SIZE];
char mean[MAX_SIZE];
initTree(head);
printf(1 : 영어 단어 및 단어의 뜻을 입력합니다.\n);
printf(2 : 영어 단어를 검색한 후 뜻과 철자의 수정여부를 묻습니다.\n);
printf(3 : 영어 단어 및 단어의 뜻을 삭제합니다.\n);
printf(4 : 현재 까지 입력된 영어단어를 모두 출력합니다.\n);
printf(5 : 프로그램을 종료합니다.\n);
while(1)
{
printf(기능을 입력하세요 : );
input = selectMenu();
switch(input)
{
case 1 :
{
//트리 노드 생성후 단어 및 단어의 뜻을 입력
printf(입력할 단어의 철자를 입력하세요 : );
scanf(%s, word);
printf(입력할 단어의 뜻을 입력하세요 : );
scanf(%s, mean);
head = insertWord(head, word, mean);
break;
}
case 2 :
{
//이진트리 탐색을 이용해 검색한 후 철자의 수정여부 물음
printf(검색할 단어의 철자를 입력하세요 : );
scanf(%s, word);
printWord(searchWord(head, word), 0);
break;
}
case 3 :
{
//이진트리 탐색을 이용해 해당단어를 찾은 후 삭제
printf(삭제할 단어의 철자를 입력하세요 : );
scanf(%s, word);
head = deleteWord(head, word);
break;
}
case 4 :
{
//이진트리 탐색을 이용해 입력된 모든 영어단어를 출력함.
printWord(head, 1);
break;
}
case 5 :
{
//프로그램 종료
exit(1);
break;
}
}
input = NULL;
}
}
void initTree(tree *head)
{
head = (tree*)malloc(sizeof(tree));
head-left = NULL;
head-right = NULL;
}
tree *searchWord(tree *Node, char *word)
{
if(Node)
{
int result = strcmp(Node-word, word);
if(result == 0)
return Node;
if(result 0)
return searchWord(Node-left, word);
else
return searchWord(Node-right, word);
}
else
return NULL;
}
tree *insertWord(tree *Node, char *word, char *mean)
{
if(Node == NULL)
{
Node = (tree*)malloc(sizeof(tree));
strcpy(Node-word, word);
strcpy(Node-mean, mean);
return Node;
}
else
{
int result = strcmp(Node-word, word);
if(result == 0)
{
printf(이미 존재하는 단어입니다.\n);
return Node;
}
else if(result 0)
{
Node-left = insertWord(Node-left, word, mean);
return Node;
}
else
{
Node-right = insertWord(Node-right, word, mean);
return Node;
}
}
}
tree *deleteWord(tree *Node, char *word)
{
if ( NULL == Node )
{
printf(삭제할 단어가 없습니다.\n);
return NULL ;
}
else
{
int Result = strcmp(Node-word, word) ;
if (Result == 0 )
{
tree* left = Node-left ;
tree* right = Node-right ;
free(Node ) ;
if ( NULL == left && NULL == right )
{
return NULL ;
}
else if ( NULL == left )
{
return right ;
}
else if ( NULL == right )
{
return left ;
}
else
{
tree* newnode = (tree*)malloc( sizeof(tree) ) ;
strcpy(newnode-word, left-word) ;
strcpy(newnode-mean, left-mean) ;
newnode-right = right ;
newnode-left = deleteWord(left, left-word ) ;
return newnurn newnode ;
}
}
else if (Result 0)
{
Node-left = deleteWord(Node-left, word ) ;
return Node ;
}
else
{
Node-right = deleteWord(Node-right, word ) ;
return Node ;
}
}
}
void printWord(tree *Node, int recursive)
{
if (Node == NULL ) return;
if (recursive == 1)
printWord(Node-left, recursive);
printf(단어 : %s\n,Node-word);
printf(뜻 : %s\n,Node-mean);
if (recursive == 1 )
printWord(Node-right, recursive);
}