#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cctype>

std::vector<char> characters;
std::vector<std::string> morseCodes;

void loadMorseTable(const std::string& filename) {
    std::ifstream file(filename);
    std::string line;

    if (!file.is_open()) {
        std::cerr << "Erreur lors de l'ouverture du fichier!" << std::endl;
        exit(1);
    }

    while (getline(file, line)) {
        if (line.length() > 2) {
            characters.push_back(line[0]);
            morseCodes.push_back(line.substr(2));
        }
    }
}

std::string textToMorse(const std::string& text) {
    std::string morseText;

    for (char c : text) {
        c = toupper(c);
        for (size_t i = 0; i < characters.size(); ++i) {
            if (characters[i] == c) {
                morseText += morseCodes[i] + " ";
                break;
            }
        }
    }

    return morseText;
}

int main() {
    loadMorseTable("morse_codes.txt");
    std::cout << "Entrez votre texte: ";
    std::string inputText;
    getline(std::cin, inputText);

    std::cout << "Texte en Morse: " << textToMorse(inputText) << std::endl;

    return 0;
}

 

 

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:----.

Modifié le: vendredi 13 octobre 2023, 07:45