Code:
#include <stdio.h>
#include<iostream.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
//#include <dos.h>
#include <windows.h>
#define LEFT 1
#define RIGHT 2
class node
{
public:
char word[20],meaning[100];
node *left,*right;
};
node *maketree(char[],char[]);
node* treefromfile();
void filefromtree(node*);
void addword(node*,char[],char[]);
void seperateword(char[],char[],char[]);
void displayall(node*);
node* bsearch(node*,char[]);
void showmenu();
FILE *file_ptr;
void prog()
{
char word[20],meaning[100];
int menuchoice;
node *temp;
temp=treefromfile();
if(temp==NULL)
{
cout<<"File does not exist or dictionary is empty...";
getch();
}
while(1)
{
showmenu();
cin>>menuchoice;
switch(menuchoice)
{
case 1:cout<<"Enter word ";
cin>>word;
cout<<"Enter meaning : " ;
flushall();
cout.flush();
gets(meaning);
if(temp==NULL)
temp=maketree(word,meaning);
else
addword(temp,word,meaning);
break;
case 2:if(temp==NULL)
cout<<"The dictionary is empty...";
else
{
cout<<"Find meaning of : ";
flushall();
gets(word);
node *t;
t=bsearch(temp,word);
if(t==NULL)
printf("Word not found...");
else
{
cout<< "t->word";
puts(t->meaning);
}
}
getch();
break;
case 3:if(temp==NULL)
printf("Dictionary is empty...");
else
displayall(temp);
getch();
break;
case 4:filefromtree(temp);
exit(1);
break;
default:cout<<"Enter Again";
Sleep(1000);
prog();
break;
}
}
}
void showmenu()
{
cout<<"\t\t\nCOMPUTER DICTIONARY"<<endl;
cout<<"\t[1]. Add a word."<<endl;
cout<<"\t[2]. Find meaning."<<endl;
cout<<"\t[3]. Display all."<<endl;
cout<<"\t[4]. Save and Close."<<endl;
//Enter Choice"()";
}
node* treefromfile()
{
node *ptree=NULL;
char word[20],meaning[100],str[120],*i;
int flags=0;
file_ptr=fopen("c:\\dict.anu","r");
if(file_ptr==NULL)
ptree=NULL;
else
{
while(!feof(file_ptr))
{
i=fgets(str,120,file_ptr);
if(i==NULL)
break;
seperateword(str,word,meaning);
if(flags==0)
{
ptree=maketree(word,meaning);
flags=1;
}
else
addword(ptree,word,meaning);
}
fclose(file_ptr);
}
return ptree;
}
node* maketree(char w[],char m[])
{
node *p;
p=new node;
strcpy(p->word,w);
strcpy(p->meaning,m);
p->left=NULL;
p->right=NULL;
return p;
}
void seperateword(char str[],char w[],char m[])
{
int i,j;
for(i=0;str[i]!=' ';i++)
{
w[i]=str[i];
w[i++]=NULL; //Append the null and skip the space.
}
for(j=0;str[i]!='\0';i++,j++)
{
m[j]=str[i];
}
m[j]=NULL;
}
void addword(node *tree,char word[],char meaning[])
{
node *p,*q;
p=q=tree;
while(strcmp(word,p->word)!=0 && p!=NULL)
{
q=p;
if(strcmp(word,p->word)<0)
p=p->left;
else
p=p->right;
}
if(strcmp(word,q->word)==0)
{
cout<<"This word already exists...";
Sleep(1000);
}
else if(strcmp(word,q->word)<0)
q->left=maketree(word,meaning);
else
q->right=maketree(word,meaning);
}
node* bsearch(node *tree,char word[])
{
node *q;
q=tree;
while(q!=NULL)
{
//p=q;
if(strcmp(word,q->word)<0)
q=q->left;
else if(strcmp(word,q->word)>0)
q=q->right;
if(strcmp(word,q->word)==0)
break;
}
return q;
}
void filefromtree(node *tree)
{
void travandwrite(node*);
file_ptr=fopen("c:\\dict.anu","w");
if(file_ptr==NULL)
{
cout<<"Cannot open file for writing data..."<<endl;
}
else //if(tree==NULL)
{
if(tree!=NULL)
{
travandwrite(tree);
}
fclose(file_ptr); //Close the file anyway.
}
}
void travandwrite(node *tree)
{
if(tree!=NULL)
{
fprintf(file_ptr,"%s,%s",tree->word,tree->meaning);
travandwrite(tree->left);
travandwrite(tree->right);
}
}
void displayall(node *tree)
{
if(tree!=NULL)
{
displayall(tree->left);
//cout<<"tree->word,tree->meaning";
displayall(tree->right);
}
}
void intro()
{
int i;
cout<<"DICTIONARY LOADING ..."<<endl;
for(i=0;i<50;i++)
{
cout<<"\tþþþ";
cout<<2*i<<"% completed"<<endl;
Sleep(150);
}
cout<<"\t\tDICTIONARY LOADING COMPLETED"<<endl;
}
void main()
{
intro();
prog();
}