diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..95050ce --- /dev/null +++ b/main.cpp @@ -0,0 +1,200 @@ +#include +#include +#include +#include + +using namespace std; + +const map morse_map = { + {'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', "----."}, + {'.', ".-.-.-"}, {',', "--..--"}, {'?', "..--.."}, {'!', "-.-.--"}, + {'\'', ".----."}, {'/', "-..-."}, {'(', "-.--."}, {')', "-.--.-"}, + {'&', ".-..."}, {':', "---..."}, {';', "-.-.-."}, {'=', "-...-"}, + {'+', ".-.-."}, {'-', "-....-"}, {'_', "..--.-"}, {'"', ".-..-."}, + {'$', "...-..-"}, {'@', ".--.-."}}; + +bool should_exit = false; + +bool is_exit(const string &s) { + return s.length() == 4 && tolower(s[0]) == 'e' && tolower(s[1]) == 'x' && + tolower(s[2]) == 'i' && tolower(s[3]) == 't'; +} + +string read_input(const string &prompt) { + cout << prompt; + string line; + if (!getline(cin, line) || line.empty()) return ""; + if (is_exit(line)) { + should_exit = true; + return ""; + } + return line; +} + +class MorseDecoder { +private: + map morse_to_char; + +public: + MorseDecoder() { + for (const auto &[ch, code] : morse_map) { + morse_to_char[code] = ch; + } + } + + char decode(const string &morse) { + auto it = morse_to_char.find(morse); + return it != morse_to_char.end() ? it->second : '?'; + } +}; + +class MorseEncoder { +private: + const map &char_to_morse; + +public: + MorseEncoder() : char_to_morse(morse_map) {} + + string encode(char c) { + auto it = char_to_morse.find(toupper(c)); + return it != char_to_morse.end() ? it->second : ""; + } + + string get_full_sequence(const string &text) { + string sequence; + sequence.reserve(text.length() * 5); + bool need_space = false; + + for (char c : text) { + if (c == ' ') { + sequence += " / "; + need_space = false; + } else { + string morse = encode(c); + if (!morse.empty()) { + if (need_space) sequence += " "; + sequence += morse; + need_space = true; + } + } + } + return sequence; + } + + void print_morse_pattern(const string &text) { + for (char c : text) { + if (c == ' ') { + cout << "\n[Space]\n"; + } else { + string morse = encode(c); + if (!morse.empty()) { + cout << c << " -> " << morse << "\n"; + } + } + } + } +}; + +void decode_mode() { + MorseDecoder decoder; + cout << "Decode mode: Enter morse sequence to decode. Press 'e' to switch to " + "encode mode, 'exit' to exit.\n\n"; + + while (!should_exit) { + string input = read_input("Enter morse sequence: "); + if (input.empty()) continue; + + if (tolower(input[0]) == 'e') return; + + size_t start = input.find_first_not_of(" \t"); + if (start == string::npos) continue; + size_t end = input.find_last_not_of(" \t"); + + string decoded_message; + string current_morse; + bool needs_word_separator = false; + bool has_invalid_chars = false; + bool in_separator = false; + + auto decode_and_add = [&]() { + char decoded = decoder.decode(current_morse); + cout << current_morse << " -> " << decoded << "\n"; + decoded_message += decoded; + current_morse.clear(); + }; + + for (size_t i = start; i <= end; ++i) { + char c = input[i]; + + if (c == ' ' || c == '/') { + if (!in_separator && !current_morse.empty()) { + decode_and_add(); + in_separator = true; + } + if (c == '/') needs_word_separator = true; + } else if (c == '.' || c == '-') { + if (in_separator) { + if (needs_word_separator && !decoded_message.empty()) { + decoded_message += ' '; + needs_word_separator = false; + } + in_separator = false; + } + current_morse += c; + } else { + in_separator = false; + has_invalid_chars = true; + } + } + + if (!current_morse.empty()) decode_and_add(); + + if (has_invalid_chars) { + cout << "\nWarning: Invalid characters were ignored.\n"; + } + + cout << "\nFinal message: " << decoded_message << "\n\n"; + } +} + +void encode_mode() { + MorseEncoder encoder; + cout << "Encode mode: Enter text to see Morse code patterns. Press 'd' to " + "switch to decode mode, 'exit' to exit.\n\n"; + + while (!should_exit) { + string input = read_input("Enter text to encode: "); + if (input.empty()) continue; + + if (tolower(input[0]) == 'd') return; + + cout << "\n"; + encoder.print_morse_pattern(input); + cout << "\nFinal sequence: " << encoder.get_full_sequence(input) << "\n\n"; + } +} + +int main() { + cout << "dotdash - Morse code playground\n"; + cout << "Starting in decode mode...\n\n"; + + bool decode = true; + while (!should_exit) { + decode ? decode_mode() : encode_mode(); + if (!should_exit) { + decode = !decode; + cout << "\nSwitched to " << (decode ? "decode" : "encode") << " mode.\n\n"; + } + } + + cout << "\nExiting...\n"; + return 0; +}