Created
October 10, 2012 17:46
-
-
Save mimic2300/3867178 to your computer and use it in GitHub Desktop.
Morseovka
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <map> | |
using namespace std; | |
class Morse | |
{ | |
public: | |
Morse() | |
{ | |
morse['0'] = "-----"; | |
morse['1'] = ".----"; | |
morse['2'] = "..---"; | |
morse['3'] = "...--"; | |
morse['4'] = "....-"; | |
morse['5'] = "....."; | |
morse['6'] = "-...."; | |
morse['7'] = "--..."; | |
morse['8'] = "---.."; | |
morse['9'] = "----."; | |
morse['A'] = ".-"; | |
morse['B'] = "-..."; | |
morse['C'] = "-.-."; | |
morse['D'] = "-.."; | |
morse['E'] = "."; | |
morse['F'] = "..-."; | |
morse['G'] = "--."; | |
morse['H'] = "...."; | |
morse['I'] = ".."; | |
morse['J'] = ".---"; | |
morse['K'] = "-.-"; | |
morse['L'] = ".-.."; | |
morse['M'] = "--"; | |
morse['N'] = "-."; | |
morse['O'] = "---"; | |
morse['P'] = ".--."; | |
morse['Q'] = "--.-"; | |
morse['R'] = ".-."; | |
morse['S'] = "..."; | |
morse['T'] = "-"; | |
morse['U'] = "..-"; | |
morse['V'] = "...-"; | |
morse['W'] = ".--"; | |
morse['X'] = "-..-"; | |
morse['Y'] = "-.--"; | |
morse['Z'] = "--.."; | |
morse['?'] = "..--.."; | |
morse[','] = "--..--"; | |
morse['!'] = "--...-"; | |
morse['.'] = ".-.-.-"; | |
morse[';'] = "-.-.-."; | |
morse['/'] = "-..-."; | |
morse['='] = "-...-"; | |
morse['-'] = "-....-"; | |
morse['\t'] = ".----."; | |
morse['('] = "-.--."; | |
morse[')'] = "-.--.-"; | |
morse['"'] = ".-..-."; | |
morse[':'] = "---..."; | |
morse['_'] = "..--.-"; | |
morse['@'] = ".--.-."; | |
} | |
string ToMorse(const string text) | |
{ | |
string out; | |
string::const_iterator it = text.begin(); | |
for (; it != text.end(); it++) | |
{ | |
out += morse[toupper(*it)]; | |
if (it != text.end() - 1) | |
out += " "; | |
} | |
return out; | |
} | |
string ToText(const string morseText, const bool lower = false) | |
{ | |
string out, code; | |
istringstream iss(morseText, istringstream::in); | |
while (iss >> code) | |
{ | |
map<char, string>::const_iterator it = morse.begin(); | |
for(; it != morse.end(); it++) | |
{ | |
if (!it->second.compare(code)) | |
{ | |
out += (lower) ? tolower(it->first) : it->first; | |
break; | |
} | |
} | |
} | |
return out; | |
} | |
private: | |
map<char, string> morse; | |
}; | |
int main() | |
{ | |
Morse morse; | |
// .... . .-.. .-.. --- .-- --- .-. .-.. -.. --...- | |
string m = morse.ToMorse("Hello World!"); | |
// helloworld! | |
string r = morse.ToText(m, true); | |
cin.get(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment