0

我是地图新手,地图似乎是解决这个问题的好主意。我想提供一个输入并通过地图运行该输入,然后输出相关的值。有可能按照这些思路做点什么吗?

#include <iostream>
#include <map>
#include <string>

int main()
{
std::string word;
word ="1110001010101000100000001110111010111000101011100010100011101011101000111010111";
//std::cout << "Please, enter your phrase: ";
//std::getline (std::cin,word);


// char inputstring[100];
//while(std::cin.good()){

std::map <std::string, char> morse_char;
morse_char["000"] = ' ';
morse_char["10111"] = 'a';
morse_char["111010101"] = 'b';
morse_char["11101011101"] = 'c';
morse_char["1110101"] = 'd';
morse_char["1"] = 'e';
morse_char["101011101"] = 'f';
morse_char["111011101"] = 'g';
morse_char["1010101"] = 'h';
morse_char["101"] = 'i';
morse_char["1011101110111"] = 'j';
morse_char["111010111"] = 'k';
morse_char["101110101"] = 'l';
morse_char["1110111"] = 'm';
morse_char["11101"] = 'n';
morse_char["11101110111"] = 'o';
morse_char["10111011101"] = 'p';
morse_char["1110111010111"] = 'q';
morse_char["1011101"] = 'r';
morse_char["10101"] = 's';
morse_char["111"] = 't';
morse_char["1010111"] = 'u';
morse_char["101010111"] = 'v';
morse_char["1110111"] = 'w';
morse_char["11101010111"] = 'x';
morse_char["1110101110111"] = 'y';
morse_char["11101110101"] = 'z';



//}
std::cout << word << std::endl;


return 0;
}
4

2 回答 2

0

我想你可以看看这个问题的来源

试图将莫尔斯电码转换为英文。挣扎

基本思想是您将您的word与每个莫尔斯电码进行比较并找到匹配项。

于 2013-09-17T00:36:53.393 回答
0

这最终成为我正在寻找的那种解决方案:

#include <iostream>
#include <map>
#include <string>
#include <vector>

 std::vector<std::string> split( const std::string& code, const std::string& sep )
 {
    std::vector<std::string> result ;

    std::string::size_type pos = 0 ;
    auto f = code.find(sep) ;
    while( f != std::string::npos )
    {
        result.emplace_back( code.begin()+pos, code.begin()+f ) ;
        pos = f + sep.size() ;
        f = code.find( sep, pos ) ;
    }
    result.push_back( code.substr(pos) ) ;

    return result ;
 }

 int main()
 {
    typedef std::map <std::string, char> morse_charT;
    morse_charT my_morse;
    my_morse["000"] = ' ';
    my_morse["10111"] = 'a';
    my_morse["111010101"] = 'b';
    my_morse["11101011101"] = 'c';
    my_morse["1110101"] = 'd';
    my_morse["1"] = 'e';
    my_morse["101011101"] = 'f';
    my_morse["111011101"] = 'g';
    my_morse["1010101"] = 'h';
    my_morse["101"] = 'i';
    my_morse["1011101110111"] = 'j';
    my_morse["111010111"] = 'k';
    my_morse["101110101"] = 'l';
    my_morse["1110111"] = 'm';
    my_morse["11101"] = 'n';
    my_morse["11101110111"] = 'o';
    my_morse["10111011101"] = 'p';
    my_morse["1110111010111"] = 'q';
    my_morse["1011101"] = 'r';
    my_morse["10101"] = 's';
    my_morse["111"] = 't';
    my_morse["1010111"] = 'u';
    my_morse["101010111"] = 'v';
    my_morse["1110111"] = 'w';
    my_morse["11101010111"] = 'x';
    my_morse["1110101110111"] = 'y';
    my_morse["11101110101"] = 'z';

    const std::string word_seperator = "0000000" ;
    const std::string letter_seperator = "000" ;

    const std::string msg =    
"1110001010101000100000001110111010111000101011100010100011101011101000111010111" ;

    for( const std::string& word : split( msg, word_seperator ) )
    {

        for( const std::string& letter : split( word, letter_seperator ) )
        {

            morse_charT::iterator  it= my_morse.find(letter);
            if( it != my_morse.end() ) std::cout  << it->second;

        }
    }
于 2013-09-18T02:53:42.737 回答