C++ C++;Trie字典--丢失了两天
这是我的dictionary.h文件:C++ C++;Trie字典--丢失了两天,c++,trie,C++,Trie,这是我的dictionary.h文件: #ifndef DICTIONARY_H #define DICTIONARY_H class Dictionary { public: struct Node { std::string word; std::string definition; static const int ALPHABET_SIZE = 26;
#ifndef DICTIONARY_H
#define DICTIONARY_H
class Dictionary
{
public:
struct Node
{
std::string word;
std::string definition;
static const int ALPHABET_SIZE = 26;
Node* next[ALPHABET_SIZE];
};
typedef Node TrieNode;
typedef TrieNode* Trie;
void createTrie();
bool insertTrie(std::string word, std::string definition);
bool loadDictionary(std::string fileName);
bool lookup(std::string word);
void deleteTrie();
bool writeTrie(std::string fileName);
Dictionary();
~Dictionary();
};
#endif // DICTIONARY_H
以下是my dictionary.cpp文件:
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include "dictionary.h"
using namespace std;
Dictionary::Dictionary()
{
createTrie();
}
Dictionary::~Dictionary()
{
deleteTrie();
}
void Dictionary::createTrie()
{
static const int ALPHABET_SIZE = 26;
Node *TrieNode = new Node;
TrieNode->word = "";
TrieNode->definition = "";
for (int i = 0; i < ALPHABET_SIZE; i++)
{
TrieNode->next[i] = nullptr;
}
}
bool Dictionary::insertTrie(string word, string definition)
{
static const int ALPHABET_SIZE = 26;
Node *newNode = nullptr;
newNode = new Node;
for (int i = 0; i < word.length(); i++)
{
int letter = (int)word[i] - (int)'a';
newNode->word[letter];
newNode->definition;
for (int i = 0; i < ALPHABET_SIZE; i++)
{
newNode->next[i] = nullptr;
}
}
}
bool Dictionary::loadDictionary(string fileName)
{
string word;
string definition;
fstream dataFile;
dataFile.open(fileName);
cout << "Loading dictionary..." << endl;
if (dataFile)
{
getline(dataFile, word, ':');
getline(dataFile, definition);
insertTrie(word, definition);
while (!dataFile.eof())
{
getline(dataFile, word, ':');
getline(dataFile, definition);
insertTrie(word, definition);
}
dataFile.close();
return 0;
}
else
{
return 1;
}
}
bool Dictionary::lookup(string word)
{
for (int i = 0; i < word.length(); i++)
{
//char letter = (char)word[i] - (char)'a';
Node* tmp = TrieNode->word[i];
}
}
void Dictionary::deleteTrie()
{
;
}
bool Dictionary::writeTrie(string fileName)
{
fstream dataFile;
dataFile.open(fileName, fstream::out);
dataFile.close();
return 0;
}
#包括
#包括
#包括
#包括
#包括“dictionary.h”
使用名称空间std;
字典::字典()
{
createTrie();
}
字典::~字典()
{
deletetetrie();
}
void Dictionary::createTrie()
{
静态常数int字母表大小=26;
节点*三节点=新节点;
三元组->单词=”;
三节点->定义=”;
对于(int i=0;i下一个[i]=nullptr;
}
}
bool字典::insertTrie(字符串单词,字符串定义)
{
静态常数int字母表大小=26;
Node*newNode=nullptr;
newNode=新节点;
for(int i=0;i单词[字母];
新建节点->定义;
对于(int i=0;inext[i]=nullptr;
}
}
}
bool Dictionary::loadDictionary(字符串文件名)
{
字符串字;
字符串定义;
流数据文件;
打开(文件名);
cout文件名;
dic.createTrie();
loaded=dic.loadDictionary(文件名);
while(答案='y')
{
回答;
如果(答案='n')
{
回答;
如果(答案='y')
{
cout>文件名;
writed=dic.writeTrie(文件名);
返回0;
}
}
否则如果(答案='y')
{
cout>单词;
lookedup=dic.lookup(字);
}
其他的
单词[i]
真正令人烦恼的是,我不知道这个错误是否一定是我在这一点上做错了,或者我与指针的不断斗争是否意味着我在链的后面某个地方搞砸了。有助于指示行号,但是:Node*tmp=trinode->word[I];
Trinode
是一种类型,因此这没有意义
更不用说在createTrie()
中,您有一个与类型Node*trinode=new Node;
)同名的局部变量,这是命名约定如何帮助避免混淆的完美例子。大多数人会告诉您“大写为类型,小写为正数”(或其变体).将有助于指示行号,但:Node*tmp=trinode->word[i];
Trinode
是一种类型,因此这没有意义
更不用说在createTrie()
中,您有一个与类型Node*trinode=new Node;
)同名的局部变量,这是命名约定如何帮助避免混淆的完美例子。大多数人会告诉您“大写为类型,小写为正数”(或其变体).此代码中存在严重问题,但最重要的是不清楚代码的用途。您能否举例说明程序应如何运行?此代码中存在严重问题,但最重要的是不清楚代码的用途。您能否举例说明程序应如何运行?
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include "dictionary.h"
using namespace std;
int main()
{
string fileName;
string word;
char answer = 'y';
int loaded;
int written;
int lookedup;
Dictionary dic;
cout << "Please enter the name of the dictionary file: ";
cin >> fileName;
dic.createTrie();
loaded = dic.loadDictionary(fileName);
while (answer == 'y')
{
cout << "Would you like to look up a word? ";
cin >> answer;
if (answer == 'n')
{
cout << "Would you like to write the dictionary to a file? ";
cin >> answer;
if (answer == 'y')
{
cout << "Please enter the filename you wish to save it too: ";
cin >> fileName;
written = dic.writeTrie(fileName);
return 0;
}
}
else if (answer == 'y')
{
cout << "Enter a word: ";
cin >> word;
lookedup = dic.lookup(word);
}
else
cout << "Please select a valid option.";
}
return 0;
}