Source code for submission s509

fs.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char Morse[300][10] =
  6. {
  7. ".-",
  8. "-...",
  9. "-.-.",
  10. "-..",
  11. ".",
  12. "..-.",
  13. "--.",
  14. "....",
  15. "..",
  16. ".---",
  17. "-.-",
  18. ".-..",
  19. "--",
  20. "-.",
  21. "---",
  22. ".--.",
  23. "--.-",
  24. ".-.",
  25. "...",
  26. "-",
  27. "..-",
  28. "...-",
  29. ".--",
  30. "-..-",
  31. "-.--",
  32. "--..",
  33. "..--",
  34. ".-.-",
  35. "---.",
  36. "----"
  37. };
  38.  
  39. int CharToIndex[300];
  40. char IndexToChar[40] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_,.?";
  41.  
  42. char String[2000];
  43. int Length[2000];
  44. char Code[10000];
  45. char Buffer[100];
  46.  
  47. void CharToMorse(char c, char* Buffer, int& Length)
  48. {
  49. int l;
  50. for(l = 0; Morse[CharToIndex[(int) c]][l]; l++)
  51. {
  52. Buffer[l] = Morse[CharToIndex[(int) c]][l];
  53. }
  54.  
  55. Length = l;
  56. Buffer[l] = '\0';
  57. }
  58.  
  59. char MorseToChar(char* Code, int Length)
  60. {
  61. char buf[10] = "";
  62. for(int i = 0; i < Length; i++)
  63. {
  64. buf[i] = Code[i];
  65. }
  66.  
  67. buf[Length] = '\0';
  68.  
  69. for(int i = 0; 1; i++)
  70. {
  71. if(!(strcmp(Morse[i], buf)))
  72. {
  73. return IndexToChar[i];
  74. }
  75. }
  76. }
  77.  
  78. int main()
  79. {
  80. for(int i = 0; i < 30; i++)
  81. {
  82. CharToIndex[(int) IndexToChar[i]] = i;
  83. }
  84.  
  85. /*for(int i = 0; i < 30; i++)
  86. {
  87. printf("%c is %s\n", IndexToChar[i], Morse[i]);
  88. }*/
  89.  
  90. while(fgets(String, 2000, stdin))
  91. {
  92. /*if(String[0] == '@')
  93. {
  94. return 0;
  95. }*/
  96.  
  97. int i, l = 0, total = 0;
  98. for(i = 0; String[i] != '\n'; i++)
  99. {
  100. CharToMorse(String[i], Buffer, Length[i]);
  101. for(int j = 0; Buffer[j]; j++)
  102. {
  103. Code[l++] = Buffer[j];
  104. }
  105. }
  106.  
  107. Code[l] = '\0';
  108. //puts(Code);
  109.  
  110. total = i;
  111.  
  112. l = 0;
  113. for(i = total - 1; i >= 0; i--)
  114. {
  115. putchar(MorseToChar(Code + l, Length[i]));
  116. l += Length[i];
  117. }
  118.  
  119. putchar('\n');
  120. }
  121.  
  122. return 0;
  123. }
  124.  
  125.