diff --git a/MorseCodeToEnglish.cs b/MorseCodeToEnglish.cs new file mode 100644 index 0000000..5179bd6 --- /dev/null +++ b/MorseCodeToEnglish.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; + +namespace MorseCodeToEnglish +{ + public class Program + { + static void Main(string[] args) + { + //assumption is that the first argument is the file + string filename; + try + { + filename = args[0]; + } + catch + { + Console.WriteLine("no file specified"); + Console.ReadKey(); + return; + } + + filename = getFullPath(filename); + if (filename != "file not found") + { + translateMorseToEnglish(filename); + } + else + { + Console.WriteLine("file not found"); + Console.ReadKey(); + return; + } + return; + } + + private static void translateMorseToEnglish(string filename) + { + var MorseHashTable = new Dictionary(); + PopulateMorseHashTable(ref MorseHashTable); + IEnumerable lines; + string[] letters; + var outputline = new StringBuilder(); + + lines = System.IO.File.ReadLines(filename); + foreach (string line in lines) + { + outputline.Clear(); + letters = line.Replace("||", ",").Split(','); + foreach (string letter in letters) + { + outputline.Append(MorseHashTable[letter]); + } + Console.WriteLine(outputline); + } + } + + private static string getFullPath(string filename) + { // user can enter the full file path or the filename if it is in the current directory + string fullpath = "file not found"; + if (File.Exists(filename)) + { + fullpath = filename; + } + else if (System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory() + filename)) + { + fullpath = System.IO.Directory.GetCurrentDirectory() + filename; + } + return fullpath; + } + + private static void PopulateMorseHashTable(ref Dictionary hashtable) + { + hashtable.Add("", " "); //blank returns space + hashtable.Add(".-", "a"); + hashtable.Add("-...", "b"); + hashtable.Add("-.-.", "c"); + hashtable.Add("-..", "d"); + hashtable.Add(".", "e"); + hashtable.Add("..-.", "f"); + hashtable.Add("--.", "g"); + hashtable.Add("....", "h"); + hashtable.Add("..", "i"); + hashtable.Add(".---", "j"); + hashtable.Add("-.-", "k"); + hashtable.Add(".-..", "l"); + hashtable.Add("--", "m"); + hashtable.Add("-.", "n"); + hashtable.Add("---", "o"); + hashtable.Add(".--.", "p"); + hashtable.Add("--.-", "q"); + hashtable.Add(".-.", "r"); + hashtable.Add("...", "s"); + hashtable.Add("-", "t"); + hashtable.Add("..-", "u"); + hashtable.Add("...-", "v"); + hashtable.Add(".--", "w"); + hashtable.Add("-..-", "x"); + hashtable.Add("-.--", "y"); + hashtable.Add("--..", "z"); + hashtable.Add("-----", "0"); + hashtable.Add(".----", "1"); + hashtable.Add("..---", "2"); + hashtable.Add("...--", "3"); + hashtable.Add("....-", "4"); + hashtable.Add(".....", "5"); + hashtable.Add("-....", "6"); + hashtable.Add("--...", "7"); + hashtable.Add("---..", "8"); + hashtable.Add("----.", "9"); + } + } +} diff --git a/MorseCodeToEnglish.exe b/MorseCodeToEnglish.exe new file mode 100644 index 0000000..cd6fb0a Binary files /dev/null and b/MorseCodeToEnglish.exe differ diff --git a/MorseCodeToEnglishInteractive.cs b/MorseCodeToEnglishInteractive.cs new file mode 100644 index 0000000..4da5c25 --- /dev/null +++ b/MorseCodeToEnglishInteractive.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; + +namespace MorseCodeToEnglishInteractive +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Please enter the filename or full path (exit to quit): "); + string filename = Console.ReadLine(); + + while (filename.ToLower() != "exit") + { + filename = getFullPath(filename); + if (filename != "file not found") + { + translateAndOutput(filename); + } + else + { + Console.WriteLine(""); + Console.WriteLine("file not found"); + } + Console.WriteLine(""); + Console.WriteLine("Please enter the filename or full path (exit to quit): "); + filename = Console.ReadLine(); + } + } + + private static void translateAndOutput(string filename) + { + var MorseHashTable = new Dictionary(); + PopulateMorseHashTable(ref MorseHashTable); + IEnumerable lines; + string[] letters; + var outputline = new StringBuilder(); + + Console.WriteLine("Output:"); + + lines = System.IO.File.ReadLines(filename); + foreach (string line in lines) + { + outputline.Clear(); + letters = line.Replace("||", ",").Split(','); + foreach (string letter in letters) + { + outputline.Append(MorseHashTable[letter]); + } + Console.WriteLine(outputline); + } + } + + private static string getFullPath(string filename) + { // user can enter the full file path or the filename if it is in the current directory + string fullpath = "file not found"; + if (File.Exists(filename)) + { + fullpath = filename; + } + else if (System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory() + filename)) + { + fullpath = System.IO.Directory.GetCurrentDirectory() + filename; + } + return fullpath; + } + + private static void PopulateMorseHashTable(ref Dictionary hashtable) + { + hashtable.Add("", " "); //blank returns space + hashtable.Add(".-", "a"); + hashtable.Add("-...", "b"); + hashtable.Add("-.-.", "c"); + hashtable.Add("-..", "d"); + hashtable.Add(".", "e"); + hashtable.Add("..-.", "f"); + hashtable.Add("--.", "g"); + hashtable.Add("....", "h"); + hashtable.Add("..", "i"); + hashtable.Add(".---", "j"); + hashtable.Add("-.-", "k"); + hashtable.Add(".-..", "l"); + hashtable.Add("--", "m"); + hashtable.Add("-.", "n"); + hashtable.Add("---", "o"); + hashtable.Add(".--.", "p"); + hashtable.Add("--.-", "q"); + hashtable.Add(".-.", "r"); + hashtable.Add("...", "s"); + hashtable.Add("-", "t"); + hashtable.Add("..-", "u"); + hashtable.Add("...-", "v"); + hashtable.Add(".--", "w"); + hashtable.Add("-..-", "x"); + hashtable.Add("-.--", "y"); + hashtable.Add("--..", "z"); + hashtable.Add("-----", "0"); + hashtable.Add(".----", "1"); + hashtable.Add("..---", "2"); + hashtable.Add("...--", "3"); + hashtable.Add("....-", "4"); + hashtable.Add(".....", "5"); + hashtable.Add("-....", "6"); + hashtable.Add("--...", "7"); + hashtable.Add("---..", "8"); + hashtable.Add("----.", "9"); + } + } +} diff --git a/MorseCodeToEnglishInteractive.exe b/MorseCodeToEnglishInteractive.exe new file mode 100644 index 0000000..9c7b358 Binary files /dev/null and b/MorseCodeToEnglishInteractive.exe differ