This commit is contained in:
Martin Brodbeck 2024-02-28 13:35:09 +01:00
parent 1f6ab48af4
commit 751e4dfef7

View file

@ -4,7 +4,7 @@
#include "morse.h" #include "morse.h"
std::map<char, std::string> morseCode = { std::map<char, std::string> morseMap = {
{'A', ".-"}, {'A', ".-"},
{'B', "-..."}, {'B', "-..."},
{'C', "-.-."}, {'C', "-.-."},
@ -82,7 +82,7 @@ void refurbishMessage(std::string &msg)
// Remove all other unknown characters // Remove all other unknown characters
msg.erase(remove_if(msg.begin(), msg.end(), msg.erase(remove_if(msg.begin(), msg.end(),
[](const char &c) { return c != ' ' && morseCode.find(c) == morseCode.end(); }), [](const char &c) { return c != ' ' && morseMap.find(c) == morseMap.end(); }),
msg.end()); msg.end());
// Remove spaces, if there are too many of them // Remove spaces, if there are too many of them
@ -104,20 +104,20 @@ std::string messageToMorse(std::string &msg)
} }
// Ignore and continue with next char, if not found // Ignore and continue with next char, if not found
auto search = morseCode.find(c); auto search = morseMap.find(c);
if (search == morseCode.end()) { if (search == morseMap.end()) {
continue; continue;
} }
for (unsigned int j = 0; j < morseCode[c].length(); j++) { for (unsigned int j = 0; j < morseMap[c].length(); j++) {
char m = morseCode[c][j]; char m = morseMap[c][j];
if (j == 0 && i > 0 && msg[i - 1] != ' ') { if (j == 0 && i > 0 && msg[i - 1] != ' ') {
morseString.push_back('c'); morseString.push_back('c');
} }
morseString += m; morseString += m;
if (j < morseCode[c].length() - 1) { if (j < morseMap[c].length() - 1) {
morseString.push_back('i'); morseString.push_back('i');
} }
} }
@ -140,12 +140,12 @@ std::string charToMorse(char ch)
return morseString; return morseString;
} }
for (unsigned int j = 0; j < morseCode[ch].length(); j++) { for (unsigned int j = 0; j < morseMap[ch].length(); j++) {
char m = morseCode[ch][j]; char m = morseMap[ch][j];
morseString += m; morseString += m;
if (j < morseCode[ch].length() - 1) { if (j < morseMap[ch].length() - 1) {
morseString.push_back('i'); morseString.push_back('i');
} }
} }