单词之间的摩尔斯电码空格问题

Trouble with morse code spaces between words

我在尝试让我的代码将 space 字符转换为 'xx' 时遇到问题。我已经设置好了,所以在每个字母之后都有一个 x 来分隔字母,但我无法完全理解下面的内容来处理单词之间的 space。

#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;
}


我已经注释掉了所有不必要的部分。

#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;
}

您的问题有两个方面。 space 字符不是字母顺序的,因此没有 space 字符可以进入您的 if 块。其次,一次只发送一个单词,你甚至从来没有发送过 space 个字符。

下面是上述代码的示例输出:

Please enter a sentence: hello world
The morse code translation for that sentence is: 
 ....x.x.-..x.-..x---xxx.--x---x.-.x.-..x-..x