gpt4 book ai didi

c++ - 在 g++ ubuntu 中工作,但在 visual studio 中显示段错误,在 mac 中显示代码块和 g++

转载 作者:行者123 更新时间:2023-11-28 07:20:33 25 4
gpt4 key购买 nike

这是字典的代码。每个节点有26个指针等等。

此代码适用于 linux g++ 编译器中的大约 270 个单词插入但即使在 mac 和 fedora 中的 g++ 上也显示段错误,并在 visual studio 2010 中显示一些异常处理错误。如果自动插入超过 275 个单词,则显示段错误(ubuntu)。

我不担心程序的效率。并且使用的 txt 文件包含一个英文单词列表,每个单词都在一行中。

我只是需要它来工作。提前致谢..

#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

最佳答案

发生这种情况可能是因为在创建新节点时指针 [alphabets] 没有得到初始化。试试这个结构节点的定义

//// 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;

关于c++ - 在 g++ ubuntu 中工作,但在 visual studio 中显示段错误,在 mac 中显示代码块和 g++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19545053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com