style: clang-format

This commit is contained in:
2025-11-11 20:46:24 +01:00
parent a012a80438
commit 9dec568a3f

View File

@@ -31,7 +31,8 @@ bool is_exit(const string &s) {
string read_input(const string &prompt) { string read_input(const string &prompt) {
cout << prompt; cout << prompt;
string line; string line;
if (!getline(cin, line) || line.empty()) return ""; if (!getline(cin, line) || line.empty())
return "";
if (is_exit(line)) { if (is_exit(line)) {
should_exit = true; should_exit = true;
return ""; return "";
@@ -80,7 +81,8 @@ public:
} else { } else {
string morse = encode(c); string morse = encode(c);
if (!morse.empty()) { if (!morse.empty()) {
if (need_space) sequence += " "; if (need_space)
sequence += " ";
sequence += morse; sequence += morse;
need_space = true; need_space = true;
} }
@@ -110,12 +112,15 @@ void decode_mode() {
while (!should_exit) { while (!should_exit) {
string input = read_input("Enter morse sequence: "); string input = read_input("Enter morse sequence: ");
if (input.empty()) continue; if (input.empty())
continue;
if (tolower(input[0]) == 'e') return; if (tolower(input[0]) == 'e')
return;
size_t start = input.find_first_not_of(" \t"); size_t start = input.find_first_not_of(" \t");
if (start == string::npos) continue; if (start == string::npos)
continue;
size_t end = input.find_last_not_of(" \t"); size_t end = input.find_last_not_of(" \t");
string decoded_message; string decoded_message;
@@ -139,7 +144,8 @@ void decode_mode() {
decode_and_add(); decode_and_add();
in_separator = true; in_separator = true;
} }
if (c == '/') needs_word_separator = true; if (c == '/')
needs_word_separator = true;
} else if (c == '.' || c == '-') { } else if (c == '.' || c == '-') {
if (in_separator) { if (in_separator) {
if (needs_word_separator && !decoded_message.empty()) { if (needs_word_separator && !decoded_message.empty()) {
@@ -155,7 +161,8 @@ void decode_mode() {
} }
} }
if (!current_morse.empty()) decode_and_add(); if (!current_morse.empty())
decode_and_add();
if (has_invalid_chars) { if (has_invalid_chars) {
cout << "\nWarning: Invalid characters were ignored.\n"; cout << "\nWarning: Invalid characters were ignored.\n";
@@ -172,9 +179,11 @@ void encode_mode() {
while (!should_exit) { while (!should_exit) {
string input = read_input("Enter text to encode: "); string input = read_input("Enter text to encode: ");
if (input.empty()) continue; if (input.empty())
continue;
if (tolower(input[0]) == 'd') return; if (tolower(input[0]) == 'd')
return;
cout << "\n"; cout << "\n";
encoder.print_morse_pattern(input); encoder.print_morse_pattern(input);
@@ -191,7 +200,8 @@ int main() {
decode ? decode_mode() : encode_mode(); decode ? decode_mode() : encode_mode();
if (!should_exit) { if (!should_exit) {
decode = !decode; decode = !decode;
cout << "\nSwitched to " << (decode ? "decode" : "encode") << " mode.\n\n"; cout << "\nSwitched to " << (decode ? "decode" : "encode")
<< " mode.\n\n";
} }
} }