我的程序运行后崩溃
my program crashes after running
这是我的代码,基本上我要做的是让我的程序从我准备的文本文件中提取单词,并将其传递给我的程序,计算唯一单词的数量,然后打印出带有计数的唯一单词。我已经清除了所有错误,但现在我被卡住了,因为每次我尝试运行它时它都会崩溃。如果有人能告诉我在上哪里做错了,我将不胜感激
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
class BST
{
public:
BST ();
void insert (char*);
void printBST () const;
bool findNode (char*) const;
private:
struct Node;
typedef Node* NodePtr;
struct Node
{
char *word;
int count;
NodePtr left, right;
};
NodePtr root;
int compareVP (char*, char*) const;
void insert (NodePtr&, char*);
void inorderPrint (NodePtr) const;
bool findNode (NodePtr, char*) const;
};
int main ()
{
BST t;
char word[25];
ifstream readfile("infile.txt");
if(!readfile)
{
cout << "File could not be opened/found";
return 0;
}
while(readfile>> word)
{
t.insert(word);
}
if(readfile.eof())
t.printBST ();
}
BST::BST ()
{
root = NULL;
}
void BST::insert (char* word)
{
insert (root, word);
}
void BST::printBST () const
{
inorderPrint (root);
}
bool BST::findNode (char* word) const
{
return findNode (root, word);
}
int BST::compareVP (char* item1, char* item2) const
{
char* value1 = item1;
char* value2 = item2;
if (strcmp(value1,value2)==0)
return 0;
else if (strcmp(value1,value2)>0)
return 1;
else
return -1;
}
void BST::insert (NodePtr& root, char* word)
{
if (root == NULL)
{
NodePtr temp = new Node;
temp -> word = word;
temp -> left = NULL;
temp -> right = NULL;
root = temp;
}
else if (compareVP (root -> word, word) > 0)
insert (root -> left, word);
else if (compareVP (root -> word, word) < 0)
insert (root -> right, word);
else if (compareVP (root -> word, word) == 0)
root -> count++;
}
void BST::inorderPrint (NodePtr root) const
{
cout << "WordtCountn";
if (root != NULL)
{
inorderPrint (root -> left);
cout << root -> word << "t";
cout << root -> count << "n";
inorderPrint (root -> right);
}
else
cout << endl;
}
bool BST::findNode (NodePtr root, char* word) const
{
if (root == NULL)
return false;
else
{
int k = compareVP (root -> word, word);
if (k == 0)
return true;
else if (k > 0)
return findNode (root -> left, word);
else
return findNode (root -> right, word);
}
}
这里有一个问题:
char* word; // Pointer contains reference to undef areal of memory
while(readfile >> word) // you trying save line to undef area - result is unpredictable
第二个问题:
在函数insert()中,您只需附加到节点指针,并在更高级别上重用该指针。
可能的简单解决方案:
char word[1000];
while(readfile >> word)
{
t.insert(strdup(word));
}
更改了打印方法。以及用于插入的线。
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
class BST
{
public:
BST ();
void insert (char*);
void printBST () const;
bool findNode (char*) const;
private:
struct Node;
typedef Node* NodePtr;
struct Node
{
char *word;
int count;
NodePtr left, right;
};
NodePtr root;
int compareVP (char*, char*) const;
void insert (NodePtr&, char*);
void inorderPrint (NodePtr) const;
bool findNode (NodePtr, char*) const;
};
int main ()
{
BST t;
char word[25];
ifstream readfile("infile.txt");
if(!readfile)
{
cout << "File could not be opened/found";
return 0;
}
while(readfile>> word)
{
t.insert(strdup(word));
}
if(readfile.eof())
t.printBST ();
}
BST::BST ()
{
root = NULL;
}
void BST::insert (char* word)
{
insert (root, word);
}
void BST::printBST () const
{
cout << "WordtCountn";
inorderPrint (root);
}
bool BST::findNode (char* word) const
{
return findNode (root, word);
}
int BST::compareVP (char* item1, char* item2) const
{
char* value1 = item1;
char* value2 = item2;
if (strcmp(value1,value2)==0)
return 0;
else if (strcmp(value1,value2)>0)
return 1;
else
return -1;
}
void BST::insert (NodePtr& root, char* word)
{
if (root == NULL)
{
NodePtr temp = new Node;
temp -> word = word;
temp -> left = NULL;
temp -> right = NULL;
temp -> count = 1;
root = temp;
}
else if (compareVP (root -> word, word) > 0)
insert (root -> left, word);
else if (compareVP (root -> word, word) < 0)
insert (root -> right, word);
else if (compareVP (root -> word, word) == 0)
root -> count++;
}
void BST::inorderPrint (NodePtr root) const
{
if (root != NULL)
{
inorderPrint (root -> left);
cout << root -> word << "t";
cout << root -> count;
inorderPrint (root -> right);
}
else
cout << endl;
}
bool BST::findNode (NodePtr root, char* word) const
{
if (root == NULL)
return false;
else
{
int k = compareVP (root -> word, word);
if (k == 0)
return true;
else if (k > 0)
return findNode (root -> left, word);
else
return findNode (root -> right, word);
}
}
相关文章:
- 在 Ubuntu 服务器上运行 QT 应用程序时崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 在传递 com 变体时运行 excel 宏崩溃
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- 如果程序在没有 sudo 的情况下运行,gtk_init会导致 ubuntu 16.04 崩溃
- 当我运行MEX文件时,MATLAB崩溃
- 使用-static libstdc++时std::线程弱,因此在运行时会导致崩溃
- 运行简单的.oct文件时Octave崩溃,我的代码出了什么问题?
- C++具有移动语义的可变参数工厂会导致运行时崩溃
- C++ - 代码在Visual Studio中完美运行,但命令行中的.exe不断崩溃
- 我的视频流程在运行darknet.exe以进行检测对象时崩溃了
- 当使用 /ENTRY:main 和 /MT 运行时库编译为 /SUBSYSTEM:WINDOWS 时,应用程序崩溃
- Omnet++(Eclipse IDE?)在运行INET项目时崩溃 - 在opplibs.dll中EXCEPTION_A
- 使用数组的单向链表:它在运行时崩溃
- "Unable to open file libc++abi.dylib" .程序生成,但在运行时崩溃。使用 xcode 10.1 (10B61)
- 故障转储文件是否需要崩溃时正在运行的本地文件
- 为什么在Shell输出中运行的可执行操作到stdout,而不是崩溃时的stderr
- QT项目在配置文件上崩溃,与发布和调试器一起运行
- 当从共享库运行时,为什么此Android NDK代码崩溃
- 通过Qt崩溃运行外部程序