C++ 在g+工作+;ubuntu,但在visual studio、代码块和g++;在mac中
这是字典的代码。 每个节点有26个指针,以此类推 这段代码在LinuxG++编译器中用于大约270个单词的插入 但即使在mac和fedora中的g++上也显示了分段错误,并且在VisualStudio2010中显示了一些异常处理错误。 如果自动插入超过275个单词,则显示分段错误(ubuntu) 我不担心程序的效率。使用的txt文件包含一行中的英文单词列表 我只是需要它来工作。提前谢谢C++ 在g+工作+;ubuntu,但在visual studio、代码块和g++;在mac中,c++,C++,这是字典的代码。 每个节点有26个指针,以此类推 这段代码在LinuxG++编译器中用于大约270个单词的插入 但即使在mac和fedora中的g++上也显示了分段错误,并且在VisualStudio2010中显示了一些异常处理错误。 如果自动插入超过275个单词,则显示分段错误(ubuntu) 我不担心程序的效率。使用的txt文件包含一行中的英文单词列表 我只是需要它来工作。提前谢谢 #define alphabets 26 #include <iostream> #includ
#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;
}
定义字母表26
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类词典
{
////字母节点////
结构节点
{
字符字母;
字符*含义;
布尔完成;
结构节点*指针[字母表];
}*根,*温度;
公众:
字符*字;
整数长度;
int letterPos;
int pointerId;
布尔搜索发现;
布尔插入;
////建造师////
字典()
{
root=新结构节点;
根->字母='0';
根->含义=新字符[9];
strcpy(root->意思是“rootNode”);
根->完成=0;
}
////从localfunction调用////
int insertWord(结构节点*&当前节点)
{
if(currentNode==NULL&&lettpos//// letter node ////
struct node
{
char letter;
char * meaning;
bool completion;
struct node * pointer[alphabets];
node()
{
for(int i =0; i<alphabets;i++)
{
pointer[i]=NULL;
}
}
}* root, * temp;
///字母节点////
结构节点
{
字符字母;
字符*含义;
布尔完成;
结构节点*指针[字母表];
节点()
{
对于(int i=0;i这是函数autoInsert()的修改代码
int自动插入(字典和英语)
{
整数计数=0;
fstream文件(“123.txt”,ios::in);
char*line=NULL;
char ch;
行=新字符[25];
第[0]行=“\0”;
ch='\0';
char*ptr=&ch;
而(计数<1510&&file.getline(第25行,'\n'))
{
{
计数++;
您是否尝试使用调试器?您应该使用标准模板库类重新编码。将std::string用于字符串,将std::set用于字典。然后观察内存故障消失。words2.txt的格式是什么?第一行的第一个单词,第二行的下一个单词,因此我没有使用debuggeryour valgrind的报告证实了这个答案。在visual studio上使用它,但有其他问题。将发布修改后的代码。
int autoInsert(dictionary &english)
{
int count = 0;
fstream file("123.txt",ios::in);
char * line = NULL;
char ch;
line = new char[25];
line[0] = '\0';
ch='\0';
char * ptr = &ch;
while( count < 1510 && file.getline(line,25,'\n'))
{
{
count++;
cout << "Inserting: " << line << endl;
english.insertIntoDictionary(line);
}
}
file.close();
return 0;
}