Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace MorseToBinary
- {
- public static class Program
- {
- public static void Main(string[] args)
- {
- var text = Console.ReadLine();
- var encryptor = new MorseEncryptor(text);
- Console.WriteLine(encryptor.ToMorse());
- Console.WriteLine(encryptor.ToBinary());
- }
- }
- public class MorseEncryptor
- {
- public string Text;
- public MorseEncryptor(string text)
- {
- Text = text;
- }
- public string ToMorse() => Convert(GetMorseString);
- public string ToBinary() => Convert(GetBinaryString);
- private string Convert(CharacterConverter converter)
- {
- var s = new StringBuilder();
- foreach (var c in Text)
- s.Append(converter(c));
- return s.ToString();
- }
- private static string GetMorseString(char c) => $"{MorseContainer.GetCharacterMorseRepresentation(c)} ";
- private static string GetBinaryString(char c) => $"{MorseContainer.GetCharacterBinaryRepresentation(c)}000";
- }
- public delegate string CharacterConverter(char c);
- public static class MorseContainer
- {
- private const int ArrayLength = 'z' + 1;
- private const int CaseOffset = 'A' - 'a';
- private static string[] morse = new string[ArrayLength];
- private static string[] binary = new string[ArrayLength];
- static MorseContainer()
- {
- // Initialize letter morse dictionary - has no fucking pattern at all, hardcoding is the best
- morse['A'] = ".-";
- morse['B'] = "-...";
- morse['C'] = "-.-.";
- morse['D'] = "-..";
- morse['E'] = ".";
- morse['F'] = "..-.";
- morse['G'] = "--.";
- morse['H'] = "....";
- morse['I'] = "..";
- morse['J'] = ".---";
- morse['K'] = "-.-";
- morse['L'] = ".-..";
- morse['M'] = "--";
- morse['N'] = "-.";
- morse['O'] = "---";
- morse['P'] = ".--.";
- morse['Q'] = "--.-";
- morse['R'] = ".-.";
- morse['S'] = "...";
- morse['T'] = "-";
- morse['U'] = "..-";
- morse['V'] = "...-";
- morse['W'] = ".--";
- morse['X'] = "-..-";
- morse['Y'] = "-.--";
- morse['Z'] = "--..";
- for (int i = 'a'; i <= 'z'; i++)
- morse[i] = morse[i + CaseOffset];
- // Initialize number morse dictionary - has a really nice pattern
- bool[] dots = new bool[5];
- for (int i = 0; i < 10; i++)
- {
- char[] chars = new char[5];
- for (int j = 0; j < 5; j++)
- chars[j] = dots[j] ? '.' : '-';
- morse[i + '0'] = new string(chars);
- dots[i % 5] = !dots[i % 5];
- }
- // Initialize binary dictionary
- for (int i = 0; i < ArrayLength; i++)
- if (morse[i] != null)
- binary[i] = ConvertMorseLetterToBinary(morse[i]);
- binary[' '] = "0";
- }
- public static string ConvertMorseLetterToBinary(string letter)
- {
- var s = new StringBuilder();
- foreach (var c in letter)
- s.Append($"{GetMorseCharacterBinaryRepresentation(c)}0");
- return s.Remove(s.Length - 1, 1).ToString();
- }
- public static string ConvertMorseToBinary(string morse)
- {
- // 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
- var s = new StringBuilder();
- var words = morse.Split(" ");
- foreach (var w in words)
- {
- var letters = w.Split(' ');
- foreach (var l in letters)
- s.Append($"{ConvertMorseLetterToBinary(l)}000");
- s.Append($"0000");
- }
- return s.Remove(s.Length - 7, 7).ToString();
- }
- public static string ConvertBinaryToMorse(string binary)
- {
- var s = new StringBuilder();
- int aces = 0;
- int zeroes = 0;
- foreach (var c in binary)
- {
- switch (c)
- {
- case '1':
- if (zeroes > 0)
- RegisterZeroes();
- aces++;
- break;
- case '0':
- if (aces > 0)
- RegisterAces();
- zeroes++;
- break;
- }
- void RegisterZeroes()
- {
- switch (zeroes)
- {
- case 3:
- s.Append(" "); // separates single letter
- break;
- case 7:
- s.Append(" "); // separates entire word
- break;
- }
- zeroes = 0;
- }
- void RegisterAces()
- {
- s.Append(GetMorseRepresentation(aces));
- aces = 0;
- }
- }
- return s.ToString();
- }
- public static string GetCharacterBinaryRepresentation(char character) => binary[character];
- public static string GetCharacterMorseRepresentation(char character) => morse[character];
- private static string GetMorseCharacterBinaryRepresentation(char morseCharacter)
- {
- switch (morseCharacter)
- {
- case '.':
- return "1";
- case '-':
- return "111";
- }
- return null;
- }
- private static char GetMorseRepresentation(int aces)
- {
- switch (aces)
- {
- case 1:
- return '.';
- case 3:
- return '-';
- }
- return (char)0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement