-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordsearch.cpp
executable file
·199 lines (186 loc) · 4.56 KB
/
wordsearch.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
// Ryan Wiener, Porter Haet, 2/16/17
#include <vector>
#include <dirent.h>
#include <cctype>
#include "wordsearch.h"
using namespace std;
// Given a directory, return all the files in that directory
// if the directory does not exist -- report error.
int getdir (string dir, vector<string> &files)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error opening " << dir << endl;
return 1;
}
while ((dirp = readdir(dp)) != NULL) {
if (string(dirp->d_name)[0] != '.')
{
files.push_back(string(dirp->d_name));
}
}
closedir(dp);
return 0;
}
int main(int argc, char **argv)
{
if (argc <= 1)
{
cerr << "No directory inputted.\nUsage: ./wordsearch directory ...\n";
exit(1);
}
string dir;
vector<string> filenames;
dir = string(argv[1]);
if (getdir(dir,filenames)!=0) {
cout << "Error opening " << dir << "; Exiting ..." << endl;
return(-2);
}
ifstream fin;
word* root = NULL; // root of word tree
string next;
string slash = "/";
for (int i = 0; i < filenames.size(); i++)
{
string absoluteFile =(string(argv[1])+slash+filenames[i]);
fin.open(absoluteFile.c_str());
if (fin.fail())
{
cerr << "Couldn't open file " << filenames[i] << endl;
exit(1);
}
cout << filenames[i] << endl;
while (fin >> next)
{
for (int j = 0; j < next.length(); j++)
{
// gets rid of punctuation and makes lowercase
char ch = next[j];
if(ispunct(ch) || ch == '"' || isspace(ch) || isdigit(ch))
{
next.erase(j, 1);
j--;
}
else
{
next[j] = tolower(next[j]);
}
}
if (next == "") { continue; }
if (!root) {
root = new word(next);
}
//cout << next << endl;
word* currentWord = root->get(next);
if (currentWord) // checks if word was found
{
// cout << next << " found" << endl;
list<file>* filesWithWord = currentWord->getFiles();
file newFile(filenames[i], 1);
file* fileWithWord = filesWithWord->getDataPointer(newFile); // gets pointer to word in list or returns NULL if not found
if (fileWithWord) // checks if file was found
{
fileWithWord->setCount(fileWithWord->getCount() + 1); // increments count by 1 in file
}
else
{
currentWord->getFiles()->pushfront(newFile); // push new file object
}
currentWord->incrementTotalCount();
}
else
{
//cout << next << " not found" << endl;
word* newWord = new word(next);
// create new word object and append to list of words
file newFile(filenames[i], 1);
newWord->getFiles()->pushfront(newFile);
root->insert(newWord);
newWord->incrementTotalCount();
}
// cout << "printing ---------------" << endl;
// root->print();
// cout << "---------" << endl;
}
fin.close();
}
int mode = 0;
cout << "Enter mode (1 for word search, 2 for word sort, or 0 to quit): ";
cin >> mode;
while (mode < 0 || mode > 2)
{
cout << "Invalid mode entry" << endl;
cout << "Enter mode (1 for word search, 2 for word sort, or 0 to quit): ";
cin >> mode;
}
while (mode == 1 || mode == 2)
{
if (mode == 1)
{
string input;
cout << "Enter word (or exit to quit): ";
cin >> input;
if (input != "exit")
{
for (int i = 0; i < input.length(); i++)
{
// gets rid of punctuation and makes lowercase
if(ispunct(input[i]) || next[i] == '"')
{
input.erase(i, 1);
i--;
}
else
{
input[i] = tolower(input[i]);
}
}
word* currentWord = root->get(input);
if (currentWord) // checks to see if word was found
{
printResults(*currentWord);
}
else
{
cout << "No results found.\n";
}
/*
cout << "Enter word: ";
cin >> input;
*/
}
}
else
{
int numWords;
cout << "Enter how many words to print (or a negative number to quit): ";
cin >> numWords;
if (numWords >= 0)
{
root->print(numWords);
}
}
cout << "Enter mode (1 for word search, 2 for word sort, or 0 to quit): ";
cin >> mode;
while (mode < 0 || mode > 2)
{
cout << "Invalid mode entry" << endl;
cout << "Enter mode (1 for word search, 2 for word sort, or 0 to quit): ";
cin >> mode;
}
}
return 0;
}
void printResults(word current)
{
cout << "Filename Count" << endl;
list<file> files = *(current.getFiles());
for (int i = files.length() - 1; i >= 0; i--) {
file f = files.get(i);
cout << f.getFilename() << " " << f.getCount() << endl; // print out filename and count
}
cout << "Word Total " << current.getWordTotal() << endl;
cout << "File Total " << current.getFileTotal() << endl;
cout << "Average " << current.getWordAverage() << endl;
}