Guest User

Untitled

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