Module 9: Search the Library (C++ code only)

This commit is contained in:
David Doblas Jiménez 2021-07-19 22:15:11 +02:00
parent db47df67a7
commit e39354cb15
1 changed files with 125 additions and 49 deletions

174
a.c
View File

@ -8,53 +8,124 @@ using namespace std;
// For compiling C++ code
// g++ a.c -o a
class Library {
public:
void ComputeStats() {
assert(counts.empty());
counts.resize(18);
for (string s : words) {
int len = s.length();
if (len < 18) {
counts[len]++;
}
}
}
void PrintStats() const {
cout << "Here are the counts of each word length:\n";
for (int i=1; i<counts.size(); i++) {
cout << "[" << i << "] " << counts[i] << "\n";
}
}
string GetWord(int i) const {
assert(i >= 0 && i < words.size());
return words[i];
}
void ReadFromFile(string filename) {
ifstream f;
f.open(filename);
while (!f.eof())
{
string line;
getline(f, line);
// cout << line << "\n";
if (!line.empty())
{
int len = line.length();
if (line[len-1] == '\r') {
line = line.substr(0, len-1);
}
words.push_back(line);
}
}
cout << "Read " << words.size() << " words from file '"
<< filename << "'\n";
}
private:
vector<string> words;
vector<int> counts;
};
string ToUpper(string s)
{
string s2;
for (char c : s)
{
s2.push_back(toupper(c));
}
return s2;
}
struct Word
{
Word(string s)
{
word = s;
}
string word;
};
typedef vector<Word> Words;
const int num_buckets = 1001;
// hash function
int bucket(string s)
{
assert(!s.empty());
int i = 0;
for (char c : s)
{
i = (i * 217) + c;
}
i = abs(i);
int b = i % num_buckets;
assert(b >= 0 && b < num_buckets);
return b;
}
class Library
{
public:
Library()
{
shelves.resize(num_buckets);
}
bool IsWord(string s) const
{
for (Word w : shelves[bucket(s)])
{
if (s == w.word)
{
return true;
}
}
return false;
}
void ComputeStats()
{
assert(counts.empty());
counts.resize(18);
for (Word w : words)
{
int len = w.word.length();
if (len < 18)
{
counts[len]++;
}
}
}
void PrintStats() const
{
cout << "Here are the counts of each word length:\n";
for (int i = 1; i < counts.size(); i++)
{
cout << "[" << i << "] " << counts[i] << "\n";
}
}
string GetWord(int i) const
{
assert(i >= 0 && i < words.size());
return words[i].word;
}
void ReadFromFile(string filename)
{
ifstream f;
f.open(filename);
while (!f.eof())
{
string line;
getline(f, line);
// cout << line << "\n";
if (!line.empty())
{
line = ToUpper(line);
int len = line.length();
if (line[len - 1] == '\r')
{
line = line.substr(0, len - 1);
}
words.push_back(Word(line));
shelves[bucket(line)].push_back(Word(line));
}
}
cout << "Read " << words.size() << " words from file '"
<< filename << "'\n";
}
void DebugBuckets() const
{
for (int i = 0; i < shelves.size(); i++)
{
cout << "[" << i << "] " << shelves[i].size() << "\n";
}
}
private:
Words words;
vector<Words> shelves;
vector<int> counts;
};
struct Grid
{
@ -106,7 +177,7 @@ struct Grid
cout << " " << s << "\n";
}
}
string name; // string are initialized empty
string name; // strings are initialized empty
vector<string> lines;
};
@ -114,8 +185,13 @@ int main()
{
Library lib;
lib.ReadFromFile("top_12000.txt");
lib.ComputeStats();
lib.PrintStats();
//lib.ComputeStats();
//lib.PrintStats();
cout << lib.IsWord("DOG") << "\n";
cout << lib.IsWord("CAT") << "\n";
cout << lib.IsWord("THANKS") << "\n";
lib.DebugBuckets();
//Grid grid("MY GRID");
//grid.LoadFromFile("test");