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"
std::map<char, std::string> morseCode = {
std::map<char, std::string> morseMap = {
{'A', ".-"},
{'B', "-..."},
{'C', "-.-."},
@ -82,7 +82,7 @@ void refurbishMessage(std::string &msg)
// Remove all other unknown characters
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());
// 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
auto search = morseCode.find(c);
if (search == morseCode.end()) {
auto search = morseMap.find(c);
if (search == morseMap.end()) {
continue;
}
for (unsigned int j = 0; j < morseCode[c].length(); j++) {
char m = morseCode[c][j];
for (unsigned int j = 0; j < morseMap[c].length(); j++) {
char m = morseMap[c][j];
if (j == 0 && i > 0 && msg[i - 1] != ' ') {
morseString.push_back('c');
}
morseString += m;
if (j < morseCode[c].length() - 1) {
if (j < morseMap[c].length() - 1) {
morseString.push_back('i');
}
}
@ -140,12 +140,12 @@ std::string charToMorse(char ch)
return morseString;
}
for (unsigned int j = 0; j < morseCode[ch].length(); j++) {
char m = morseCode[ch][j];
for (unsigned int j = 0; j < morseMap[ch].length(); j++) {
char m = morseMap[ch][j];
morseString += m;
if (j < morseCode[ch].length() - 1) {
if (j < morseMap[ch].length() - 1) {
morseString.push_back('i');
}
}