繁体   English   中英

莫尔斯电码二进制搜索树中的分段错误

[英]Segmentation Fault in Morse Code Binary Search Tree

本质上,在该程序中,将创建一个二进制搜索树,其中充满了由字母和2个TREENODE指针(左右)组成的TREENODE结构,以连接到其他节点并模拟该树。

用户输入将保存到字符数组text 编码功能遍历数组,并指出莫尔斯电码翻译,并将其保存到字符数组morse 奇迹般有效。

问题 :在Decode功能中收到分段错误。 GDB: ![BST分段错误

这表明尚未真正创建任何二进制搜索树。

如何修复此代码,以使解码功能起作用? (我知道我只可以提示text但我想创建一个合法的函数来解码字符数组。

头文件:

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

Decode ,您需要添加node = root; 每当您开始解码新的莫尔斯电码符号时。 这可以通过以下方式完成:

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

如果输入无效,还应该检查要访问的节点(例如, node->left )是否确实存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM