Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * English to Morse converter
- * 24/09/2011
- * This program takes an English string and converts it to Morse code
- */
- #include <iostream>
- #include <map>
- #include <string>
- #include <cctype>
- using namespace std;
- void buildMorseMap();
- void EnglishToMorse(string input);
- map <std::string, std::string> morseMap;
- int main(int argc, char **argv)
- {
- buildMorseMap();
- string input;
- cout<<"Write one sentence: ";
- getline(cin,input); // Read in a whole line of text from the keyboard - this won't break if you use spaces as well
- EnglishToMorse(input);
- return (0);
- }
- // This function does the actual conversion
- void EnglishToMorse(string input)
- {
- // Convert the whole string to uppercase because there are no map entries for the lowercase originals
- for (int i=0; i<input.length(); i++)
- {
- input.at(i) = toupper(input.at(i));
- }
- string completedMorse; // Temporary string in which to store the completed Morse string for output
- for (int i=0; i<input.length(); i++)
- {
- string temp(1, input.at(i)); // Use a temporary string variable for the lookups, because the .at() function returns a primitive char rather than a string
- completedMorse.append(morseMap[temp]);
- completedMorse.append(" "); // Put a space in between each part of the Morse output
- }
- cout<<"Morse: "<<completedMorse<<endl;
- }
- // This function builds the Map container for storing the letter->Morse conversions
- // This acts as a "lookup" table i.e. you can ask what the morse value for 'A' is and it'll tell you
- // Mappings referred to at http://tcfreenet.org/people/calguire/morse.html
- void buildMorseMap()
- {
- morseMap.insert(std::make_pair("A",".-"));
- morseMap.insert(std::make_pair("B","-..."));
- morseMap.insert(std::make_pair("C","-.-."));
- morseMap.insert(std::make_pair("D","-.."));
- morseMap.insert(std::make_pair("E","."));
- morseMap.insert(std::make_pair("F","..-."));
- morseMap.insert(std::make_pair("G","--."));
- morseMap.insert(std::make_pair("H","...."));
- morseMap.insert(std::make_pair("I",".."));
- morseMap.insert(std::make_pair("J",".---"));
- morseMap.insert(std::make_pair("K","-.-"));
- morseMap.insert(std::make_pair("L",".-.."));
- morseMap.insert(std::make_pair("M","--"));
- morseMap.insert(std::make_pair("N","-."));
- morseMap.insert(std::make_pair("O","---"));
- morseMap.insert(std::make_pair("P",".--."));
- morseMap.insert(std::make_pair("Q","--.-"));
- morseMap.insert(std::make_pair("R",".-."));
- morseMap.insert(std::make_pair("S","..."));
- morseMap.insert(std::make_pair("T","-"));
- morseMap.insert(std::make_pair("U","..-"));
- morseMap.insert(std::make_pair("V","...-"));
- morseMap.insert(std::make_pair("W",".--"));
- morseMap.insert(std::make_pair("X","-..-"));
- morseMap.insert(std::make_pair("Y","-.--"));
- morseMap.insert(std::make_pair("Z","--.."));
- morseMap.insert(std::make_pair("1",".----"));
- morseMap.insert(std::make_pair("2","..---"));
- morseMap.insert(std::make_pair("3","...--"));
- morseMap.insert(std::make_pair("4","....-"));
- morseMap.insert(std::make_pair("5","....."));
- morseMap.insert(std::make_pair("6","-...."));
- morseMap.insert(std::make_pair("7","--..."));
- morseMap.insert(std::make_pair("8","---.."));
- morseMap.insert(std::make_pair("9","----."));
- morseMap.insert(std::make_pair("0","-----"));
- morseMap.insert(std::make_pair(",","--..--"));
- morseMap.insert(std::make_pair(".",".-.-.-"));
- morseMap.insert(std::make_pair("?","..--.."));
- morseMap.insert(std::make_pair(";","-.-.-"));
- morseMap.insert(std::make_pair(":","---..."));
- morseMap.insert(std::make_pair("/","-..-."));
- morseMap.insert(std::make_pair("-","-....-"));
- morseMap.insert(std::make_pair("()","-.--.-"));
- morseMap.insert(std::make_pair("_","..--.-"));
- }
Advertisement
Add Comment
Please, Sign In to add comment