Guest User

Untitled

a guest
Jan 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 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åäö";
  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 uppercase version of the letter,
  24.     // since our alphabet is in uppercase (this practically means that
  25.     // the program will not make any difference between cases)
  26.     char uppercaseLetter = toupper(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] == uppercaseLetter)
  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 letter
  67.         string letterTranslation = morseFromLetter(currentLetter);
  68.  
  69.         // Add the translation of the current letter
  70.         // to the full text translation variable
  71.         textTranslation += letterTranslation;
  72.  
  73.         // Add the padding text that we want
  74.         // to have between every morse string
  75.         textTranslation += padding;
  76.     }
  77.  
  78.     // Have the function return the result,
  79.     // the text translated into morse code
  80.     return textTranslation;
  81. }
  82.  
  83. int main()
  84. {
  85.     // These are the variables we will need for the loop,
  86.     // also it looks a lot neater to have them here.
  87.     string input, translation;
  88.  
  89.     // Start by welcoming the user and giving
  90.     // instructions on how to use the program
  91.     cout << "Welcome to the morse code translator!" << endl << endl
  92.          << "After '>', type any text you want to translate " << endl
  93.          << "and press Enter to receive the translation. " << endl << endl
  94.          << "To end the program simply submit 'quit'." << endl << endl
  95.          << "Have fun!" << endl;
  96.  
  97.     // Loop until "further notice" :)
  98.     while(true)
  99.     {
  100.         // Have the user input something to translate
  101.         cout << "> ";
  102.         cin >> input;
  103.  
  104.         // Check if the user wants to quit the program.
  105.         if(input == "quit")
  106.             break; // Stop looping if a quit request is detected
  107.        
  108.         // Translate what the user input into morse code
  109.         translation = morseFromText(input);
  110.  
  111.         // Output the translation to the user
  112.         cout << "= " << translation << endl << endl;
  113.  
  114.         // Loop again!
  115.     }
  116.  
  117.     // Require the user to press any key
  118.     // before finally ending the program
  119.     system("pause");
  120. }
Add Comment
Please, Sign In to add comment