Odpowiedź :
Odpowiedź:
#include <iostream>
#include <string>
#include <fstream>
#include <map>
using namespace std;
map< char, string > ascii_to_morse =
{
{'A',".-"},
{'B',"-..."},
{'C',"-.-."},
{'D',"-.."},
{'E',"."},
{'F',"..-."},
{'G',"--."},
{'H',"...."},
{'I',".."},
{'J',".---"},
{'K',"-.-"},
{'L',".-.."},
{'M',"--"},
{'N',"-."},
{'O',"---"},
{'P',".--."},
{'Q',"--.-"},
{'R',".-."},
{'S',"..."},
{'T',"-"},
{'U',"..-"},
{'V',"...-"},
{'W',".--"},
{'X',"-..-"},
{'Y',"-.--"},
{'Z',"--.."},
{'0',"-----"},
{'1',".----"},
{'2',"..---"},
{'3',"...--"},
{'4',"....-"},
{'5',"....."},
{'6',"-...."},
{'7',"--..."},
{'8',"---.."},
{'9',"----."},
{'.',".-.-.-"},
{',',"--..--"},
{'?',"..--.."},
{'=',"-...-"},
};
map< string, char > morse_to_ascii;
int main()
{
// build the reversed map
for (auto kv : ascii_to_morse)
morse_to_ascii[kv.second] = kv.first;
// ascii to morse test
{
ifstream is{ "c:\\temp\\ascii.txt" };
if (!is)
return -1;
ofstream os{ "c:\\temp\\morse.txt" };
if (!os)
return -2;
// ascii to morse
char c;
while (is >> c)
os << ascii_to_morse[c] << ' ';
}
// morse to ascii test
{
ifstream is{ "c:\\temp\\morse.txt" };
if (!is)
return -1;
ofstream os{ "c:\\temp\\ascii_check.txt" };
if (!os)
return -2;
// morse to ascii
string m;
while (is >> m)
os << morse_to_ascii[m] << ' ';
}
}