#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <Windows.h>
#include <string>
#include <ctime>
using namespace std;
std::map<char, std::string> morse_dict = {
{'А', ".-"},
{'Б', "-..."},
{'В', ".--"},
{'Г', "--."},
{'Д', "-.."},
{'Е', "."},
{'Ж', "...-"},
{'З', "--.."},
{'И', ".."},
{'Й', ".---"},
{'К', "-.-"},
{'Л', ".-.."},
{'М', "--"},
{'Н', "-."},
{'О', "---"},
{'П', ".--."},
{'Р', ".-."},
{'С', "..."},
{'Т', "-"},
{'У', "..-"},
{'Ф', "..-."},
{'Х', "...."},
{'Ц', "-.-."},
{'Ч', "---."},
{'Ш', "----"},
{'Щ', "--.-"},
{'Ъ', "--.--"},
{'Ы', "-.--"},
{'Ь', "-..-"},
{'Э', "..-.."},
{'Ю', "..--"},
{'Я', ".--.."}
};
std::string encode_to_morse(const std::string& text) {
std::string result;
for (const char c : text) {
if (morse_dict.count(c)) {
result += morse_dict[c] + " ";
}
}
return result;
}
std::string decode_from_morse(const std::string& morse) {
std::string result;
std::vector<std::string> words;
std::string word;
for (const char c : morse) {
if (c == ' ') {
words.push_back(word);
word.clear();
}
else {
word += c;
}
}
words.push_back(word);
for (const std::string& word : words) {
for (const auto& p : morse_dict) {
if (p.second == word) {
result += p.first;
break;
}
}
}
return result;
}
string cezar(string text, int offset)
{
transform(text.begin(), text.end(), text.begin(), toupper);
char ABC[] = { 'А','Б','В','Г','Д','Е','Ж','З','И',
'Й','К','Л','М','Н','О','П','Р','С',
'Т','У','Ф','Х','Ц','Ч','Ш','Щ','Ъ',
'Ы','Ь','Э','Ю','Я' };
for (int i = 0; i < text.size(); i++)
{
if (text[i] == ' ')
continue;
else
{
auto it = std::find(std::begin(ABC), std::end(ABC), text[i]);
if (it != std::end(ABC))
{
int index = std::distance(std::begin(ABC), it);
int newIndex = (index + offset) % sizeof(ABC);
text[i] = ABC[newIndex];
}
}
}
return text;
}
string toDVDV(string m)
{
string code = "";
for (int i = 0; i < m.size(); ++i)
{
int a = rand() % 2;
if (m[i] == '.')
{
if (a) code += "00";
else code += "11";
code += ' ';
}
else if (m[i] == ' ')
code += " | ";
else
{
if (a) code += "01";
else code += "10";
code += ' ';
}
}
return code;
}
string fromDVDV(string code)
{
string output = "";
for (int i = 0; i < code.size(); i += 3)
{
string binary = code.substr(i, 2);
if (binary == "00" || binary == "11") output += '.';
else if (binary == "01" || binary == "10") output += '-';
else if (binary == "| ") output += ' ';
}
return output;
}
int main()
{
srand(time(NULL));
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
string decoded = cezar(decode_from_morse(fromDVDV("")), key);
cout << decoded << endl;
return 0;
}