我的程序运行后崩溃

my program crashes after running

本文关键字:崩溃 运行 程序 我的      更新时间:2023-10-16

这是我的代码,基本上我要做的是让我的程序从我准备的文本文件中提取单词,并将其传递给我的程序,计算唯一单词的数量,然后打印出带有计数的唯一单词。我已经清除了所有错误,但现在我被卡住了,因为每次我尝试运行它时它都会崩溃。如果有人能告诉我在上哪里做错了,我将不胜感激

#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);
}
}