_#include<iostream>
#include<string>
#include<fstream>
#include<windows.h>
using namespace std;
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void upper(string &a)
{
for(int i=0 ; i<a.length() ; i++)
if(a[i]<=122 && a[i]>=97)
a[i]=a[i]-32;
}
class trie
{
class node
{
protected:
friend class trie;
char data;
node *next;
bool wordEnd;
int sizeOfNext;
public:
node()
{
data='0';
next=NULL;
wordEnd=false;
sizeOfNext=0;
}
void addToNext(char toAdd)
{
if(sizeOfNext==0)
{
next=new node();
next->data=toAdd;
sizeOfNext++;
}
else
{
node* temp;
temp=new node[sizeOfNext];
for(int i=0 ; i<sizeOfNext ; i++)
temp[i]=next[i];
int size=sizeOfNext+1;
delete[] next;
next=new node[size];
for(int i=0 ; i<size-1 ; i++)
next[i]=temp[i];
sizeOfNext=size;
next[sizeOfNext-1].data=toAdd;
delete[] temp;
}
}
int searchInNext(char toSearch)
{
if(sizeOfNext==0)
return -1;
else
for(int i=0 ; i<=sizeOfNext ; i++)
if(toSearch==next[i].data)
return i;
return -1;
}
void searchAnyLast(string toSearch)
{
for(int i=0 ; i<sizeOfNext ; i++)
{
if(next[i].wordEnd)
{
toSearch.pop_back();
toSearch.push_back(next[i].data);
cout<<"Suggestion : "<<toSearch<<endl;
}
}
}
void searchSecondLast(string toSearch)
{
for(int i=0 ; i<sizeOfNext ; i++)
for(int j=0 ; j<next[i].sizeOfNext ; j++)
{
if(next[i].next[j].wordEnd)
if(next[i].next[j].data==toSearch[toSearch.length()-1])
{
char t=toSearch[toSearch.length()-1];
toSearch.pop_back();
toSearch.pop_back();
toSearch.push_back(next[i].data);
toSearch.push_back(t);
cout<<"Suggestion : "<<toSearch<<endl;
}
}
}
};
node* root;
public:
trie()
{
root=new node();
}
void addWord(string toAdd)
{
if(toAdd.size()==0)
return;
else
{
node* temp=root;
for(int i=0 ; i<toAdd.size() ; i++)
{
if(temp->searchInNext(toAdd[i])==-1)
temp->addToNext(toAdd[i]);
temp=&(temp->next[temp->searchInNext(toAdd[i])]);
}
temp->wordEnd=true;
}
}
void search(string toSearch)
{
node* temp=root;
for(int i=0 ; i<toSearch.size() ; i++)
{
if(temp->searchInNext(toSearch[i])!=-1)
{
if(toSearch.length()-i==2 && temp->next[temp->searchInNext(toSearch[i])].searchInNext(toSearch[i+1])!=-1)
{
if(temp->next[temp->searchInNext(toSearch[i])].next[temp->next[temp->searchInNext(toSearch[i])].searchInNext(toSearch[i+1])].wordEnd==false)
temp->searchSecondLast(toSearch);
}
else if(toSearch.length()-i==2 && temp->next[temp->searchInNext(toSearch[i])].searchInNext(toSearch[i+1])==-1)
{
temp->searchSecondLast(toSearch);
}
if(temp->next[temp->searchInNext(toSearch[i])].wordEnd==false && toSearch.length()-i==1)
{
cout<<"Word not found.\n";
temp->searchAnyLast(toSearch);
return;
}
temp=&(temp->next[temp->searchInNext(toSearch[i])]);
}
else if(toSearch.length()-i==1)
{
temp->searchAnyLast(toSearch);
return;
}
else if(toSearch.length()-i==2)
{
cout<<"Word not found.\n";
temp->searchSecondLast(toSearch);
}
else
{
cout<<"Word "<<toSearch<<" is not in the dictionary\n";
return;
}
}
if(temp->wordEnd)
cout<<"Word "<<toSearch<<" is in the dictionary\n";
else
cout<<"Word "<<toSearch<<" is not in the dictionary\n";
}
};
void readDictionary(trie &t)
{
string arg;
ifstream in;
in.open("Dictionary.txt");
if(in)
cout<<"Adding My own dictionary\n";
else
{
do
{
cout<<"OOPS !!! My Dictionary is missing. Can you please tell me the path to my file\n";
cin>>arg;
in.open(arg);
}
while(!in);
cout<<"Thank You \n";
}
string temp;
while(!in.eof())
{
in>>temp;
t.addWord(temp);
}
in.close();
}
void searchDictionary(trie &t)
{
cout<<"\t********************** Dictionary ************************\n";
cout<<"\n\n\n\t\tEnter Word\n\t\t*****************\n\t\t*\t\t*\n\t\t*****************";
gotoxy(17,6);
string temp;
cin>>temp;
upper(temp);
gotoxy(0,9);
t.search(temp);
}
void main()
{
trie dic;
readDictionary(dic);
char a;
do
{
system("cls");
searchDictionary(dic);
cout<<"\n\n\nDo you want to search another word : Y/N\n";
cin>>a;
}
while(a!='N' && a!='n');
}
#include<string>
#include<fstream>
#include<windows.h>
using namespace std;
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void upper(string &a)
{
for(int i=0 ; i<a.length() ; i++)
if(a[i]<=122 && a[i]>=97)
a[i]=a[i]-32;
}
class trie
{
class node
{
protected:
friend class trie;
char data;
node *next;
bool wordEnd;
int sizeOfNext;
public:
node()
{
data='0';
next=NULL;
wordEnd=false;
sizeOfNext=0;
}
void addToNext(char toAdd)
{
if(sizeOfNext==0)
{
next=new node();
next->data=toAdd;
sizeOfNext++;
}
else
{
node* temp;
temp=new node[sizeOfNext];
for(int i=0 ; i<sizeOfNext ; i++)
temp[i]=next[i];
int size=sizeOfNext+1;
delete[] next;
next=new node[size];
for(int i=0 ; i<size-1 ; i++)
next[i]=temp[i];
sizeOfNext=size;
next[sizeOfNext-1].data=toAdd;
delete[] temp;
}
}
int searchInNext(char toSearch)
{
if(sizeOfNext==0)
return -1;
else
for(int i=0 ; i<=sizeOfNext ; i++)
if(toSearch==next[i].data)
return i;
return -1;
}
void searchAnyLast(string toSearch)
{
for(int i=0 ; i<sizeOfNext ; i++)
{
if(next[i].wordEnd)
{
toSearch.pop_back();
toSearch.push_back(next[i].data);
cout<<"Suggestion : "<<toSearch<<endl;
}
}
}
void searchSecondLast(string toSearch)
{
for(int i=0 ; i<sizeOfNext ; i++)
for(int j=0 ; j<next[i].sizeOfNext ; j++)
{
if(next[i].next[j].wordEnd)
if(next[i].next[j].data==toSearch[toSearch.length()-1])
{
char t=toSearch[toSearch.length()-1];
toSearch.pop_back();
toSearch.pop_back();
toSearch.push_back(next[i].data);
toSearch.push_back(t);
cout<<"Suggestion : "<<toSearch<<endl;
}
}
}
};
node* root;
public:
trie()
{
root=new node();
}
void addWord(string toAdd)
{
if(toAdd.size()==0)
return;
else
{
node* temp=root;
for(int i=0 ; i<toAdd.size() ; i++)
{
if(temp->searchInNext(toAdd[i])==-1)
temp->addToNext(toAdd[i]);
temp=&(temp->next[temp->searchInNext(toAdd[i])]);
}
temp->wordEnd=true;
}
}
void search(string toSearch)
{
node* temp=root;
for(int i=0 ; i<toSearch.size() ; i++)
{
if(temp->searchInNext(toSearch[i])!=-1)
{
if(toSearch.length()-i==2 && temp->next[temp->searchInNext(toSearch[i])].searchInNext(toSearch[i+1])!=-1)
{
if(temp->next[temp->searchInNext(toSearch[i])].next[temp->next[temp->searchInNext(toSearch[i])].searchInNext(toSearch[i+1])].wordEnd==false)
temp->searchSecondLast(toSearch);
}
else if(toSearch.length()-i==2 && temp->next[temp->searchInNext(toSearch[i])].searchInNext(toSearch[i+1])==-1)
{
temp->searchSecondLast(toSearch);
}
if(temp->next[temp->searchInNext(toSearch[i])].wordEnd==false && toSearch.length()-i==1)
{
cout<<"Word not found.\n";
temp->searchAnyLast(toSearch);
return;
}
temp=&(temp->next[temp->searchInNext(toSearch[i])]);
}
else if(toSearch.length()-i==1)
{
temp->searchAnyLast(toSearch);
return;
}
else if(toSearch.length()-i==2)
{
cout<<"Word not found.\n";
temp->searchSecondLast(toSearch);
}
else
{
cout<<"Word "<<toSearch<<" is not in the dictionary\n";
return;
}
}
if(temp->wordEnd)
cout<<"Word "<<toSearch<<" is in the dictionary\n";
else
cout<<"Word "<<toSearch<<" is not in the dictionary\n";
}
};
void readDictionary(trie &t)
{
string arg;
ifstream in;
in.open("Dictionary.txt");
if(in)
cout<<"Adding My own dictionary\n";
else
{
do
{
cout<<"OOPS !!! My Dictionary is missing. Can you please tell me the path to my file\n";
cin>>arg;
in.open(arg);
}
while(!in);
cout<<"Thank You \n";
}
string temp;
while(!in.eof())
{
in>>temp;
t.addWord(temp);
}
in.close();
}
void searchDictionary(trie &t)
{
cout<<"\t********************** Dictionary ************************\n";
cout<<"\n\n\n\t\tEnter Word\n\t\t*****************\n\t\t*\t\t*\n\t\t*****************";
gotoxy(17,6);
string temp;
cin>>temp;
upper(temp);
gotoxy(0,9);
t.search(temp);
}
void main()
{
trie dic;
readDictionary(dic);
char a;
do
{
system("cls");
searchDictionary(dic);
cout<<"\n\n\nDo you want to search another word : Y/N\n";
cin>>a;
}
while(a!='N' && a!='n');
}
Comments and Questions |