Traducir números en código Morse y texto inglés

Traté de añadir números para el array de cadenas de texto en inglés y matriz de código Morse pero para Inglés a Mosre sólo tradujo 2 números a Morse cuando escribí 3 números en texto en inglés. Para Morse a traducción de texto en inglés, los números en Morse traducidos a símbolos aleatorios.

¿Cómo puedo resolver esto?

#include  
#include  
#include 
 
using namespace std;
 
string convertToEnglish(string morse, string const morseCode[], string const alpha[]);
string convertToMorse(string english, string const morseCode[], string const alpha[]);
 
int main()
{
    string option = ""; 
    cout << "TEXT TO MORSE CODE or MORSE CODE TO TEXT Please selection option: \nSelect 1 to decode 
    English text to Morse code \nSelect 2 to decode Morse code to English text \n";
    getline(cin, option);
 
    string const morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.",
    "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
    ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",".----","..---","...--"," "," "}; 
 
    string const englishLetter[] = {"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","1","2","3",""," "}; 
 
 
    if (option == "1") 
    {
        //english to morse
 
        string input = ""; 
        cout << "Enter English words to translate, with a space between letters and three spaces between 
        words:";
        getline(cin, input);
 
        //Convert string to be all upper case
        string uppercaseInput = "";
        for (int i = 0; i < input.size(); i++)
        {
            uppercaseInput += toupper(input[i]);
        }
 
        cout << convertToMorse(uppercaseInput, morseCode, englishLetter) << endl;
 
    } 
    else if (option == "2")
    {
        //morse to english
        string input = "";
        cout << "Enter morse with only space between code: \n";
        getline(cin, input);
        cout << convertToEnglish(input, morseCode, englishLetter) << endl;
    }
 
    return 0;
}
 
string convertToMorse(string english, string const morseCode[], string const alpha[]) 
{
    string output = "";
    string currentLetter = "";
 
    int position = 0;
 
    int const letterCount = english.length(); 
 
    int const numberOfAlpha = 28;
 
    cout << "\n";
 
    while (position <= letterCount)
    {
        currentLetter = string(1, english[position]); 
 
        int marker = 0;
 
        string addition = "";
 
        while (marker < 28)
        {           
            if (currentLetter == alpha[marker])
            {
                addition = morseCode[marker];
            }
 
            ++marker;
        }
 
        output += addition + " ";
 
        ++position;
    }
 
    return output;
}
 
string convertToEnglish(string morse, string const morseCode[], string const alpha[]) 
{
    string output = "";
    string currentLetter = "";
    istringstream ss(morse);    
 
    int const characters = 28;
 
    while(ss >> currentLetter)
    {
 
        int index = 0;
        while(currentLetter != morseCode[index] && index < characters)
        {
            ++index; //increment here so we don't have to decrement after the loop like if we put in the condition
        }
 
        output += 'A' + index;
    }
 
    return output;
}

Pregunta hecha hace 3 años, 4 meses, 29 días - Por devgeek


2 Respuestas:

  • El problema en tu código se debe a que estás tratando de traducir los números directamente a letras en lugar de a su representación en código morse cuando estás convirtiendo de Morse a texto inglés. Además, tienes un pequeño error en la línea donde añades los números a los arrays de letras y código morse, ya que estás dejando una cadena vacía entre los números y el espacio que debería representar.

    Para resolver este problema, primero debes corregir el error en la declaración de los arrays:

    cpp
    string const morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.",
        "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
        ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",".----","..---","...--","....",""};
    
    string const englishLetter[] = {"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","1","2","3"};
    

    Luego, en la función convertToEnglish, debes modificar la forma en que se comparan los datos y se añaden al output. Aquí te dejo una posible solución:

    cpp
    string convertToEnglish(string morse, string const morseCode[], string const alpha[]) 
    {
        string output = "";
        string currentLetter = "";
        istringstream ss(morse);    
    
        int const characters = 28;
    
        while(ss >> currentLetter)
        {
            int index = 0;
            while (index < characters)
            {           
                if (currentLetter == morseCode[index])
                {
                    if (alpha[index] != "")
                        output += alpha[index];
                    break;
                }
                ++index;
            }
        }
    
        return output;
    }
    

    Con estos cambios, tu programa debería ser capaz de traducir correctamente los números en Morse y en texto inglés. ¡Prueba estos cambios y me dices si funcionan para ti!

    Respondida el Dec 18, 2020 a las 16:00 - por Gpt

    Votos positivos: 0 | Votos negativos: 0

  • Problemas identificados:

    Conversión de inglés a Morse: Solo traduce los dos primeros dígitos en texto en inglés. Conversión de Morse a inglés: Traduce los códigos Morse numéricos a símbolos aleatorios.

    Solución:

    Conversión de inglés a Morse:

    Para traducir números en texto en inglés a Morse, debes agregar los caracteres numéricos correspondientes a la matriz englishLetter. Modifica la matriz englishLetter de la siguiente manera:

    cpp
    string const englishLetter[] = {"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", "1", "2", "3", " ", " "};
    

    Conversión de Morse a inglés:

    Para traducir los códigos Morse numéricos a números en texto en inglés, debes agregar los caracteres numéricos correspondientes a la matriz morseCode. Modifica la matriz morseCode de la siguiente manera:

    cpp
    string const morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.",
    "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
    ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",".----","..---","...--","1","2","3",""," "};
    

    Código corregido:

    ```cpp include include include using namespace std;

    string convertToEnglish(string morse, string const morseCode[], string const alpha[]); string convertToMorse(string english, string const morseCode[], string const alpha[]);

    int main() { string option = ""; cout << "TEXT TO MORSE CODE or MORSE CODE TO TEXT Please selection option: \nSelect 1 to decode English text to Morse code \nSelect 2 to decode Morse code to English text \n"; getline(cin, option);

    string const morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.",
    "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
    ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",".----","..---","...--","1","2","3",""," "}; 
    
    string const englishLetter[] = {"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","1","2","3",""," "}; 
    
    
    if (option == "1") 
    {
        //english to morse
    
        string input = ""; 
        cout << "Enter English words to translate, with a space between letters and three spaces between 
        words:";
        getline(cin, input);
    
        //Convert string to be all upper case
        string uppercaseInput = "";
        for (int i = 0; i < input.size(); i++)
        {
            uppercaseInput += toupper(input[i]);
        }
    
        cout << convertToMorse(uppercaseInput, morseCode, englishLetter) << endl;
    
    } 
    else if (option == "2")
    {
        //morse to english
        string input = "";
        cout << "Enter morse with only space between code: \n";
        getline(cin, input);
        cout << convertToEnglish(input, morseCode, englishLetter) << endl;
    }
    
    return 0;
    

    }

    string convertToMorse(string english, string const morseCode[], string const alpha[]) { string output = ""; string currentLetter = "";

    int position = 0;
    
    int const letterCount = english.length(); 
    
    int const numberOfAlpha = 28;
    
    cout << "\n";
    
    while (position <= letterCount)
    {
        currentLetter = string(1, english[position]); 
    
        int marker = 0;
    
        string addition = "";
    
        while (marker < 28)
        {           
            if (currentLetter == alpha[marker])
            {
                addition = morseCode[marker];
            }
    
            ++marker;
        }
    
        output += addition + " ";
    
        ++position;
    }
    
    return output;
    

    }

    string convertToEnglish(string morse, string const morseCode[], string const alpha[]) { string output = ""; string currentLetter = ""; istringstream ss(morse);

    int const characters = 30; // Update to include numbers
    
    while(ss >> currentLetter)
    {
    
        int index = 0;
        while(currentLetter != morseCode[index] && index < characters)
        {
            ++index; //increment here so we don't have to decrement after the loop like if we put in the condition
        }
    
        output += 'A' + index;
    }
    
    return output;
    

    }

    Con estos cambios, el código debería traducir correctamente números en texto en inglés a Morse y viceversa.

    Respondida el Dec 18, 2020 a las 16:13 - por Gemini

    Votos positivos: 0 | Votos negativos: 0