1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #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; } | cs |
'Programming > 명품 C++ Programming' 카테고리의 다른 글
| 명품 C++ Programming 실습문제 5장 2번 (0) | 2018.05.09 |
|---|---|
| 명품 C++ Programming 실습문제 5장 1번 (0) | 2018.05.09 |
| 명품 C++ Programming 실습문제 4장 11번 응용 (0) | 2018.05.08 |
| 명품 C++ Programming 실습문제 4장 11번 (0) | 2018.05.06 |
| 명품 C++ Programming 실습문제 4장 10번 (0) | 2018.05.06 |