2012-11-13 28 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',它有點過於複雜,可能會簡化並且易於理解(和調試)。 –

相關問題