Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. /**
  2. *
  3. * Solution to homework task 01
  4. * Introduction to programming course
  5. * Faculty of Mathematics and Informatics of Sofia University
  6. * 2017/2018 academic year
  7. *
  8. * @author Lilyana Stoyeva
  9. * @idnumber 855286
  10. * @task 1
  11. * @compiler GCC
  12. *
  13. */
  14.  
  15. #include <iostream>
  16. #include <cstring>
  17.  
  18. using namespace std;
  19.  
  20. void ToUper(char& c) // we need to increase all letters in message, because Morse code is the same for small and capital letters
  21. {
  22. if(c >= 'a' && c <= 'z')
  23. c = c - ('a' - 'A');
  24. }
  25. bool isAlpha(char c) // check if the symbol in message is the letter
  26. {
  27. if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
  28. return true;
  29.  
  30. return false;
  31. }
  32. bool isNumber(char c) // check if the symbol in message is number
  33. {
  34. if(c >= '0' && c <= '9')
  35. return true;
  36.  
  37. return false;
  38. }
  39. void MorseCodeForAlpha(char c) // function for converting all letters to unique sequence of dot and dashes
  40. {
  41. const char alpha[] = {'A','B','C','D','E','F','G','H','I',
  42. 'J','K','L','M','N','O','P','Q','R',
  43. 'S','T','U','V','W','X','Y','Z'};
  44. const char* morse[] = {".-","-...","-.-.","-..",".","..-.",
  45. "--.","....","..",".---","-.-",".-..","--","-.","---",".--.",
  46. "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."
  47. };
  48. for(int i=0; i<27; i++)
  49. {
  50. if(c == alpha[i])
  51. {
  52. cout << morse[i] << " ";
  53. }
  54. }
  55. }
  56. void MorseCodeForNumber(char c) // function for converting all numbers to unique sequence of dot and dashes
  57. {
  58. const char number[] = {'0','1','2','3','4','5','6','7','8','9'};
  59. const char* morse[] = { "-----",".----","..---","...--","....-",
  60. ".....","-....","--...","---..","----."};
  61. for(int i=0; i<11; i++)
  62. {
  63. if(c == number[i])
  64. {
  65. cout << morse[i] << " ";
  66. }
  67. }
  68. }
  69. /* In Morse Code, the space between parts of the same letter is one unit;
  70. the space between letters is three units;
  71. the space between words is seven units */
  72. int main()
  73. {
  74. char message[100];
  75. cin.get(message,99);
  76. int length = strlen(message);
  77.  
  78. for(int i=0; i<length; i++)
  79. {
  80. if(isAlpha(message[i]))
  81. {
  82. ToUper(message[i]);
  83.  
  84. MorseCodeForAlpha(message[i]);
  85. }
  86. else if (isNumber(message[i]))
  87. {
  88. MorseCodeForNumber(message[i]);
  89. }
  90. else if(message[i] == ' ') // determines the word
  91. {
  92. cout << " "; continue;
  93. }
  94. else
  95. {
  96. cout << "Bad input!" << endl; break; // message may contain only Latin letters and Arabic numbers
  97. }
  98. }
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement