0
基本上在這個程序中創建了一個二叉搜索樹,它完整的由一個字母amd 2 TREENODE
指針(左和右)組成的結構體TREENODE
連接到其他節點並模擬樹。用戶輸入被保存到字符數組text
。編碼函數在數組中迭代並查找莫爾斯碼轉換,將其保存到字符數組morse
。奇蹟般有效。摩爾斯電碼二叉搜索樹中的分段錯誤
這表示沒有真正創建二叉搜索樹。
如何解決此代碼,使解碼功能的作品? (我知道我可以只清點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;
}
}
}
}
的main():
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();
}
如何解決這個問題?你可以使用調試器 – user463035818
也許你應該看看'std :: map' /'std :: string'來代替。 –