我能够将英语编码为摩尔斯电码,但在进行反向操作时遇到了麻烦。这是我到目前为止所拥有的:
在 main() 中:
cout << "Enter your Morse code, separated by /, ended by *: ";
cin.getline(morseCode, 100, '*');
char* token = strtok(morseCode, "/");
while(token != NULL)
{
cout << endl << "Decoding: " << token << endl;
string newCode = token;
t.Decode(newCode);
token = strtok(NULL, "/");
}
解码功能:
void Decode(string x)
{
Node* r = SearchAndReturnString(root, x);
if(r->code == x) cout << r->letter;
else cout << r->code << " with x being " << x << endl; cout << "Error.";
}
我的输出是一堆随机垃圾数据,然后程序崩溃。我知道它与SearchAndReturnString
功能有关,但我不知道还有什么用。
编辑:
节点结构:
struct Node
{
string letter;
string code;
Node *left;
Node *right;
};
搜索并返回函数:
Node* SearchAndReturnString(Node *r, string x)
{
if(r != NULL)
{
if(r->code == x) {cout << r->code << " matches " << x << endl; return r;}
else if(r->code > x) {SearchAndReturnString(r->left, x);}
else {SearchAndReturnString(r->right, x);}
}
else return NULL;
}
这是所要求的整个代码:
标题: http: //pastebin.com/QyaakvMK
主要: http: //pastebin.com/NcseqrbX