摩斯码转化器

Write a program that can translate Morse code in the format of ...---...
A space and a slash will be placed between words. ..- / --.-
For bonus, add the capability of going from a string to Morse code.
Super-bonus if your program can flash or beep the Morse.
This is your Morse to translate:
.... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--

#include <map>
using namespace std;

map<string, char> gMorse;

void Decrypt(const string morse, string &str){
    string code;
    for (int i = 0; i < morse.size(); ++i){
        if (morse[i] != '/'){
            if (morse[i] == ' '){
                str += gMorse[code];
                code = "";
            }
            else{
                code.push_back(morse[i]);
            }
        }
        else{
            str += " ";
            ++i;
        }
    }
    //get last char
    str += gMorse[code];
}

int main(){

    gMorse[".-"] = 'A';
    gMorse["-..."] = 'B';
    gMorse["-.-."] = 'C';
    gMorse["-.."] = 'D';
    gMorse["."] = 'E';
    gMorse["..-."] = 'F';
    gMorse["--."] = 'G';
    gMorse["...."] = 'H';
    gMorse[".."] = 'I';
    gMorse[".---"] = 'J';
    gMorse["-.-"] = 'K';
    gMorse[".-.."] = 'L';
    gMorse["--"] = 'M';
    gMorse["-."] = 'N';
    gMorse["---"] = 'O';
    gMorse[".--."] = 'P';
    gMorse["--.-"] = 'Q';
    gMorse[".-."] = 'R';
    gMorse["..."] = 'S';
    gMorse["-"] = 'T';
    gMorse["..-"] = 'U';
    gMorse["...-"] = 'V';
    gMorse[".--"] = 'W';
    gMorse["-..-"] = 'X';
    gMorse["-.--"] = 'Y';
    gMorse["--.."] = 'Z';


    string morse(".... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--");
    string result;
    Decrypt(morse, result);

    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容