Advertisement
AlFas

New Morse code approach

Aug 17th, 2019
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.34 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() => Convert(GetMorseString);
  27.         public string ToBinary() => Convert(GetBinaryString);
  28.  
  29.         private string Convert(CharacterConverter converter)
  30.         {
  31.             var s = new StringBuilder();
  32.             foreach (var c in Text)
  33.                 s.Append(converter(c));
  34.             return s.ToString();
  35.         }
  36.  
  37.         private static string GetMorseString(char c) => $"{MorseContainer.GetCharacterMorseRepresentation(c)} ";
  38.         private static string GetBinaryString(char c) => $"{MorseContainer.GetCharacterBinaryRepresentation(c)}000";
  39.     }
  40.  
  41.     public delegate string CharacterConverter(char c);
  42.  
  43.     public static class MorseContainer
  44.     {
  45.         private const int ArrayLength = 'z' + 1;
  46.         private const int CaseOffset = 'A' - 'a';
  47.  
  48.         private static string[] morse = new string[ArrayLength];
  49.         private static string[] binary = new string[ArrayLength];
  50.  
  51.         static MorseContainer()
  52.         {
  53.             // Initialize letter morse dictionary - has no fucking pattern at all, hardcoding is the best
  54.             morse['A'] = ".-";
  55.             morse['B'] = "-...";
  56.             morse['C'] = "-.-.";
  57.             morse['D'] = "-..";
  58.             morse['E'] = ".";
  59.             morse['F'] = "..-.";
  60.             morse['G'] = "--.";
  61.             morse['H'] = "....";
  62.             morse['I'] = "..";
  63.             morse['J'] = ".---";
  64.             morse['K'] = "-.-";
  65.             morse['L'] = ".-..";
  66.             morse['M'] = "--";
  67.             morse['N'] = "-.";
  68.             morse['O'] = "---";
  69.             morse['P'] = ".--.";
  70.             morse['Q'] = "--.-";
  71.             morse['R'] = ".-.";
  72.             morse['S'] = "...";
  73.             morse['T'] = "-";
  74.             morse['U'] = "..-";
  75.             morse['V'] = "...-";
  76.             morse['W'] = ".--";
  77.             morse['X'] = "-..-";
  78.             morse['Y'] = "-.--";
  79.             morse['Z'] = "--..";
  80.  
  81.             for (int i = 'a'; i <= 'z'; i++)
  82.                 morse[i] = morse[i + CaseOffset];
  83.  
  84.             // Initialize number morse dictionary - has a really nice pattern
  85.             bool[] dots = new bool[5];
  86.             for (int i = 0; i < 10; i++)
  87.             {
  88.                 char[] chars = new char[5];
  89.                 for (int j = 0; j < 5; j++)
  90.                     chars[j] = dots[j] ? '.' : '-';
  91.                 morse[i + '0'] = new string(chars);
  92.                 dots[i % 5] = !dots[i % 5];
  93.             }
  94.  
  95.             // Initialize binary dictionary
  96.             for (int i = 0; i < ArrayLength; i++)
  97.                 if (morse[i] != null)
  98.                     binary[i] = ConvertMorseLetterToBinary(morse[i]);
  99.             binary[' '] = "0";
  100.         }
  101.  
  102.         public static string ConvertMorseLetterToBinary(string letter)
  103.         {
  104.             var s = new StringBuilder();
  105.             foreach (var c in letter)
  106.                 s.Append($"{GetMorseCharacterBinaryRepresentation(c)}0");
  107.             return s.Remove(s.Length - 1, 1).ToString();
  108.         }
  109.         public static string ConvertMorseToBinary(string morse)
  110.         {
  111.             // 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
  112.             var s = new StringBuilder();
  113.  
  114.             var words = morse.Split("  ");
  115.             foreach (var w in words)
  116.             {
  117.                 var letters = w.Split(' ');
  118.                 foreach (var l in letters)
  119.                     s.Append($"{ConvertMorseLetterToBinary(l)}000");
  120.                 s.Append($"0000");
  121.             }
  122.  
  123.             return s.Remove(s.Length - 7, 7).ToString();
  124.         }
  125.         public static string ConvertBinaryToMorse(string binary)
  126.         {
  127.             var s = new StringBuilder();
  128.  
  129.             int aces = 0;
  130.             int zeroes = 0;
  131.             foreach (var c in binary)
  132.             {
  133.                 switch (c)
  134.                 {
  135.                     case '1':
  136.                         if (zeroes > 0)
  137.                             RegisterZeroes();
  138.                         aces++;
  139.                         break;
  140.                     case '0':
  141.                         if (aces > 0)
  142.                             RegisterAces();
  143.                         zeroes++;
  144.                         break;
  145.                 }
  146.  
  147.                 void RegisterZeroes()
  148.                 {
  149.                     switch (zeroes)
  150.                     {
  151.                         case 3:
  152.                             s.Append(" "); // separates single letter
  153.                             break;
  154.                         case 7:
  155.                             s.Append("  "); // separates entire word
  156.                             break;
  157.                     }
  158.                     zeroes = 0;
  159.                 }
  160.                 void RegisterAces()
  161.                 {
  162.                     s.Append(GetMorseRepresentation(aces));
  163.                     aces = 0;
  164.                 }
  165.             }
  166.  
  167.             return s.ToString();
  168.         }
  169.  
  170.         public static string GetCharacterBinaryRepresentation(char character) => binary[character];
  171.         public static string GetCharacterMorseRepresentation(char character) => morse[character];
  172.  
  173.         private static string GetMorseCharacterBinaryRepresentation(char morseCharacter)
  174.         {
  175.             switch (morseCharacter)
  176.             {
  177.                 case '.':
  178.                     return "1";
  179.                 case '-':
  180.                     return "111";
  181.             }
  182.             return null;
  183.         }
  184.         private static char GetMorseRepresentation(int aces)
  185.         {
  186.             switch (aces)
  187.             {
  188.                 case 1:
  189.                     return '.';
  190.                 case 3:
  191.                     return '-';
  192.             }
  193.             return (char)0;
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement