【问题标题】:my program crashes after running我的程序运行后崩溃
【发布时间】:2015-04-27 16:38:17
【问题描述】:

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

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

【问题讨论】:

  • 你试过调试吗?
  • 是的,我尝试过,但没有成功。事实上,这有点令人沮丧。

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


【解决方案1】:

这里有一个问题:

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

【讨论】:

  • 但 char *word 的工作方式与 char word[100] 不相似吗?如果我把它设为静态会有帮助吗?
  • 在我完成之后,我认为我的程序无法正常运行,它不会在我的 BST 中插入单词,我不知道为什么。我目前仍在努力。
  • char *word 分配一个没有特定值的指针变量。然后你不能将那个无意义的值传递给operator&gt;&gt;
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多