#include<iostream>
using namespace std;
class Morse
{
public:
static string decode(string);
static string encode(string);
protected:
static string arrDecode[256];
static string arrEncode[128];
static unsigned decodeUnit(string);
};
string Morse::arrDecode[256]
={" ","t","n","m","d","k","g","o", "b","x","c","y","z","q","","CH",
"6","" ,"/","" ,"" ,"" ,"" ,"" , "7","" ,"" ,"" ,"8","" ,"9","0",
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"-",";","!","(",")","" ,"" ,
"" ,"" ,"" ,",","" ,"" ,"" ,"" , ":","" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"?","_","" ,"" ,
"" ,"" ,"\"","","" ,".","" ,"" , "" ,"" ,"@","" ,"" ,"" ,"\'","",
"5","4","" ,"3","" ,"" ,"" ,"2", "" ,"" ,"+","" ,"" ,"" ,"" ,"1",
"h","v","f","" ,"l","" ,"p","j", "s","u","r","w","i","a","e","",
};
string Morse::decode(string str)
{
string s;
char c='\0';
unsigned spacecount=1, i=0;
for(; (c=str[i])!='\0'; i++)
{
if((c=='-' || c=='.') && spacecount>0)
{
s=s+arrDecode[decodeUnit(&str[i])];
spacecount=0;
}
else if(c!='-' && c!='.')
spacecount++;
if(c=='/' ||c=='\\' ||c=='|' ||spacecount==3)
s=s+' ';
}
return s;
}
unsigned Morse::decodeUnit(string str)
{
char n='\0';
if(str[0]=='.')
n='\xff';
char c=0;
for(unsigned i=0; (c=str[i])!='\0'; i++)
{
n<<=1;
if(c=='-')
n^=0x1;
else if(c=='.')
n&=0xfe;
else
{
n>>=1;
break;
}
}
return unsigned(n & 0xff);
}
string Morse::arrEncode[128]
={"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,
" " ,"-.-.--",".-..-.","" ,"" ,"" ,"" ,".----.",
"-.--..","-.--.-","" ,".-.-." ,"--..--","-....-",".-.-.-","-..-." ,
"-----" ,".----" ,"..---" ,"...--" ,"....-" ,"....." ,"-...." ,"--..." ,
"---.." ,"----." ,"---...","-.-.-.","" ,"-...-" ,"" ,"--..--",
".--.-.",".-" ,"-..." ,"-.-." ,"-.." ,"." ,"..-." ,"--." ,
"...." ,".." ,".---" ,"-.-" ,".-.." ,"--" ,"-." ,"---" ,
".--." ,"--.-" ,".-." ,"..." ,"-" ,"..-" ,"...-" ,".--" ,
"-..-" ,"-.--" ,"--.." ,"" ,"" ,"" ,"" ,"..--.-",
"" ,".-" ,"-..." ,"-.-." ,"-.." ,"." ,"..-." ,"--." ,
"...." ,".." ,".---" ,"-.-" ,".-.." ,"--" ,"-." ,"---" ,
".--." ,"--.-" ,".-." ,"..." ,"-" ,"..-" ,"...-" ,".--" ,
"-..-" ,"-.--" ,"--.." ,"" ,"" ,"" ,"" ,"" ,
};
string Morse::encode(string str)
{
string s="";
for(unsigned i=0; str[i]!='\0'; i++)
s=s+arrEncode[str[i]]+' ';
return s;
}
int main()
{
cout<<"******** Welcome to the Morse Code Translator 1.0 by CLAYCRAFT ********\n"
<<" You can type either Morse code or English text below.\n"
<<" Tips:\n"
<<" * Split the Morse units with either a space' ', or a comma','.\n"
<<" * A '/','\\' or '|' will separate the words.\n"
<<" * For example, try typing:\n"
<<" .... . .-.. .-.. --- --..-- /.-- --- .-. .-.. -.. -.-.--\n"
<<" * Then you can type the words you see to check if this translation is\n"
<<" reversible."<<endl<<endl<<">>> ";
string s;
char c;
while(1)
{
cin>>s;
c=cin.get();
if(s[0]=='-' || s[0]=='.' || s[0]=='/' || s[0]=='\\' || s[0]=='|')
{
cout<<Morse::decode(s);
}
else
{
cout<<Morse::encode(s);
if(c==' ')
cout<<'/';
}
if(c=='\n')
cout<<endl<<endl<<">>> ";
}
return 0;
}