Guest User

Untitled

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