2012-11-13 30 views
-3

我正在尝试编写一个程序,它将执行不同的功能到一棵树,到目前为止,除了打印功能,它们都可以工作。它以前是有效的,但是在试图解决其他功能中的一些问题时(没有搞乱它),现在它们被修复了,这个功能突然不起作用,我无法理解为什么。这里是我的代码:在函数调用时出现Seg错误

main.cpp中:

using namespace std; 
#include <iostream> 
#include <cstdlib> 
#include <cstring> 
#include "lcrs.h" 

int main() 
{ 
char *temp1; 
char *temp2; 
temp1 = new char; 
temp2 = new char; 

lcrs tree; 

do{ 
    cout << "LCRS> "; 
    cin >> temp1; 
    if(strcmp(temp1, "quit") == 0) 
    { 
     return 0; 
    } 
    if(strcmp(temp1, "insert") == 0) 
    { cin >> temp2; 
     bool error; 
     for(int i=0; i<strlen(temp2); i++) 
     { 
      if(!isdigit(temp2[i])) 
      { 
       cout << "Error!" << endl; 
       error = true; 
      } 
     } 
     if(!error) 
     { 
      tree.insert(atoi(temp2), tree.root); 
     } 
    } 
    else if(strcmp(temp1, "height") == 0) 
    { 
     if(tree.root == NULL) 
      cout << "-1" << endl; 
     else 
      cout << tree.getHeight(tree.root) << endl; 
    } 
    else if(strcmp(temp1, "preorder") == 0) 
    { 
     cout << "Root is " << tree.root->data << endl; 
     tree.print(tree.root); 
     cout << "" << endl; 
    } 
    else if(strcmp(temp1, "search") == 0) 
    { 
     cin >> temp2; 
       bool error; 
       for(int i=0; i<strlen(temp2); i++) 
      { 
         if(!isdigit(temp2[i])) 
         { 
           cout << "Error!" << endl; 
           error = true; 
        } 
       } 
       if(!error) 
        { 
         if(tree.search(atoi(temp2), tree.root)) 
       cout << "true" << endl; 
      else 
       cout << "false" << endl; 
       } 

    } 
    else 
    { 
     cout << "Error! " << endl; 
    } 
}while(strcmp(temp1, "quit") !=0); 

return 0; 
} 

lcrs.h:

using namespace std; 
#include <cstdlib> 
#include <iostream> 

class node{ 
    public: 
    int data; 
    node *right; 
    node *below; 

    node() 
    { 
     right = NULL; 
     below = NULL; 
    } 
}; 

class lcrs{ 
    public: 
    node *root; 
    bool search(int, node*); 
    void print(node*); 
    void insert(int, node*&); 
    int getHeight(node*); 

    lcrs() 
    { 
     root = NULL; 
    } 
}; 

lcrs.cpp:

using namespace std; 
#include "lcrs.h" 

bool lcrs::search(int x, node *b) 
{ 
    if(b == NULL) 
     return false; 
    else 
    { 
     if(b->data == x) 
      return true; 
     else 
     { 
      return search(x, b->right) || search(x, b->below); 
     } 
    } 
} 

void lcrs::print(node *z) 
{ 
    if(z->below == NULL || z->right != NULL) 
    { 
     cout << z->data << ","; 
     print(z->right); 
    } 
    else if(z->below != NULL && z->right == NULL) 
    { 
     cout << z->data << ","; 
     print(z->below); 
    } 
    else if(z->below != NULL && z->right != NULL) 
    { 
     cout << z->data << ","; 
     print(z->below); 
     print(z->right); 
    } 
    else if(z->right == NULL && z->below == NULL) 
    { 
      cout << z->data << ""; 
    } 


} 

void lcrs::insert(int x, node *&a) 
{ 
    if(a == NULL) 
    { 
     node *newnode; 
     newnode = new node; 
     newnode->data = x; 
     a = newnode; 
    } 
    else if(a->data < x) 
    { 
     if(a->right != NULL) 
     { 
      insert(x, a->right); 
     } 
     else if(a->below != NULL) 
     { 
      if(a->below->right != NULL) 
      { 
       insert(x, a->below->right); 
      } 
      else 
      { 
       insert(x, a->below); 
      } 
     } 
     else 
     { 
      node *n; 
      n = new node; 
      n->data = x; 
      a->below = n; 
     } 
    } 
    else if(a->data > x) 
    { 
     if(a->below != NULL) 
     { 
      insert(x, a->below); 
     } 
     else 
     { 
      node *n; 
      n = new node; 
      n->data = x; 
      a->right = n; 
     } 
    } 
} 
int lcrs::getHeight(node *h) 
{ 
    int height = 0; 
    node *n; 
    n = new node; 
    n = h; 
    while(n->below != NULL || n->right != NULL) 
    { 
     if(n->below != NULL) 
     { 
      n = n->below; 
      height ++; 
     } 
     else if(n->right != NULL) 
     { 
      n = n->right; 
     } 
    } 
    return height; 
} 

我得到一个赛格故障就在tree.print(tree.root)函数调用。我在函数的一开始就写了一个print语句,但它从来没有这样做过,所以我对这个问题的位置感到困惑。

非常感谢您的帮助。

+3

有没有听说过一个叫调试的花哨的东西? –

+1

你真的应该尝试去掉那些代码,直到真正需要重现错误。更容易找到你自己的错误,并且更容易阅读 – Chris

+1

与之前的修订版进行比较,找出在“消除”这些扭结时弄错了什么。 –

回答

0

存在很多问题。这可能与你读取输入的方式有关,将字符串填充到缓存中存储单个字符(,请使用std :: string代替 - 这是出于某种原因)或者它可能与事实有关tree.root可能为空,而你正在提取它:cout << "Root is " << tree.root->data << endl;

另外,你真的应该尽量在发布代码时减少一些东西。这有两个目的:它可以帮助你(因为你可能实际上发现什么是你自己的错误,或至少孤立错误,因为你正在修剪的东西),它可以帮助我们,因为我们不需要去通过页面和代码页面。

+0

我已经检查过,根目录不是空的,使用那个语句你有那里,它不是。而且我担心如果我只发布与问题相关的内容,我可能会留下一些可能会引起外界注意的事情。 –

+0

你*不*检查root是否为空或不为空。你无条件地在那里解引用root,它可能很空。 –

0

我发现问题,它只是一个小小的错字。 (当然是这样。) 感谢所有给出合法答案并真诚地帮助的人。 而且,还要感谢所有那些给我赞美的人。我明白,在我使用了调试器并且仍然处于我的智慧结尾之后,人们可以提醒我,我只是一名低级别的ComSci学生。非常感谢你。

+0

找到它的好工作。至于sass,对不起,如果你感到轻视,但对于每个使用调试器的Sarah Awesome,都有100个非真棒的人没有。祝你好运。还有一个小小的提示:你可能想重新检查一下你的'lcrs :: print',它有点过于复杂,可能会简化并且易于理解(和调试)。 –

相关问题