Advertisement
AlFas

Interesting Morse code approach

Aug 14th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.26 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace MorseToBinary
  5. {
  6.     public static class Program
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             var text = Console.ReadLine();
  11.             var encryptor = new MorseEncryptor(text);
  12.             Console.WriteLine(encryptor.ToMorse());
  13.             Console.WriteLine(encryptor.ToBinary());
  14.         }
  15.     }
  16.  
  17.     public class MorseEncryptor
  18.     {
  19.         public string Text;
  20.  
  21.         public MorseEncryptor(string text)
  22.         {
  23.             Text = text;
  24.         }
  25.  
  26.         public string ToMorse()
  27.         {
  28.             var s = new StringBuilder();
  29.             foreach (var c in Text)
  30.             {
  31.                 if (char.IsLetter(c))
  32.                     s.Append(AlphabetMorseContainer.GetMorseRepresentation(c));
  33.                 else if (char.IsDigit(c))
  34.                     s.Append(NumericalMorseContainer.GetMorseRepresentation(c));
  35.                 s.Append(" ");
  36.             }
  37.             return s.ToString();
  38.         }
  39.         public string ToBinary()
  40.         {
  41.             var s = new StringBuilder();
  42.             foreach (var c in Text)
  43.             {
  44.                 if (char.IsLetter(c))
  45.                     s.Append(AlphabetMorseContainer.GetBinaryRepresentation(c));
  46.                 else if (char.IsDigit(c))
  47.                     s.Append(NumericalMorseContainer.GetBinaryRepresentation(c));
  48.                 else if (c == ' ')
  49.                     s.Append("0");
  50.                 s.Append("000");
  51.             }
  52.             return s.ToString();
  53.         }
  54.     }
  55.  
  56.     public abstract class MorseContainer
  57.     {
  58.         protected static string InitializationExceptionMessage = "Initialization of a new instance of this class is prohibited, despite it being non-static. It is purposefully non-static to allow inheritance of static methods, since this is something that is not allowed by the language itself.";
  59.         protected static InvalidOperationException ExceptionInstance = new InvalidOperationException(InitializationExceptionMessage);
  60.  
  61.         public static string ConvertMorseLetterToBinary(string letter)
  62.         {
  63.             var s = new StringBuilder();
  64.             foreach (var c in letter)
  65.                 s.Append($"{GetBinaryRepresentation(c)}0");
  66.             return s.Remove(s.Length - 1, 1).ToString();
  67.         }
  68.         public static string ConvertMorseToBinary(string morse)
  69.         {
  70.             // The following code assumes that there is at least one letter per word in the given code; if that's not the case, it will throw an exception and eventually crash the program
  71.             var s = new StringBuilder();
  72.  
  73.             var words = morse.Split("  ");
  74.             foreach (var w in words)
  75.             {
  76.                 var letters = w.Split(' ');
  77.                 foreach (var l in letters)
  78.                     s.Append($"{ConvertMorseLetterToBinary(l)}000");
  79.                 s.Append($"0000");
  80.             }
  81.  
  82.             return s.Remove(s.Length - 7, 7).ToString();
  83.         }
  84.         public static string ConvertBinaryToMorse(string binary)
  85.         {
  86.             var s = new StringBuilder();
  87.  
  88.             int aces = 0;
  89.             int zeroes = 0;
  90.             foreach (var c in binary)
  91.             {
  92.                 switch (c)
  93.                 {
  94.                     case '1':
  95.                         if (zeroes > 0)
  96.                             RegisterZeroes();
  97.                         aces++;
  98.                         break;
  99.                     case '0':
  100.                         if (aces > 0)
  101.                             RegisterAces();
  102.                         zeroes++;
  103.                         break;
  104.                 }
  105.  
  106.                 void RegisterZeroes()
  107.                 {
  108.                     switch (zeroes)
  109.                     {
  110.                         case 3:
  111.                             s.Append(" "); // separates single letter
  112.                             break;
  113.                         case 7:
  114.                             s.Append("  "); // separates entire word
  115.                             break;
  116.                     }
  117.                     zeroes = 0;
  118.                 }
  119.                 void RegisterAces()
  120.                 {
  121.                     s.Append(GetMorseRepresentation(aces));
  122.                     aces = 0;
  123.                 }
  124.             }
  125.  
  126.             return s.ToString();
  127.         }
  128.  
  129.         private static string GetBinaryRepresentation(char morseCharacter)
  130.         {
  131.             switch (morseCharacter)
  132.             {
  133.                 case '.':
  134.                     return "1";
  135.                 case '-':
  136.                     return "111";
  137.             }
  138.             return null;
  139.         }
  140.         private static char GetMorseRepresentation(int aces)
  141.         {
  142.             switch (aces)
  143.             {
  144.                 case 1:
  145.                     return '.';
  146.                 case 3:
  147.                     return '-';
  148.             }
  149.             return (char)0;
  150.         }
  151.     }
  152.  
  153.     public class AlphabetMorseContainer : MorseContainer
  154.     {
  155.         private static CharacterOffsetArray<string> morse = new CharacterOffsetArray<string>(26, 'A')
  156.         {
  157.             ['A'] = ".-",
  158.             ['B'] = "-...",
  159.             ['C'] = "-.-.",
  160.             ['D'] = "-..",
  161.             ['E'] = ".",
  162.             ['F'] = "..-.",
  163.             ['G'] = "--.",
  164.             ['H'] = "....",
  165.             ['I'] = "..",
  166.             ['J'] = ".---",
  167.             ['K'] = "-.-",
  168.             ['L'] = ".-..",
  169.             ['M'] = "--",
  170.             ['N'] = "-.",
  171.             ['O'] = "---",
  172.             ['P'] = ".--.",
  173.             ['Q'] = "--.-",
  174.             ['R'] = ".-.",
  175.             ['S'] = "...",
  176.             ['T'] = "-",
  177.             ['U'] = "..-",
  178.             ['V'] = "...-",
  179.             ['W'] = ".--",
  180.             ['X'] = "-..-",
  181.             ['Y'] = "-.--",
  182.             ['Z'] = "--..",
  183.         };
  184.         private static CharacterOffsetArray<string> binary = new CharacterOffsetArray<string>(26, 'A');
  185.  
  186.         static AlphabetMorseContainer()
  187.         {
  188.             // Initialize binary dictionary
  189.             for (char i = 'A'; i <= 'Z'; i++)
  190.                 binary[i] = ConvertMorseLetterToBinary(morse[i]);
  191.         }
  192.  
  193.         /// <summary>Don't use this, or you'll get a nice exception thrown right on your face.</summary>
  194.         public AlphabetMorseContainer() => throw ExceptionInstance;
  195.  
  196.         public static string GetBinaryRepresentation(char c) => binary[c];
  197.         public static string GetMorseRepresentation(char c) => morse[c];
  198.     }
  199.     public class NumericalMorseContainer : MorseContainer
  200.     {
  201.         private static CharacterOffsetArray<string> morse = new CharacterOffsetArray<string>(10, '0');
  202.         private static CharacterOffsetArray<string> binary = new CharacterOffsetArray<string>(10, '0');
  203.  
  204.         static NumericalMorseContainer()
  205.         {
  206.             // Initialize morse dictionary - has a really nice pattern
  207.             bool[] dots = new bool[5];
  208.             for (int i = 0; i < 10; i++)
  209.             {
  210.                 char[] chars = new char[5];
  211.                 for (int j = 0; j < 5; j++)
  212.                     chars[j] = dots[j] ? '.' : '-';
  213.                 morse[i + '0'] = new string(chars);
  214.                 dots[i % 5] = !dots[i % 5];
  215.             }
  216.             // Initialize binary dictionary
  217.             for (char i = '0'; i <= '9'; i++)
  218.                 binary[i] = ConvertMorseLetterToBinary(morse[i]);
  219.         }
  220.  
  221.         /// <summary>Don't use this, or you'll get a nice exception thrown right on your face.</summary>
  222.         public NumericalMorseContainer() => throw ExceptionInstance;
  223.  
  224.         public static string GetBinaryRepresentation(char c) => binary[c];
  225.         public static string GetMorseRepresentation(char c) => morse[c];
  226.     }
  227.  
  228.     public class CharacterOffsetArray<T>
  229.     {
  230.         private T[] array;
  231.  
  232.         public char Offset;
  233.  
  234.         public CharacterOffsetArray(int length, char offset)
  235.         {
  236.             array = new T[length];
  237.             Offset = offset;
  238.         }
  239.  
  240.         public T this[char index]
  241.         {
  242.             get => array[index - Offset];
  243.             set => array[index - Offset] = value;
  244.         }
  245.         public T this[int index]
  246.         {
  247.             get => array[index - Offset];
  248.             set => array[index - Offset] = value;
  249.         }
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement