Проблема с пробелами Морзе между словами - PullRequest
1 голос
/ 14 октября 2019

У меня проблемы с попыткой заставить мой код преобразовать символ пробела в 'xx'. Я установил его так, что после каждой буквы есть буква x, чтобы отделить буквы, но я не могу получить то, что у меня есть ниже, чтобы работать для пробела между словами.

#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;

string translate(string word)
{
    string morseCode[] = { ".-x", "-...x", "-.-.x", "-..x", ".x", "..-.x",
    "--.x", "....x", "..x", ".---x", "-.-x", ".-..x", "--x", "-.x", "---x",
    ".--.x", "--.-x", ".-.x", "...x", "-x", "..-x", "...-x", ".--x", "-..-x",
    "-.--x", "--..x" };
    char ch;
    string morseWord = " ";
    //string morseWord = " " == "xx";


    for (unsigned int i = 0; i < word.length(); i++)
    {
        if (isalpha(word[i]))
        {
            ch = word[i];
            ch = toupper(ch);
            morseWord += morseCode[ch - 'A'];
            morseWord += morseCode[ch = ' '] == "xx";
            //morseWord += "xx";
            //morseWord += " " == "xx";
        }

    }
    return morseWord;
}

int main()
{
    stringstream stringsent;
    string sentence;
    string word = "";

    cout << "Please enter a sentence: ";
    getline(cin, sentence);
    stringsent << sentence;
    cout << "The morse code translation for that sentence is: " << endl;
    while (stringsent >> word)
        cout << translate(word) << endl;
    system("pause");
    return 0;
}


1 Ответ

1 голос
/ 14 октября 2019

Я закомментировал все ненужные биты.

#include <iostream>
// #include <cstring>
// #include <sstream>
#include <ccytpe>  // You were relying on an include dependency; this is the
                   // library that contains isalpha() 
using namespace std;

string translate(string word)
{
    string morseCode[] = { ".-x", "-...x", "-.-.x", "-..x", ".x", "..-.x",
    "--.x", "....x", "..x", ".---x", "-.-x", ".-..x", "--x", "-.x", "---x",
    ".--.x", "--.-x", ".-.x", "...x", "-x", "..-x", "...-x", ".--x", "-..-x",
    "-.--x", "--..x" };
    char ch;
    string morseWord = " ";
    //string morseWord = " " == "xx";


    for (unsigned int i = 0; i < word.length(); i++)
    {
        if (isalpha(word[i]))
        {
            ch = word[i];
            ch = toupper(ch);
            morseWord += morseCode[ch - 'A'];
            // morseWord += morseCode[ch = ' '] == "xx";  // Having a space 
                                                          // character is 
                                                          // impossible here
            //morseWord += "xx";
            //morseWord += " " == "xx";
        }
        else if (isspace(word[i])) // True for any whitespace character
        {
            morseWord += "xx";
        }

    }
    return morseWord;
}

int main()
{
    // stringstream stringsent;
    string sentence;
    // string word = ""; // should just be 'string word;' 
                         // Default constructed strings are already empty

    cout << "Please enter a sentence: ";
    getline(cin, sentence);
    // stringsent << sentence;
    cout << "The morse code translation for that sentence is: " << endl;
        cout << translate(sentence) << endl;
    return 0;
}

Ваша проблема была двоякой. Пробел не является алфавитным, поэтому ни один пробел не может войти в ваш блок if. Во-вторых, отправляя только одно слово за раз, вы даже никогда не отправляли пробелы для начала.

Вот пример вывода из приведенного выше кода:

Please enter a sentence: hello world
The morse code translation for that sentence is: 
 ....x.x.-..x.-..x---xxx.--x---x.-.x.-..x-..x
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...