👍 g++ trie.cpp spellCheck.cpp
👍 ./a.out
dbg ARA
dbg A
👍 cat dictionary
ara are area
👍 cat trie.h
class Trie;
class TrieNonLeafNode {
public:
TrieNonLeafNode() {
}
TrieNonLeafNode(char);
private:
bool leaf, endOfWord;
char *letters;
TrieNonLeafNode **ptrs;
};
class Trie {
public:
Trie() : notFound(-1) {
}
Trie(char*);
private:
TrieNonLeafNode *root;
const int notFound;
};
👍 cat trie.cpp
#include <iostream>
#include "trie.h"
using namespace std;
TrieNonLeafNode::
TrieNonLeafNode(char ch) {
cout << "dbg " << ch << endl;
ptrs = new TrieNonLeafNode*;
letters = new char[2];
if (ptrs == 0 || letters == 0) {
cerr << "Out of memory3.\n";
exit(1);
}
leaf = false;
endOfWord = false;
*ptrs = 0;
*letters = ch;
*(letters + 1) = '\0';
}
Trie::Trie(char* word) : notFound(-1){
cout << "dbg " << word << endl;
root = new TrieNonLeafNode(*word);
}
👍 cat spellCheck.cpp
#include <iostream>
#include <fstream>
#include "trie.h"
using namespace std;
char* strupr(char *s) {
for (char *ss = s;
(*ss = toupper(*ss)); ss++);
return s;
}
int main(int argc, char* argv[]) {
char fileName[25], s[80];
ifstream dictionary("dictionary");
if (dictionary.fail()) {
cerr << "Cannot open 'dictionary'"
<< endl;
exit(-1);
}
dictionary >> s;
Trie trie(strupr(s));
return 0;
}
ch7.4 Case Study: Spell Checker p373/391 of