-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
286 lines (230 loc) · 7.06 KB
/
1.cpp
File metadata and controls
286 lines (230 loc) · 7.06 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<dirent.h>
//limited the number of words that the program can handle
#define MAX_SIZE 100000
//limited the number of files the program can handle
#define MAX_FILES 10
using namespace std;
//function used for sorting ====================================================
int comparator(const void *p,const void *q)
{
const int *a = (const int *)p;
const int *b = (const int *)q;
return(a[1]-b[1]);
}
//==============================================================================
//basic structure of the linked list ===========================================
typedef struct node{
char *data;
int freq[MAX_FILES][2];// 0 index for file , 1 for frequency
struct node *next;
}node;
//==============================================================================
DIR *dpdf;
struct dirent *epdf;
int words=0,currentfile=0;
node table[MAX_SIZE];
string stored[MAX_SIZE];
//hash function ================================================================
long int hash(char *st)
{
long int hashvalue=0;
int c;
char *str = &st[0];
while(c=*str)
{
hashvalue = c + hashvalue<<6 + hashvalue<<16 - hashvalue;
str++;
}
return hashvalue%10003;
}
//==============================================================================
//main function to execute the program =========================================
int main()
{
//int num = filenames.size();
int i,val,len,j,num;
string locationname[MAX_FILES];
string userstring;
char *pointertostring,c;
std::cout<<"Getting all files in the desired directory.....\n";
//opening the current directory
dpdf = opendir("./");
vector<string> tobeopen;
vector<string> filenames;
//storing all file names in a vector ===========================================
if (dpdf != NULL)
{
//cout<<"gg"<<endl;
while (epdf = readdir(dpdf))
{
filenames.push_back(epdf->d_name);
//cout<<epdf->d_name<<endl;
}
}
else
return 0;
//==============================================================================
//cout<<tobeopen.size()<<endl;
num = filenames.size();
i=0;
//storing file names having '.txt' format in another vector ====================
while(i<num)
{
string fname = filenames[i];
len = strlen((char *)&fname[0]);
j=0;
while(j+3<len)
{
if(fname[j]=='.'&&fname[j+1]=='t'&&fname[j+2]=='x'&&fname[j+3]=='t'&&fname[j+4]!='~')
{
tobeopen.push_back(fname);
}
j++;
}
i++;
}
cout<<tobeopen.size();
//==============================================================================
//cout<<(tobeopen.size())<<endl;
for(i=0;i<MAX_SIZE;i++) table[i].next=NULL;
//==============================================================================
//******************************************************************************
//start of while loop to read files one by one from directory ==============
while(currentfile<(tobeopen.size()))
{ // for reading file from the directory vector: tobeopen is used
string finame = tobeopen[currentfile];
cout<<endl<<finame<<" ";
ifstream fin;
fin.open(finame.c_str(),ios::in);
bool check=false;
//while loop for reading the characters in a file ----------------------
while(!fin.eof())
{
string g;
check = false;
//cout<<c;
fin.get(c);
while((c>=97&&c<=122)||(c>=65&&c<=90)||(c>=48&&c<=57)&&!fin.eof())
{
// cout<<c;
if(c>=65&&c<=90) c+=32;
check=true;
g+=c;
fin.get(c);
}
//if(check) {g+='\0';cout<<g<<" ";}
if(check)//while(fin>>g&&fin.is_open())
{
g+='\0';
cout<<g<<" ";
stored[words]=g;
len = strlen(&g[0]);
val = hash((char *)&g[0])%7;
//cout<<val<<" "<<g<<endl;
pointertostring = (char *)&stored[words][0];
//form a new node if the place corresponding to the hash value
//obtained for the word is empty
if(table[val].next==NULL)
{
table[val].freq[currentfile][0] = currentfile;
table[val].freq[currentfile][1] = 1;
table[val].data = pointertostring;
//table[val].freq = 1;
node* temp = new node;
temp->next=NULL;
table[val].next=temp;
words++; //go to next word of the file
}
//go to the corresponding node of the linked list if there
//already exists a word corresponding to the obtained hash value
else
{
node *f;
bool check=true;
f = &table[val];
//check if the word is already present in the linked list
//by traversing it from the root
//if the is word already present in the linked list
//corresponding to the hash value, increase the frequency
//corresponding to the file name that is being traversed
while(f->next!=NULL)
{
if(strcmp(f->data,pointertostring)==0)
{
f->freq[currentfile][0]= currentfile;
f->freq[currentfile][1]++;
//(f->freq)++;
check = false;
}
f=f->next;
}
//if the word is not present then create another node in
//the same linked list with the new word and update its
//frequency corresponding to the file being traversed
if(check)
{
f->data = strdup(pointertostring);
//f->freq = 1;
f->freq[currentfile][0] = currentfile;
f->freq[currentfile][1] = 1;
node* temp = new node;
temp->next=NULL;
f->next = temp;
words++; //go to next word of the file
}
}
}
}
// end of while loop for reading characters ----------------------------
//go to next file of the directory
currentfile++;
}
//end of while loop for reading files from the directory ====================
//******************************************************************************
cout<<" size of total files "<<currentfile<<endl;
char choice;
bool check;
//==============================================================================
while(1)
{
cout<<"Want to enter string?\n";
cin>>choice;
if(choice=='n'||choice=='N')
break;
//getting keyword from user
cout<<"Enter a string to search for"<<endl;
cin>>userstring;
//assigning a pointer to the variable in which user input is stored
pointertostring = (char *)(&userstring[0]);
val = hash(pointertostring)%7;
node *f = &table[val];
check = true;
while(f->next!=NULL)
{
if(strcmp(f->data,pointertostring)==0)
{
cout<<"found ";
qsort(f->freq,currentfile,2*sizeof(int),comparator);
for(i=0;i<currentfile;i++)
{
if(f->freq[i][1]>0)
cout<<"File name is "<<tobeopen[f->freq[i][0]]<<" Frequency is "<<f->freq[i][1]<<endl;
}
check = false;
break;
}
f=f->next;
}
//give an output when no file containes the searched keyword
if(check)
{
cout<<"Not found \n";
}
}
//==============================================================================
return 0;
}
/*############################## END OF CODE #################################*/