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

标签 c++ string stl containers morse-code

我在尝试让我的代码将空格字符转换为“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;
}


最佳答案

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

#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 block 。其次,在一次只发送一个单词的情况下,您甚至从来没有发送空格字符作为开头。

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

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

关于c++ - 单词之间的摩尔斯电码空格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58381647/

相关文章:

swift - Swift 中的 cos⁻¹ 按钮标题

javascript字符串替换为数组值的所有出现

c++ - 如何在ARM Cortex-M芯片上使用STL?

c++ - 输出迭代器适配器计数但不复制

c++ - 有没有办法强制使用shared_ptr在堆上创建对象?

c++ - 循环内容器的性能

c++ - 如何从 std::visit 返回常量?

c++ - 在 C++ 中,如何在没有动态分配(即 NEW)的情况下将对象初始化为对象内部的指针?

c++ - 从迭代器转换为定义的类型?

C# float.Parse 字符串