Guest User

Untitled

a guest
Jan 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // Declare the "morse letter map"
  6. //
  7. // Both these "arrays" contain the same number of items
  8. // (A string is essentially an array of characters).
  9. // Each item in the morse array represent the item (letter)
  10. // in the same position in the alphabet array.
  11. //
  12. // The reason we use this method is because we have a
  13. // big variation of letters to translate (åäö not being
  14. // right next to the rest of the letters. If they were
  15. // we could have used a more optimal solution)
  16. string alphabet ="abcdefghijklmnopqrstuvwxyz\x8F\x8E\x99";
  17. string morse[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",".--.-",".-.-","---."};
  18.  
  19. // Translates a normal character to it's morse string representation.
  20. // The function assumes the letter is already in uppercase to match our alphabet.
  21. string morseFromLetter(char letter)
  22. {
  23.     // Make sure we have the lowercase version of the letter,
  24.     // since our alphabet is in lowercase (this practically means that
  25.     // the program will not make any difference between cases)
  26.     char lowercaseLetter = tolower(letter);
  27.  
  28.     // Go through the alphabet and find index of the letter we want to translate.
  29.     // We will use this index to select the correct morse representation from the morse array.
  30.     int alphabetLength = alphabet.length();
  31.     for(int i = 0; i < alphabetLength; i++)
  32.     {
  33.         // Compare the current alphabet letter to the input letter,
  34.         // if they match we have found the correct index.
  35.         if(alphabet[i] == lowercaseLetter)
  36.         {
  37.             // Select the translation from the morse
  38.             // array and have the function return it
  39.             return morse[i];
  40.         }
  41.     }
  42.  
  43.     // Reaching this point means that the letter wasn't found in the alphabet we have defined.
  44.     // In this case we will return an empty string, since we don't know what else to return.
  45.     return "";
  46. }
  47.  
  48. // Translates a normal string of text
  49. // to it's morse string representation
  50. string morseFromText(string text, string padding = " ")  
  51. {
  52.     // Retreive the length of the text
  53.     int textLength = text.length();
  54.  
  55.     // This variable will get each letters translation added to it,
  56.     // and of course we then want to start with an empty string
  57.     string textTranslation = "";
  58.  
  59.     // Go through each letter of the text (one letter per loop)
  60.     for(int i = 0; i < textLength; i++)
  61.     {
  62.         // Retreive the letter of the current loop
  63.         char currentLetter = text[i];
  64.  
  65.         // Send currentLetter to our letter translation
  66.         // function to get the morse representation of the
  67.         // letter and add the translation of the current
  68.         // letter to the full text translation variable
  69.         textTranslation += morseFromLetter(currentLetter);
  70.  
  71.         // Add the padding text that we want
  72.         // to have between every morse string
  73.         textTranslation += padding;
  74.     }
  75.  
  76.     // Have the function return the result,
  77.     // the text translated into morse code
  78.     return textTranslation;
  79. }
  80.  
  81. int main()
  82. {
  83.     // These are the variables we will need for the loop,
  84.     // also it looks a lot neater to have them here.
  85.     string input, translation;
  86.  
  87.     // Start by welcoming the user and giving
  88.     // instructions on how to use the program
  89.     cout << "Welcome to the morse code translator!" << endl << endl
  90.          << "After '>', type any text you want to translate." << endl
  91.          << "Press Enter to receive the translation." << endl << endl
  92.          << "To end the program simply submit 'quit'." << endl << endl
  93.          << "Have fun!" << endl;
  94.  
  95.     // Loop until "further notice" :)
  96.     while(true)
  97.     {
  98.         // Have the user input something to translate
  99.         cout << "> ";
  100.         getline(cin, input);
  101.  
  102.         // Check if the user wants to quit the program.
  103.         if(input == "quit")
  104.             break; // Stop looping if a quit request is detected
  105.        
  106.         // Translate what the user input into morse code
  107.         translation = morseFromText(input);
  108.  
  109.         // Output the translation to the user
  110.         cout << "= " << translation << endl << endl;
  111.  
  112.         // Loop again!
  113.     }
  114.  
  115.     // Require the user to press any key
  116.     // before finally ending the program
  117.     system("pause");
  118. }
Add Comment
Please, Sign In to add comment