Source code for submission s719

fs.cpp

  1. #include <iostream>
  2. #include <vector>
  3. #include <stdio.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct Letter {
  9. char letter;
  10. string morse;
  11. int size;
  12.  
  13. Letter(){}
  14. Letter(char c, string m) {
  15. letter = c;
  16. morse = m;
  17. size = m.size();
  18. }
  19. };
  20.  
  21. enum {
  22. underscore = 26,
  23. comma = 27,
  24. period = 28,
  25. question = 29
  26. };
  27.  
  28. vector<Letter> dict;
  29.  
  30. void fillMorse();
  31. string printMorse();
  32. int getPosInDict(char c);
  33. char getLetterByMorse(string str);
  34.  
  35. int main() {
  36.  
  37. fillMorse();
  38. //printMorse();
  39.  
  40. string line;
  41. while(cin >> line) {
  42. //cout << line << endl;
  43. string lineInMorse = "";
  44. int morseIntSize = line.size();
  45. int morseInt[morseIntSize];
  46. for(int i = 0; i < morseIntSize; i++) {
  47. int posInDict = getPosInDict(line[i]);
  48. lineInMorse += dict[posInDict].morse;
  49. morseInt[i] = dict[posInDict].size;
  50. }
  51.  
  52. /*
  53. cout << lineInMorse << endl;
  54. for(int i = 0; i < morseIntSize; i++) {
  55. cout << morseInt[i];
  56. }
  57. cout << endl;
  58. */
  59.  
  60. string decrypted = "";
  61. int morsePos = 0;
  62. for(int i = morseIntSize-1; i >= 0; i--) {
  63. string letterInMorse = lineInMorse.substr(morsePos, morseInt[i]);
  64. morsePos += morseInt[i];
  65. decrypted += getLetterByMorse(letterInMorse);
  66. }
  67. cout << decrypted << endl;
  68. }
  69.  
  70. return 0;
  71. }
  72.  
  73. void fillMorse() {
  74. dict.push_back(Letter('A', ".-"));
  75. dict.push_back(Letter('B', "-..."));
  76. dict.push_back(Letter('C', "-.-."));
  77. dict.push_back(Letter('D', "-.."));
  78. dict.push_back(Letter('E', "."));
  79. dict.push_back(Letter('F', "..-."));
  80. dict.push_back(Letter('G', "--."));
  81. dict.push_back(Letter('H', "...."));
  82. dict.push_back(Letter('I', ".."));
  83. dict.push_back(Letter('J', ".---"));
  84. dict.push_back(Letter('K', "-.-"));
  85. dict.push_back(Letter('L', ".-.."));
  86. dict.push_back(Letter('M', "--"));
  87. dict.push_back(Letter('N', "-."));
  88. dict.push_back(Letter('O', "---"));
  89. dict.push_back(Letter('P', ".--."));
  90. dict.push_back(Letter('Q', "--.-"));
  91. dict.push_back(Letter('R', ".-."));
  92. dict.push_back(Letter('S', "..."));
  93. dict.push_back(Letter('T', "-"));
  94. dict.push_back(Letter('U', "..-"));
  95. dict.push_back(Letter('V', "...-"));
  96. dict.push_back(Letter('W', ".--"));
  97. dict.push_back(Letter('X', "-..-"));
  98. dict.push_back(Letter('Y', "-.--"));
  99. dict.push_back(Letter('Z', "--.."));
  100. dict.push_back(Letter('_', "..--"));
  101. dict.push_back(Letter(',', ".-.-"));
  102. dict.push_back(Letter('.', "---."));
  103. dict.push_back(Letter('?', "----"));
  104. }
  105.  
  106. string printMorse() {
  107. for(unsigned int i = 0; i < dict.size(); i++) {
  108. cout << dict[i].letter << " = " << dict[i].morse << endl;
  109. }
  110. return "";
  111. }
  112.  
  113. int getPosInDict(char c) {
  114. if(c >= 'A' && c <= 'Z') {
  115. return c - 'A';
  116. } else {
  117. switch(c) {
  118. case '_': return underscore;
  119. case ',': return comma;
  120. case '.': return period;
  121. case '?': return question;
  122. default: return -1;
  123. }
  124. }
  125. }
  126.  
  127. char getLetterByMorse(string str) {
  128. for(unsigned int i = 0; i < dict.size(); i++) {
  129. if(dict[i].morse == str) {
  130. return dict[i].letter;
  131. }
  132. }
  133. return '%';
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.