-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordsearch.cpp
136 lines (119 loc) · 2.91 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
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <cctype>
#include "word.h"
#include "list.h"
#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(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}
int main(int argc, char* argv[])
{
string dir;
vector<string> files = vector<string>();
Word* head = NULL;
if (argc < 2) {
cout << "No Directory specified; Exiting ..." << endl;
return(-1);
}
dir = string(argv[1]);
if (getdir(dir,files)!=0) {
cout << "Error opening " << dir << "; Exiting ..." << endl;
return(-2);
}
for (unsigned int i = 0; i < files.size(); i++) {
if(files[i][0]=='.') continue; //skip hidden files
cout << "reading " << files[i] << "...";
ifstream fin((string(argv[1])+ "/" +files[i]).c_str()); //open using absolute path
// ...read the file..
string word;
while(fin >> word)
insert_file(insert_word(head, word), files[i]);
fin.close();
cout << "done" << endl;
}
string mode;
cout << "\nWelcome to The Dictionary\n";
while ( mode != "exit" ) {
cout << "Please enter a mode (Or exit to quit the program):\n";
cout << "(1) Search\n";
cout << "(2) Sort\n";
cout << "Input: ";
cin >> mode;
for ( char ch : mode ){
ch = tolower(ch);
}
if ( mode == "1" ) {
doSearch(head);
}
else if ( mode == "2" ) {
doSort(head);
}
}
cout << "\nThank you for using. Have a nice day." << endl;
return 0;
}
void doSearch(Word*& head) {
string keyword;
cout << "\nEnter word (Or exit to quit the mode): ";
cin >> keyword;
Word* iterW = find_word(head, keyword);
cout << endl;
if (iterW != NULL) {
File* iterF = iterW->file_ptr;
while (iterF != NULL) {
cout << iterF->filename << " " << iterF->count << endl;
iterF = iterF->next;
}
cout << "--------------" << endl;
cout << "WordTotal " << iterW->wordTotal << endl;
cout << "FileTotal " << iterW->fileTotal << endl;
cout << "Average " << iterW->wordTotal/iterW->fileTotal << endl;
} else {
cout << "word not found." << endl;
}
cout << endl;
}
void doSort(Word* head) {
int count;
cout << "\nHow many words? (Or exit to quit the mode): ";
try {
cin >> count;
cout << endl;
printNode(head, count);
cout << endl << endl;
}
catch (int e) {
cout << "\nInvalid number" << endl;
}
}
void printNode(Word * head, int& count) {
if (head == NULL)
return;
if (count > 0)
printNode( head->left, count );
if (count > 0){
cout << head->word << " ";
count--;
}
if (count > 0)
printNode( head->right, count );
}