C++로 모스부호를 해석해보겠습니다. 모스 부호를 문자로, 문자를 모스 부호로 변환할 수 있습니다. 이 프로그램은 사용자가 모스 부호나 일반 문자로 메시지를 입력할 수 있게 해주고, 그에 따라 번역을 해줍니다.
프로그램 흐름
1. 프로그램은 "Text to Morse Code"와 "Morse Code to Text"의 두 가지 옵션이 있는 메뉴를 표시하는 것으로 시작합니다
2. 사용자가 옵션(1 또는 2) 중 하나를 선택합니다.
3. 사용자가 옵션 1을 선택하면 텍스트 메시지를 입력하라는 메시지가 표시되고 프로그램은 텍스트를 모스 부호로 변환하여 결과를 표시합니다.
4. 사용자가 옵션 2를 선택하면 모스 부호 메시지를 입력하라는 메시지가 표시되고, 프로그램은 모스 부호를 텍스트로 변환하여 결과를 표시합니다.
5. 프로그램은 오류 메시지를 표시하여 잘못된 선택 사항을 처리합니다.
#include <iostream>
#include <map>
#include <string>
using namespace std;
// Morse code to text translation function
string morseToText(const string& morseCode) {
map<string, char> morseMap = {
{".-", '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'},
{"/", ' '}, {"", ' '} // space is used to separate letters in Morse code
};
string textMessage = "";
string morseCharacter = "";
for (char ch : morseCode) {
if (ch == ' ' && !morseCharacter.empty()) {
textMessage += morseMap[morseCharacter];
morseCharacter = "";
} else if (ch != ' ') {
morseCharacter += ch;
}
}
if (!morseCharacter.empty()) {
textMessage += morseMap[morseCharacter];
}
return textMessage;
}
// Text to Morse code translation function
string textToMorse(const string& text) {
map<char, string> textToMorseMap = {
{'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', "----."}
};
string morseMessage = "";
for (char ch : text) {
if (ch == ' ') {
morseMessage += "/ "; // Use a slash to separate words in Morse code
} else if (textToMorseMap.find(ch) != textToMorseMap.end()) {
morseMessage += textToMorseMap[ch] + " ";
}
}
return morseMessage;
}
int main() {
cout << "Morse Code Translator" << endl;
cout << "Options:" << endl;
cout << "1. Text to Morse Code" << endl;
cout << "2. Morse Code to Text" << endl;
cout << "Enter your choice (1 or 2): ";
int choice;
cin >> choice;
cin.ignore(); // Clear the newline character from the input buffer
if (choice == 1) {
cout << "Enter the text to convert to Morse code: ";
string textMessage;
getline(cin, textMessage);
string morseMessage = textToMorse(textMessage);
cout << "Morse Code: " << morseMessage << endl;
} else if (choice == 2) {
cout << "Enter the Morse code to convert to text: ";
string morseMessage;
getline(cin, morseMessage);
string textMessage = morseToText(morseMessage);
cout << "Text: " << textMessage << endl;
} else {
cout << "Invalid choice. Please select 1 or 2." << endl;
}
return 0;
}
출력결과
문자에서 모스부호로 변환
Morse Code Translator
Options:
1. Text to Morse Code
2. Morse Code to Text
Enter your choice (1 or 2): 1
Enter the text to convert to Morse code: HELLO WORLD
Morse Code: .... . .-.. .-.. --- / .-- --- .-. .-.. -..
모스부호에서 문자로 변환
Morse Code Translator
Options:
1. Text to Morse Code
2. Morse Code to Text
Enter your choice (1 or 2): 2
Enter the Morse code to convert to text: .... . .-.. .-.. --- / .-- --- .-. .-.. -..
Text: HELLO WORLD
'Programming language > C++' 카테고리의 다른 글
[C++] 날짜 시간 표시 프로그램 (0) | 2023.10.07 |
---|---|
[C++] 콘솔기반 MP3 플레이어 만들기 (0) | 2023.10.07 |
[C++] 파일 복사 유틸리티 구현 (0) | 2023.10.07 |
[C++]단순한 계산기 구현하기 (0) | 2023.10.07 |
[C++] 비밀번호 생성기 소스 코드 (0) | 2023.10.07 |