Advertisement
Marcos510

Untitled

Nov 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <windows.h>
  4.  
  5.  
  6.  
  7.    std::string engtomol (std::string, std::string[]);
  8.    std::string moltoeng (std::string, std::string[]);
  9.  
  10.   int main ()
  11.   {
  12.       std::string morse[39] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
  13.       ".---", "-.-", ".-..", "--.", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
  14.       "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
  15.       "-....", "--...", "---..", "----.", "-----", ".-.-.-", "--..--", "..--.."};
  16.           std::string text, morsecode;
  17.           char choice;
  18.           char repeat='y';
  19.  
  20.           while (repeat=='y')
  21.           {
  22.  
  23.  
  24.                 std::cout << " A = .- \t N = -. \t 1 = .---- \n";
  25.                 std::cout << " B = -... \t O = --- \t 2 = ..--- \n";
  26.                 std::cout << " C = -.-. \t P = .--. \t 3 = ...-- \n";
  27.                 std::cout << " D = -.. \t Q = --.- \t 4 = ....- \n";
  28.                 std::cout << " E = . \t\t R = .-. \tv  5 = ..... \n";
  29.                 std::cout << " F = ..-. \t S = ... \t 6 = -.... \n";
  30.                 std::cout << " G = --. \t T = - \t\t 7 = --... \n";
  31.                 std::cout << " H = .... \t U = ..- \t 8 = ---.. \n";
  32.                 std::cout << " I = .. \t V = ...- \t 9 = ----. \n";
  33.                 std::cout << " J = .--- \t W = .-- \t 0 = ----- \n";
  34.                 std::cout << " K = -.- \t X = -..- \t Stop = .-.-.- \n";
  35.                 std::cout << " L = .-.. \t Y = -.-- \t , = --..-- \n";
  36.                 std::cout << " M = -- \t Z = --.. \t ? = ..--.. \n";
  37.  
  38.  
  39.  
  40.                 std::cout << "\n";
  41.                 std::cout << "Enter 1 to decode English to Morse\nEnter 2 to decode Morse code to English\n";
  42.                 std::cin >> choice;
  43.  
  44.                           if (choice=='1')
  45.                           {
  46.                                   std::cout << "Enter text to translate: ";
  47.                                   std::cin.get();
  48.                                   getline(std::cin,text);
  49.                   //              cout << "TEXT: " << text << endl;
  50.                                   std::cout << "MORSE CODE: " << engtomol(text, morse) << "\n";
  51.                           }
  52.                           else if (choice=='2')
  53.                           {
  54.                           std::cout << "Enter your morse code, space in between each letter.\n";
  55.                           std::cout << "If multiple words, add 3 spaces after each word: ";
  56.                           std::cin.get();
  57.                           getline(std::cin,morsecode);
  58.                   //      cout << "MORSECODE: " << morsecode << endl;
  59.                           std::cout << "TEXT: " << moltoeng (morsecode, morse) << "\n";
  60.                           }
  61.  
  62.           std::cout << "Would you like to try again? Enter y to repeat. Enter any other key to exit. ";
  63.           std::cin >> repeat;
  64.           }
  65.  
  66.   return 0;
  67.   }
  68.  
  69.   // this function converts from English to Morse
  70.   std::string engtomol (std::string text, std::string morse[])
  71.   {
  72.       int textlength = text.length();
  73.       std::string morsevalue;
  74.       std::string spacesbtwletters= " ";
  75.       std::string spacesbtwwords = "  ";//2 spaces because i am going to add it with spacesbtwletters so that it will = 3
  76.                   for (int k=0; k<textlength; k++)
  77.                   {
  78.                           if (text[k]!= ' ') //if the word(s) did not encounter a space
  79.                           {   text[k]=toupper(text[k]); //upper case letters and lower case letters are the same hence have the same appropriate morse code.
  80.                           morsevalue = spacesbtwletters+= morse[text[k]-'A']+"   ";
  81.                           }
  82.                   if (text[k]==' ')
  83.                   {
  84.                   spacesbtwletters+=spacesbtwwords;//adds 3 spaces when there is a space between words
  85.                   }
  86.       }
  87.   return morsevalue;
  88.   }
  89.  
  90.   std::string moltoeng(std::string morsecode, std::string morse[])
  91.   {
  92.       std::string english[39] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
  93.       "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2",
  94.       "3", "4", "5", "6", "7", "8", "9", "0", "Stop", ",", "?"};
  95.       std::string output = ""; //output for function
  96.       int index = 0;
  97.       while(index < morsecode.size())
  98.       {
  99.           //find current morse code
  100.           std::string letter = "";
  101.           int x = 0;
  102.           while(morsecode[index] == ' ')
  103.           {
  104.                   x++;
  105.                   index++;
  106.                   if(x==3){
  107.                           output+= ' ';
  108.                   break;
  109.                   }
  110.           }
  111.           while(morsecode[index] != ' ')
  112.           {
  113.           letter += morsecode[index++];
  114.           } //find this code in the morse alphabet
  115.           int position = 0;
  116.           for(position=0; position < 39; position++)
  117.                   {
  118.                           if(morse[position]==letter)
  119.                           {
  120.                           output+=english[position];
  121.                           ++index; //to get rid of the space character.
  122.                           break;
  123.                   }
  124.                   }
  125.   }
  126.  return output;
  127.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement