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()
- {
- var s = new StringBuilder();
- foreach (var c in Text)
- {
- if (char.IsLetter(c))
- s.Append(AlphabetMorseContainer.GetMorseRepresentation(c));
- else if (char.IsDigit(c))
- s.Append(NumericalMorseContainer.GetMorseRepresentation(c));
- s.Append(" ");
- }
- return s.ToString();
- }
- public string ToBinary()
- {
- var s = new StringBuilder();
- foreach (var c in Text)
- {
- if (char.IsLetter(c))
- s.Append(AlphabetMorseContainer.GetBinaryRepresentation(c));
- else if (char.IsDigit(c))
- s.Append(NumericalMorseContainer.GetBinaryRepresentation(c));
- else if (c == ' ')
- s.Append("0");
- s.Append("000");
- }
- return s.ToString();
- }
- }
- public abstract class MorseContainer
- {
- 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.";
- protected static InvalidOperationException ExceptionInstance = new InvalidOperationException(InitializationExceptionMessage);
- public static string ConvertMorseLetterToBinary(string letter)
- {
- var s = new StringBuilder();
- foreach (var c in letter)
- s.Append($"{GetBinaryRepresentation(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();
- }
- private static string GetBinaryRepresentation(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;
- }
- }
- public class AlphabetMorseContainer : MorseContainer
- {
- private static CharacterOffsetArray<string> morse = new CharacterOffsetArray<string>(26, 'A')
- {
- ['A'] = ".-",
- ['B'] = "-...",
- ['C'] = "-.-.",
- ['D'] = "-..",
- ['E'] = ".",
- ['F'] = "..-.",
- ['G'] = "--.",
- ['H'] = "....",
- ['I'] = "..",
- ['J'] = ".---",
- ['K'] = "-.-",
- ['L'] = ".-..",
- ['M'] = "--",
- ['N'] = "-.",
- ['O'] = "---",
- ['P'] = ".--.",
- ['Q'] = "--.-",
- ['R'] = ".-.",
- ['S'] = "...",
- ['T'] = "-",
- ['U'] = "..-",
- ['V'] = "...-",
- ['W'] = ".--",
- ['X'] = "-..-",
- ['Y'] = "-.--",
- ['Z'] = "--..",
- };
- private static CharacterOffsetArray<string> binary = new CharacterOffsetArray<string>(26, 'A');
- static AlphabetMorseContainer()
- {
- // Initialize binary dictionary
- for (char i = 'A'; i <= 'Z'; i++)
- binary[i] = ConvertMorseLetterToBinary(morse[i]);
- }
- /// <summary>Don't use this, or you'll get a nice exception thrown right on your face.</summary>
- public AlphabetMorseContainer() => throw ExceptionInstance;
- public static string GetBinaryRepresentation(char c) => binary[c];
- public static string GetMorseRepresentation(char c) => morse[c];
- }
- public class NumericalMorseContainer : MorseContainer
- {
- private static CharacterOffsetArray<string> morse = new CharacterOffsetArray<string>(10, '0');
- private static CharacterOffsetArray<string> binary = new CharacterOffsetArray<string>(10, '0');
- static NumericalMorseContainer()
- {
- // Initialize 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 (char i = '0'; i <= '9'; i++)
- binary[i] = ConvertMorseLetterToBinary(morse[i]);
- }
- /// <summary>Don't use this, or you'll get a nice exception thrown right on your face.</summary>
- public NumericalMorseContainer() => throw ExceptionInstance;
- public static string GetBinaryRepresentation(char c) => binary[c];
- public static string GetMorseRepresentation(char c) => morse[c];
- }
- public class CharacterOffsetArray<T>
- {
- private T[] array;
- public char Offset;
- public CharacterOffsetArray(int length, char offset)
- {
- array = new T[length];
- Offset = offset;
- }
- public T this[char index]
- {
- get => array[index - Offset];
- set => array[index - Offset] = value;
- }
- public T this[int index]
- {
- get => array[index - Offset];
- set => array[index - Offset] = value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement