#include <iostream>
#include <string>
using namespace std;
class Morse {
string alphabet[26];
string digit[10];
string slash, question, comma, period, plus, equal;
public:
Morse();
void text2Morse(string text, string& morse);
bool morse2Text(string morse, string& text);
};
Morse::Morse() {
string alphabet[26] = { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.." };
string digit[10] = { "-----",".----","..---","...--","....-",".....","-....","--...","---..","----." };
for (int i = 0; i < 26; i++) {
this->alphabet[i] = alphabet[i];
}
for (int i = 0; i < 10; i++) {
this->digit[i] = digit[i];
}
slash = "-..-."; question = "..--.."; comma = "--..--"; period = ".-.-.-"; plus = ".-.-."; equal = "-...-";
}
void Morse::text2Morse(string text, string& morse) {
int size = text.length();
morse = "";
for (int i = 0; i < size; i++) {
char c = text[i];
if (c == '/') morse = morse + slash + " ";
else if (c == '?') morse = morse + question + " ";
else if (c == ',') morse = morse + comma + " ";
else if (c == '.') morse = morse + period + " ";
else if (c == '+') morse = morse + plus + " ";
else if (c == '=') morse = morse + equal + " ";
else if (isdigit(c)) morse = morse + digit[c - 48] + " ";
else if (isalpha(c)) {
if (isupper(c)) {
morse = morse + alphabet[c - 65] + " ";
}
else if (islower(c)) {
morse = morse + alphabet[c - 97] + " ";
}
}
else if (c == ' ') morse += " ";
}
}
bool Morse::morse2Text(string morse, string &text) {
string bufMorse;
text2Morse(text, bufMorse);
if (bufMorse == morse) return true;
else return false;
}
int main() {
string text, morse;
Morse m;
cout << "아래에 영문 텍스트를 입력하세요. 모스 부호로 바꿉니다." << endl;
getline(cin, text);
m.text2Morse(text, morse);
cout << morse << endl << endl;
cout << "모스 부호를 다시 영문 텍스트로 바꿉니다." << endl;
if (m.morse2Text(morse, text)) cout << text << endl;
else cout << "오류 발생!" << endl;
}