Source code for submission s558

Fs.java

  1.  
  2. import java.util.HashMap;
  3. import java.util.Scanner;
  4.  
  5.  
  6. /**
  7.  *
  8.  * @author cteam023
  9.  */
  10. public class Fs {
  11.  
  12.  
  13.  
  14. private static void vyres(String radek){
  15. Kod morse = new Kod(radek);
  16. // morse.debugVypis();
  17. morse.otocCislo();
  18. System.out.println(morse);
  19. }
  20.  
  21. public static void main(String[] args) {
  22. Scanner sc = new Scanner(System.in);
  23. String radek;
  24. while(sc.hasNextLine()){
  25. radek = sc.nextLine();
  26. vyres(radek);
  27. }
  28. }
  29.  
  30. private static class Kod{
  31. private String morseovka = "";
  32. private String cislice = "";
  33.  
  34. private static final String[] tableToMorse=
  35. {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
  36. ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
  37. "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
  38. private static final HashMap<String, Character> morseToTable;
  39.  
  40. static {
  41. morseToTable = new HashMap<String, Character>();
  42. char pocitadlo = 'A';
  43. for (String s: tableToMorse){
  44. morseToTable.put(s, pocitadlo++);
  45. }
  46.  
  47. morseToTable.put("..--", '_');
  48. morseToTable.put("---.", '.');
  49. morseToTable.put(".-.-", ',');
  50. morseToTable.put("----", '?');
  51. }
  52.  
  53. public Kod(String retezec){
  54. for (char c: retezec.toCharArray()){
  55. toMorse(c);
  56. }
  57.  
  58. }
  59.  
  60. private void toMorse(char c){
  61. if (c >= 'A' && c <= 'Z'){
  62. String pridat = tableToMorse[c - 'A'];
  63. morseovka += pridat;
  64. cislice += "" + pridat.length();
  65. } else {
  66. if (c == '_'){
  67. morseovka += "..--";
  68. } else if (c == '.'){
  69. morseovka += "---.";
  70. } else if (c == ','){
  71. morseovka += ".-.-";
  72. } else if (c == '?'){
  73. morseovka += "----";
  74. }
  75. cislice += "4";
  76. }
  77.  
  78. }
  79.  
  80. public void debugVypis(){
  81. System.out.println("m:" + morseovka +", " + cislice);
  82. }
  83.  
  84. public void otocCislo(){
  85. String reverse = "";
  86. for (int i = 0; i < cislice.length(); i++){
  87. reverse = cislice.charAt(i) + reverse;
  88. }
  89. cislice = reverse;
  90. }
  91.  
  92. @Override
  93. public String toString() {
  94. String sekvence = "";
  95. String sub;
  96. int pocitadloCisel = 0;
  97. int ukazatelSekvenci = 0;
  98. while (pocitadloCisel < cislice.length()){
  99. sub = morseovka.substring(ukazatelSekvenci, ukazatelSekvenci + cislice.charAt(pocitadloCisel) - '0');
  100. ukazatelSekvenci += cislice.charAt(pocitadloCisel++) - '0';
  101. sekvence += morseToTable.get(sub);
  102. }
  103.  
  104.  
  105. return sekvence;
  106. }
  107.  
  108.  
  109.  
  110. }
  111.  
  112. }
  113.