英語をモールス符号にエンコードできましたが、逆の操作を行うのに問題があります。これが私がこれまでに持っているものです:
概して():
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;
};
SearchAndReturn 関数:
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;
}
要求されたコード全体は次のとおりです。