Guest User

Untitled

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