Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int again = 1, choice, stringLength, start, finish;
  8. string morseCode[39] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",".----","..---","...--","....-",".....","--...","---..","----.","-----",".-.-.-","-....","--..--","..--.."};
  9. string alphabet[39] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0","Stop",",","?"};
  10. string englishToMorse, morseToEnglish, anotherString, yetAnotherString;
  11. while(again == 1)
  12. {
  13. anotherString = "";
  14. cout << "\n\n\nPlease enter the number corresponding to your choice...\n"
  15. << "1) English to Morse\n"
  16. << "2) Morse to English\n"
  17. << "3) Quit\n";
  18. cin >> choice;
  19. cout << string(50, '\n');
  20.  
  21. switch(choice)
  22. {
  23. case(1):
  24. cout << "Input English sentence to be translated..." << endl;
  25. cin.ignore();
  26. getline(cin, englishToMorse);
  27. stringLength = englishToMorse.length();
  28. cout << string(50, '\n');
  29. cout << "\t\t( / REPRESENTS A SPACE BETWEEN WORDS )" << endl;
  30. cout << "\nYour sentence: " << englishToMorse << endl;
  31. cout << "The translation: ";
  32. for(int n = 0; n < stringLength; n++)
  33. {
  34. englishToMorse[n] = toupper(englishToMorse[n]);
  35. anotherString = englishToMorse[n];
  36. if(anotherString == " ")
  37. {
  38. cout << "/ ";
  39. }
  40. for(int i = 0; i < 39; i++)
  41. {
  42. if(anotherString == alphabet[i])
  43. {
  44. cout << morseCode[i] << " ";
  45. }
  46. }
  47. }
  48. break;
  49. case(2):
  50. cout << "Input Morse Code to be translated..." << endl;
  51. cin.ignore();
  52. getline(cin, morseToEnglish);
  53. stringLength = morseToEnglish.length();
  54. cout << string(50, '\n');
  55. cout << "\t\t( USE / TO SEPERATE WORDS )" << endl;
  56. cout << "\nYour code: " << morseToEnglish << endl;
  57. cout << "The translation: ";
  58. start = 0;
  59. for(int n = 0; n < stringLength; n++)
  60. {
  61. anotherString = morseToEnglish[n];
  62. if(anotherString == " ")
  63. {
  64. finish = n;
  65. yetAnotherString = morseToEnglish.substr(start, finish);
  66. for(int i = 0; i < 39; i++)
  67. {
  68. if(yetAnotherString == morseCode[i])
  69. {
  70. cout << alphabet[i];
  71. yetAnotherString = "";
  72. }
  73. }
  74. }
  75. start = finish;
  76. }
  77. break;
  78. case(3):
  79. again = 0;
  80. break;
  81. }
  82. }
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement