我的程序在运行后崩溃了

时间:2015-02-25 15:41:00

标签: c++ class pointers recursion binary-search-tree

这是我的代码,基本上我正在尝试做的是我的程序从我准备的文本文件中取出单词并将其传递给我的程序并计算唯一单词的数量,并打印出来这是一个独特的单词,旁边有计数。我已经清理了所有的错误,但现在我卡住了,因为每次我试图运行它都会崩溃。 如果有人可以告诉我我做错了什么,我将不胜感激。

#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 << "Word\tCount\n";
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);
}
}        

2 个答案:

答案 0 :(得分:2)

这里有一个问题:

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

答案 1 :(得分:0)

更改了打印方式。以及插入的一行。

#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 << "Word\tCount\n";
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);
}
}        
相关问题