これは辞書のコードです。各ノードには 26 個のポインターなどがあります。
このコードは、Linux g++ コンパイラで約 270 語の挿入に対して機能しますが、Mac および Fedora の g++ でもセグメンテーション違反を示し、Visual Studio 2010 ではいくつかの例外処理エラーを示します。275 語を超える単語が自動的に挿入されると、セグメンテーション違反 (ubuntu )。
プログラムの効率については心配していません。使用されるtxtファイルには、それぞれ1行に英単語のリストが含まれています。
私はそれが機能する必要があります。前もって感謝します..
#define alphabets 26
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <string.h>
using namespace std;
class dictionary
{
//// letter node ////
struct node
{
char letter;
char * meaning;
bool completion;
struct node * pointer[alphabets];
}* root, * temp;
public:
char * word;
int length;
int letterPos;
int pointerId;
bool searchFound;
bool inserted;
//// constructor ////
dictionary()
{
root = new struct node;
root -> letter = '0';
root -> meaning = new char[9];
strcpy(root -> meaning, "rootNode");
root -> completion = 0;
}
//// called from localfunction ////
int insertWord(struct node *& currentNode)
{
if(currentNode == NULL && letterPos < length)
{
//cout << "creating new node" << endl;
//cout << "in insert function" << " " << word[letterPos] << endl;
temp = new struct node;
temp -> letter = word[letterPos];
currentNode = temp;
temp -> meaning = NULL;
//cout << "node address: " << currentNode << endl;
if(letterPos == length - 1)
{
temp -> completion = 1;
inserted = 1;
//cout << "inserted" << endl;
return 0;
}
else
temp -> completion = 0;
letterPos++;
pointerId = word[letterPos] - 'a';
insertWord(currentNode -> pointer[pointerId]);
}
else{
/*if(letterPos == length - 1)
{
cout << "word inserted" << endl;
currentNode -> completion = 1;
return 0;
}*/
//else
{
letterPos++;
pointerId = word[letterPos] - 'a';
insertWord(currentNode -> pointer[pointerId]);
}
}
if(letterPos == length)
{
//cout << "node letter: " << currentNode -> letter << endl;
currentNode -> completion = 1;
//cout << "inseted" << endl;
inserted = 1;
return 0;
}
/*if(currentNode -> letter == word[letterPos])
if(letterPos == length - 1)
{
cout << "in if condition completion" << endl;
currentNode -> completion = 1;
}
*/
//pointerId = word[letterPos] - 'a';
}
//// called from main ////
int insertIntoDictionary(char * wordNew)
{
length = strlen(wordNew);
word = wordNew;
letterPos = 0;
inserted = 0;
pointerId = word[letterPos] - 'a';
//insertWord(root);
//cout << "Root: " << root << endl;
insertWord(root -> pointer[pointerId]);
}
//// called from localfunction ////
int search(struct node * currentNode)
{
//cout << "in search function" << " searching " << word[letterPos] << endl;
if(currentNode == NULL)
{
//cout << "end of search" << endl;
return 0;
}
/*if(currentNode == root)
{
cout << "search function in root " << endl;
pointerId = word[letterPos] - 'a';
search(currentNode -> pointer[pointerId]);
return 0;
}*/
if(word[letterPos] == '\0' && !currentNode -> completion)
{
searchFound = 0;
return 0;
}
if(currentNode -> letter == word[letterPos] && word[letterPos] != '\0')
{
//cout << "in if condition" << endl;
if(letterPos == length - 1)
{
if(currentNode -> completion)
searchFound = 1;
return 0;
}
letterPos++;
pointerId = word[letterPos] - 'a';
//letterPos++;
search(currentNode -> pointer[pointerId]);
/*if(letterPos == length)
{
//cout << "word found" << endl;
searchFound = 1;
return 0;
}*/
}
/*else
{
pointerId = word[letterPos] - 'a';
//letterPos++;
search(currentNode -> pointer[pointerId]);
}
*/
}
//// called from main ////
int searchInDictionary(char * wordFind)
{
word = wordFind;
length = strlen(wordFind);
letterPos = 0;
searchFound = 0;
pointerId = word[letterPos] - 'a';
search(root -> pointer[pointerId]);
//if(searchFound)
//cout << "Word found" << endl;
}
int display1(struct node * currentNode)
{
int i;
if(currentNode != NULL)
{
//cout << "node letter1: " << currentNode -> letter << endl;
for(i = 0; i < alphabets; i++)
display1(currentNode -> pointer[i]);
}
}
/*int display()
{
cout << "display function" << endl;
//letterPos = 0;
//pointerId = word[letterPos] - 'a';
//cout << root ->
//display1(root -> pointer[pointerId]);
cout << "root: " << root << endl;
pointerId = word[letterPos] - 'a';
cout << root -> pointer[0] << endl; //-> letter;
display1(root);
}*/
};
int autoInsert(dictionary &english)
{
int count = 0;
//dictionary english;
FILE * file = fopen("words2.txt", "r");
char * line;
char ch;
/*while(fgets(line, sizeof line, file) != NULL)
{
cout << "Inserting: " << line << endl;
english.insertIntoDictionary(line);
}*/
line = new char[25];
const char * ptr = &ch;
while( count < 274 && ( ch = fgetc(file) ) != EOF )
{
if(ch != '\n')
strcat(line, ptr);
else
{
count++;
cout << "Inserting: " << line << endl;
english.insertIntoDictionary(line);
delete [] line;
line = new char[25];
}
}
return 0;
}
int main()
{
/*dictionary english;
char * word = "apple";
cout << "insert command" << endl;
english.insertIntoDictionary(word);
cout << "search command" << endl;
english.searchInDictionary(word);
*/
system("clear");
dictionary english;
int ch;
char w[100];
start:
cout << "1. insert word\n2. search\n3. auto insert\n4. exit " << endl;
cout << "Enter choice: ";
cin >> ch;
if(ch > 3)
return 0;
if(ch != 3)
{
cout << "word: ";
cin >> w;
}
switch(ch)
{
case 1: english.searchInDictionary(w);
if(english.searchFound)
{
cout << "word already exists" << endl;
break;
}
english.insertIntoDictionary(w);
if(english.inserted)
cout << "word inserted" << endl;
break;
case 2: english.searchInDictionary(w);
if(english.searchFound)
cout << "Word found" << endl;
else
cout << "Word not found" << endl;
break;
case 3: autoInsert(english);
break;
default: return 0;
}
goto start;
return 0;
}
ここに完全なコードがあります: http://www.heypasteit.com/clip/10XL
txt ファイルのリンク: http://www.heypasteit.com/clip/10XN