-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchengine.cpp
232 lines (185 loc) · 5.45 KB
/
searchengine.cpp
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
#include <bits/stdc++.h>
using namespace std;
/// @brief -----------------just some forward declarations to avoid conflicts------------------------------
class linkedList;
class node;
struct ret_word;
linkedList* table[200]={nullptr};
vector<string> documents={"1.txt","2.txt","3.txt","4.txt"};
/*
---------------------------main definitions of classes---------------------------------------------
*/
class node{
public:
string data;
node* next;
set<string> files_found;
node(string data){
this->data=data;
next=NULL;
}
};
struct ret_word{
bool flag;
node* ret_ptr=NULL;
};
class linkedList{
public:
node* head;
node* tail;
linkedList(){
head=NULL;
tail=NULL;
}
void iathead(string data){
if(head==NULL){
head=new node(data);
tail=head;
}
else{
node* temp=new node(data);
temp->next=head;
head=temp;
}
}
void iatback(string data){
if(head==NULL){
head=new node(data);
tail=head;
}
else{
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=new node(data);
tail=temp->next;
}
}
void print_table(){
node* temp=head;
while(temp!=NULL){
cout << temp->data <<"-->";
temp=temp->next;
}
cout << "NULL";
}
};
//<--------------------------------ALL FUNCTION DECLARATIONS HERE------------------------------------------------>
ret_word search_word(string word);
void add_totable();
void print();
/**
* @brief ---------------------------indexer to put strings into the table-------------------------------------
*
* @param s
* @return int
*/
void add_totable(){
string s;
fstream myfile;
for(int i = 0 ; i < documents.size() ; i++){
myfile.open(documents[i],ios::in);
if(!myfile){
cout << "no such file exists \n";
}
else{
while(myfile >> s){
ret_word retword;
int sum=0;
for(int i=0;i<s.size();i++){
sum+=abs(toupper(s[i])-65);
}
int index=sum%200;
retword=search_word(s);
if(retword.flag==true && retword.ret_ptr->data==s){
table[index]->head->files_found.insert(documents[i]);
continue;
}
table[index]->iathead(s);
table[index]->head->files_found.insert(documents[i]);
}
}
myfile.close();
}
}
/**
* @brief to print the table--------------------------------------------------------------------------------------
*
*/
void print(){
for(int i=0;i<200;i++){
cout << i <<" ---> ";
if(table[i]->head!=NULL){
table[i]->print_table();
}
cout << "\n";
}
}
//----------------------------------------retriever here!!!!!---------------------------------------------------
ret_word search_word(string word){
ret_word retword;
for(int i = 0 ; i < documents.size() ; i++){
int sum=0;
for(int i=0;i<word.size();i++){
sum+=abs(toupper(word[i])-65);
}
int index=sum%200;
if(index>199 || index<0){
retword.flag=false;
return retword;
}
node* temp=table[index]->head;
while(temp!=NULL){
if(temp->data==word){
retword.flag=true;
retword.ret_ptr=temp;
return retword;
}
else{
temp=temp->next;
}
}
retword.flag=false;
return retword;
}
}
//-----------------------------------------the main func!!!------------------------------------------------------
int main(){
for(int i=0;i<200;i++){
table[i]=new linkedList();
}
add_totable();
int choice;
string user_enter;
cout << "what do you want to do ??\n\n";
cout << " 1 --> search a word \n\n 2 --> print table data structure \n\n 3 --> end the program \n\n ";
choice=-1;
while(choice!=3){
cout << "Enter a choice\n";
cin >> choice;
if(choice == 1){
cout << "enter a string\n";
cin >> user_enter;
ret_word r;
r=search_word(user_enter);
if(r.flag==true){
cout << "found in --> \n";
for(const auto& file:r.ret_ptr->files_found){
cout << file << " \n";
}
}
else {
cout << "not found in the database\n";
}
}
else if(choice == 2){
print();
}
else if(choice == 3){
choice=3;
}
}
cout << "Thank you for using me :) \n";
return 0;
}