-
Notifications
You must be signed in to change notification settings - Fork 0
/
filep2_3.cpp.save
191 lines (190 loc) · 4.58 KB
/
filep2_3.cpp.save
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class trie
{
public:
char data[27],key[27];
trie *link[27];
trie()
{
for(int i=0;i<27;i++)
{
link[i]=NULL;
data[i]='\0';
key[i]='\0';
}
}
};
struct file
{
char name[26];
int num[26];
};
void search(trie*);
void search_middle(trie*,char*);
int main()
{
char ch='y';//used in do while loop
file f;//for reading the names and array in file
trie *ptr=new trie;//used to traverse through trie
trie *node=new trie;//used as the root of the trie
trie *head=new trie;//used to traverse through trie
head=ptr=node;//assumes everything to the root
int i=0;//counter variable used for the indexing of letters in words
int j=0;//counter variable for loops
int c=0;//counter variable for the input of all the words in the database
int k=0;//index variable for words converted from ascii values to 0,1,2,.. etc..
int some=0;//counter variable to find the unknonw index of any word
int ascnum[26];//to store the ascii numbers-97 for every letters in the current given word
int len=0;//string length for the file name currently in use
int giv_len=0;//string length for the given name currently in use
char given[27];//input word
fstream file1;
file1.open("names.dat",ios::binary | ios::in);//opening the give file
k=given[0]-97;//index variable
for(c=0;c<3;c++)//loop for every input
{
ptr=head=node;//assigning everything to root
file1.read((char*) &f,sizeof(f));
len=strlen(f.name);
i=0;
strcpy(ptr->data,"empty");
while(f.name[i]!='\0')//creating the word in the existing trie
{
k=f.name[i]-97;
trie *temp=new trie;//used for assigning new address
for(j=0;j<=i;j++)//copying the data to node's data
temp->data[j]=f.name[j];
temp->data[j]='\0';
if(c==0)//condition for preventing coincidence
{
if(i==0)
node->link[k]=temp;
ptr->link[k]=temp;
ptr=temp;
}//end if
else
{
if(ptr->link[k]!=NULL)
ptr=ptr->link[k];
else
{
ptr=ptr->link[k]=temp;
if(i==0)
node->link[k]=temp;
}//end else
}//end else
++i;//goes to next letter of the existing word
}//end while loop
strcpy(ptr->key,f.name);//ptr->key contains the file words while others contain null in key
}//end for loop
do
{
cout<<"\nEnter the word to search:"; cin>>given;
while(given[i]!='\0')//converting every input to lowercase
{
given[i]=tolower(given[i]);
i++;
}//end while
k=given[0]-97;
ptr=head=node;
if(k>=0)
{
ptr=head=node;
i=0;
k=given[i]-97;
while(given[i]!='\0' && ptr->link[k]!=NULL)//traversing through the trie
{
k=given[i]-97;
ptr=ptr->link[k];
if(strcmp(ptr->data,"\0")==0)
break;
i++;
k=given[i]-97;
}//end while
file1.seekp(0,ios::beg);
if(strcmp(given,ptr->key)==0)
cout<<"Search:"<<ptr->data;
else if(i>2)
{
cout<<"Search"<<endl;
search(ptr);
}
else if(i<3)
{
cout<<"i<3"<<endl<<i<<endl;
//search_middle(node,given);
some=0;
len=strlen(given);
cout<<"Search:";
for(j=0;given[j];j++)
ascnum[j]=given[j]-97;
file1.seekp(0,ios::beg);
c=0;
while(file1.read((char *)&f,sizeof(f)))
{
giv_len=strlen(f.name);
k=0;
for(i=0;i<len;i++)
{
for(j=0;j<giv_len;j++)
{
if(f.num[i]==ascnum[j])
{
cout<<"i--"<<f.name[i]<<endl;
if(f.num[++i]==ascnum[++j])
{
cout<<"i++"<<f.name[i]<<endl;
++k;
++c;
break;
cout<<c<<"k-"<<k<<endl;
}
--i;
--j;
}
if(c>0)
break;
}
if(c>0)
file1.seekp(0,ios::beg);
break;
}
if(k>0)
cout<<f.name<<endl;
}
if(c==0)
cout<<"1Search not found!"<<endl;
}
else
cout<<"2Search not found!"<<endl;
}
else
cout<<"\n3Search not found!"<<endl;
}while(ch=='y');//end do while loop
file1.close();
}//end main
void search(trie *ptr)//function to print all the outputs
{
// cout<<"Search"<<endl;
int some=0,i;
for(i=0;i<26;i++)
if(ptr->link[i]!=NULL)
search(ptr->link[i]);
if(ptr->key[0]!='\0')
cout<<ptr->key<<endl;
}
/*void search_middle(trie *ptr,char *given)
{
cout<<"search_middle"<<endl;
int some=0,i=0;
for(i=0;i<=25;i++)
if(ptr->link[i]!=NULL)
search_middle(ptr->link[i],given);
if(strcmp(strstr(given,ptr->key),"\0")>1 || strcmp(strstr(given,ptr->key),"\0")<-1)
search(ptr);
}
*/