2017-07-29 9 views
0

本質的に、このプログラムでは、TREENODE構造体で構成され、他のノードに接続してツリーをシミュレートするポインタ(左と右)を持つ文字amd 2 TREENODEのポインタで構成されています。モールス符号のバイナリサーチツリー内のセグメンテーションフォールト

ユーザー入力が文字配列textに保存されます。 Encode関数は配列全体を反復し、モールスコード変換を文字配列morseに保存します。魅力のように動作します。

問題Decode機能でセグメンテーション違反が発生しました。 GDB:![BST Segmentation Fault

これは実際にバイナリ検索ツリーが作成されていないことを示します。

デコード機能が動作するようにこのコードを修正するにはどうすればよいですか? (私はちょうどtextをCOUTすることができますが、私は文字の配列を解読し、正当な関数を作成したいことを理解し

ヘッダファイル:。

struct TREENODE { 
char letter; 
TREENODE *left; 
TREENODE *right; 

TREENODE(){ // Constructor 
    letter = '*'; //To be replaced by a letter later. 
    left = 0; 
    right = 0; 
} 
}; 

struct MORSECODE{ //each morsecode object has 
    char letter; //English letters. 
    char code[20]; //Dots + dashes 
}; 

class TELEGRAPH{ //The binary (tree) 
    private: 
     static MORSECODE table[40]; 
     static TREENODE * root; 
     static void destroyTree(TREENODE *node); 
    public: 
     TELEGRAPH(){ 
      root = NULL; 
     } 
     static void buildTree(); 
     static void destroyTree(); 
     void Encode(char text[], char morse[]); 
     void Decode(char morse[], char text[]); 
};  

MORSECODE TELEGRAPH::table[40] = { 
{'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, 
{'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."}, 
{'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, 
{'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."}, 
{'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"}, 
{'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, 
{'Y', "-.--"}, {'Z', "--.."}, 
{'0', "-----"}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, 
{'4', "....-"}, {'5', "....."}, {'6', "-...."}, {'7', "--..."}, 
{'8', "---.."}, {'9', "----."}, 
{'.', ".-.-.-"}, {',', "--..--"}, {'?', "..--.."}, 
{'\0', "END"} 
}; 
TREENODE* TELEGRAPH::root = 0; 

void TELEGRAPH::Decode(char morse[], char text[]){ 
    char *morsePtr; 
    TREENODE *node; 
    node = root; 
    cout << "Decode called." << endl; 
    for (morsePtr = morse; *morsePtr; morsePtr++) { 
     if(*morsePtr != ' '){ 
      if(*morsePtr == '.'){ 
       node = node->left; 
      } 
      else if (*morsePtr == '-'){ 
       node = node->right; 
      } 
     } 
     continue; 
    } 
    *text++ = node->letter; 
    return; 
} 

void TELEGRAPH::Encode(char text[], char morse[]){ 
    int i; 
    char c, *t, *morsePtr; 
    cout << "Encode called" << endl; 
    cout << "\nSending >>> "; 
    for (t = text; *t; t++){ 
     c = toupper(*t); 
     if (c == ' ') { 
      *morse++ = ' '; 
      continue; 
     } 

     for (i = 0; table[i].letter; i++){ 
      if (table[i].letter == c) break; 
     }  
     if (!table[i].letter){ 
      continue; 
     } 
     morsePtr = table[i].code; 
     while (*morsePtr){ 
      *morse++ = *morsePtr++; 
     } 
     *morse++ = ' '; 
    } 
} 

void TELEGRAPH::buildTree(){ 
    TREENODE *node, *nextNode; 
    char *morsePtr; //Points to the dots and dashes in the table. 
    root = new TREENODE; 
    if (!root){ 
     return; 
    } 
    root->letter = ' '; 
    cout << "Alphabet in Morse:"; 
    for (int i = 0; table[i].letter; i++) { 
     node = root; 
     for (morsePtr = table[i].code; *morsePtr; morsePtr++){ //goes through the morse code for that letter/symbol. 
      if(*morsePtr == '-'){ 
       cout << *morsePtr; 
       nextNode = new TREENODE; 
       node->right = nextNode; 
       node = node->right; 
      } 
      else if(*morsePtr == '.'){ 
       cout << *morsePtr; 
       nextNode = new TREENODE; 
       node->left = nextNode; 
       node = node->left; 
      } 
     } 
    } 
} 

メイン():

int main(){ 
    TELEGRAPH station; 
    char text[80], morse[600]; 
    station.buildTree(); 
    cout << "\nEnter telegram (in English): "; 
    cin.getline(text, 80); 
    station.Encode(text, morse); 
    cout << morse; 
    cout << " >>> Received\n\n"; 
    station.Decode(morse, text); 
    cout << "Message sent: " << text << endl; 
    station.destroyTree(); 
} 
+2

このコードを修正するにはどうすればよいですか?あなたはデバッガ – user463035818

+1

を使うことができますが、代わりに 'std :: map' /' std :: string'を見てください。 –

答えて

0

Decodeでは、新しいモールス符号シンボルのデコードを開始するたびに、node = root;を追加する必要があります。これは、次のように行うことができます。

for (morsePtr = morse; *morsePtr; ++morsePtr) { 
    if (*morsePtr != ' ') { 
     // ... 
    } else { 
     node = root; 
    } 
} 

また、アクセスするノード(例: node->left)が実際に存在します。

関連する問題