简体   繁体   中英

How can I add space while decoding Morse code

I have made a program in c++ for encoding and decoding morse code. My program is working and the decoded message is fine but without spaces is there any way to add space. 在此处输入图片说明

#include <iostream>
#include <string>
using namespace std;
string translateMorseCode(string sentence);
string decoceMorseCode (string sentence);
int main()
{
 string sentence;
 cout<<"Enter word or sentence: ";
 getline(cin,sentence);
 cout<<"\nMorse Code is:\n";
 //convert input message into morse
 cout<<translateMorseCode(sentence)<<endl;

 //copying morse code into decode string for decoding 
 string decode = translateMorseCode(sentence);
 cout<<"\nDecoding morse code is text"<<endl;
 //converting back ito text string
 cout<<decoceMorseCode (decode);
 return 0;
}


string decoceMorseCode (string sentence)
{

string delimiter = " ";
string decode ="";
int pos = 0;
string token;

while ((pos = sentence.find(delimiter)) != string::npos) {
    token = sentence.substr(0, pos);

    if(token==".-")
    {
        decode.append("a");
    }
    else if(token=="-...")
    {
        decode.append("b");
    }

    else if(token=="-.-.")
    {
        decode.append("c");
    }
    else if(token=="-..")
    {
        decode.append("d");
    }
        else if(token==".")
    {
        decode.append("e");
    }
        else if(token=="..-.")
    {
        decode.append("f");
    }
        else if(token=="--.")
    {
        decode.append("g");
    }
    else if(token=="....")
    {
        decode.append("h");
    }
    else if(token=="..")
    {
        decode.append("i");
    }
    else if(token==".---")
    {
        decode.append("j");
    }
    else if(token=="-.-")
    {
        decode.append("k");
    }
    else if(token==".-..")
    {
        decode.append("l");
    }
    else if(token=="--")
    {
        decode.append("m");
    }
        else if(token=="-.")
    {
        decode.append("n");
    }
    else if(token=="---")
    {
        decode.append("o");
    }
 else if(token==".--.")
    {
        decode.append("p");
    }
    else if(token=="--.-")
    {
        decode.append("q");
    }
    else if(token==".-.")
    {
        decode.append("r");
    }
    else if(token=="...")
    {
        decode.append("s");
    }
    else if(token=="-")
    {
        decode.append("t");
    }
   else if(token=="..-")
    {
        decode.append("u");
    }
   else if(token=="...-")
    {
        decode.append("v");
    }
    else if(token==".--")
    {
        decode.append("w");
    }
    else if(token=="-..-")
    {
        decode.append("x");
    }
        else if(token=="-.--")
    {
        decode.append("y");
    }
    else if(token=="--..")
    {
        decode.append("z");
    }
    else if(token=="-----")
    {
        decode.append("0");
    }
        else if(token==".----")
    {
        decode.append("1");
    }
    else if(token=="..---")
    {
        decode.append("2");
    }
    else if(token=="...--")
    {
        decode.append("3");
    }
    else if(token=="....-")
    {
        decode.append("4");
    }
    else if(token==".....")
    {
        decode.append("5");
    }
    else if(token=="-....")
    {
        decode.append("6");
    }
        else if(token=="--...")
    {
        decode.append("7");
    }
    else if(token=="---..")
    {
        decode.append("8");
    }
    else if(token=="----.")
    {
        decode.append("9");
    }

    sentence.erase(0,pos + delimiter.length());

}

return decode ; // returnung decoded text 
}




//function convert input message into morse return Morse Code as String
string translateMorseCode(string sentence)
{
 string MorseCode="";
 for(int i=0;i<sentence.length();i++){
 switch (sentence[i]){
 case 'a':
 case 'A':
 MorseCode.append(".- ");
 break;
 case 'b':
 case 'B':
 MorseCode.append("-... ");
 break;
 case 'c':
 case 'C':
 MorseCode.append("-.-. ");
 break;
 case 'd':
 case 'D':
 MorseCode.append("-.. ");
 break;
 case 'e':
 case 'E':
 MorseCode.append(". ");
 break;
 case 'f':
 case 'F':
 MorseCode.append("..-. ");
 break;
 case 'g':
 case 'G':
 MorseCode.append("--. ");
 break;
 case 'h':
 case 'H':
 MorseCode.append(".... ");
 break;
 case 'i':
 case 'I':
 MorseCode.append(".. ");
 break;
 case 'j':
 case 'J':
 MorseCode.append(".--- ");
 break;
 case 'k':
 case 'K':
 MorseCode.append("-.- ");
 break;
 case 'l':
 case 'L':
 MorseCode.append(".-.. ");
 break;
 case 'm':
 case 'M':
 MorseCode.append("-- ");
  break;
 case 'n':
 case 'N':
 MorseCode.append("-. ");
 break;
 case 'o':
 case 'O':
 MorseCode.append("--- ");
 break;
 case 'p':
 case 'P':
 MorseCode.append(".--. ");
 break;
 case 'q':
 case 'Q':
 MorseCode.append("--.- ");
 break;
 case 'r':
 case 'R':
 MorseCode.append(".-. ");
 break;
 case 's':
 case 'S':
 MorseCode.append("... ");
 break;
 case 't':
 case 'T':
 MorseCode.append("- ");
 break;
 case 'u':
 case 'U':
 MorseCode.append("..- ");
 break;
 case 'v':
 case 'V':
 MorseCode.append("...- ");
 break;
 case 'w':
 case 'W':
 MorseCode.append(".-- ");
 break;
 case 'x':
 case 'X':
 MorseCode.append(".-- ");
 break;
 case 'y':
 case 'Y':
 MorseCode.append("-.-- ");
 break;
 case 'z':
 case 'Z':
 MorseCode.append("--.. ");
 break;
 case ' ':
 MorseCode.append(" ");
 break;
 case '1':
 MorseCode.append(".---- ");
 break;
 case '2':
 MorseCode.append("..--- ");
 break;
 case '3':
 MorseCode.append("...-- ");
 break;
 case '4':
 MorseCode.append("....- ");
 break;
 case '5':
 MorseCode.append("..... ");
 break;
 case '6':
 MorseCode.append("-.... ");
 break;
 case '7':
 MorseCode.append("--... ");
 break;
 case '8':
 MorseCode.append("---.. ");
 break;
 case '9':
 MorseCode.append("----. ");
 break;
 case '0':
 MorseCode.append("----- ");
 break;
}
}
 return MorseCode;// return Morse Code
}   

The problem is that you use a double space in your morse output to encode a word boundary, but your decoder skips all spaces. It therefore skips double spaces too, which is why it doesn't know where to put a space in the decoded output.

According to wiki:

Each character (letter or numeral) is represented by a unique sequence of dots and dashes. The duration of a dash is three times the duration of a dot. Each dot or dash is followed by a short silence, equal to the dot duration. The letters of a word are separated by a space equal to three dots (one dash), and the words are separated by a space equal to seven dots. The dot duration is the basic unit of time measurement in code transmission.[1] To increase the speed of the communication, the characters are encoded so the length of each character in Morse is approximately inversely proportional to its frequency of occurrence in English. Thus, the most common letter in English, the letter "E," has the shortest code, a single dot.

So in my opinion: Two "\\s" should be between words. One "\\s" between single char.

@Neska gave you the basic answer but did not explain where the space gets interpreted.

When you get the tag decoded your logic should be able to detect the silence between the dots and dashes. Otherwise, it would not be able to determine where an individual character ends and the next character begins. That logic should detect the longer (seven dots long) interval between the end of a word and the beginning of the next work. Put in a special character (not a . or a dash) to be interpreted as a space in your morse code interpreter.

If you are interpreting text input and searching for the next delimiter (such as a space) in order to translate the dots and dashes, output the delimiters as well. That will put in the appropriate number of spaces

For example

.-^-^^-...^-.--

translates to

at^by

where the ^ character shows where the space should be.

Your code translates this as atby

I have found a solution by myself and it was quite simple - I just added

else if (" ")
    {
        decode.append(" ");
    }

before the

 sentence.erase(0,pos + delimiter.length());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM