// Skeleton Program for the AQA AS Summer 2018 examination // this code should be used in conjunction with the Preliminary Material // written by the AQA AS Programmer Team // developed using Visual Studio Community Edition // Version Number : 0.2 using System.IO; using System; using Microsoft.VisualBasic; // Install-Package Microsoft.VisualBasic using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic internal static partial class Module1 { private const char SPACE = ' '; private const char EOL = '#'; private const string EMPTYSTRING = ""; public static void ReportError(string s) { Console.WriteLine("*".PadRight(6) + s + "*".PadLeft(6)); } public static string StripLeadingSpaces(string Transmission) { int TransmissionLength = Transmission.Length; char FirstSignal; if (TransmissionLength > 0) { FirstSignal = Transmission[0]; while (FirstSignal == SPACE & TransmissionLength > 0) { TransmissionLength -= 1; Transmission = Transmission.Substring(1); if (TransmissionLength > 0) { FirstSignal = Transmission[0]; } } } if (TransmissionLength == 0) { ReportError("No signal received"); } return Transmission; } public static string StripTrailingSpaces(string Transmission) { int LastChar = Transmission.Length - 1; while (Transmission[LastChar] == SPACE) { Transmission = Transmission.Remove(LastChar); LastChar -= 1; } return Transmission; } public static string GetTransmission() { string Filename; string Transmission; Console.Write("Enter file name: "); try { Filename = Console.ReadLine(); var Reader = new StreamReader(Filename); Transmission = Reader.ReadLine(); Reader.Close(); Transmission = StripLeadingSpaces(Transmission); if (Transmission.Length > 0) { Transmission = StripTrailingSpaces(Transmission); Transmission = Transmission + Conversions.ToString(EOL); } } catch { ReportError("No transmission found"); Transmission = EMPTYSTRING; } return Transmission; } public static string GetNextSymbol(ref int i, string Transmission) { string Signal; int SymbolLength; char Symbol; if (Transmission[i] == EOL) { Console.WriteLine(); Console.WriteLine("End of transmission"); Symbol = Conversions.ToChar(EMPTYSTRING); } else { SymbolLength = 0; Signal = Conversions.ToString(Transmission[i]); while (Signal != Conversions.ToString(SPACE) & Signal != Conversions.ToString(EOL)) { i += 1; Signal = Conversions.ToString(Transmission[i]); SymbolLength += 1; } if (SymbolLength == 1) { Symbol = '.'; } else if (SymbolLength == 3) { Symbol = '-'; } else if (SymbolLength == 0) { Symbol = SPACE; } else { ReportError("Non-standard symbol received"); Symbol = Conversions.ToChar(EMPTYSTRING); } } return Conversions.ToString(Symbol); } public static string GetNextLetter(ref int i, string Transmission) { string SymbolString = EMPTYSTRING; bool LetterEnd = false; string Symbol; while (!LetterEnd) { Symbol = GetNextSymbol(ref i, Transmission); if (Symbol == Conversions.ToString(SPACE)) { LetterEnd = true; i += 4; } else if (Transmission[i] == EOL) { LetterEnd = true; } else if (Transmission[i + 1] == SPACE & Transmission[i + 2] == SPACE) { LetterEnd = true; i += 3; } else { i += 1; } SymbolString = SymbolString + Symbol; } return SymbolString; } public static string Decode(string CodedLetter, int[] Dash, string[] Letter, int[] Dot) { int CodedLetterLength = CodedLetter.Length; int Pointer = 0; char Symbol; for (int i = 0, loopTo = CodedLetterLength - 1; i <= loopTo; i++) { Symbol = CodedLetter[i]; if (Symbol == SPACE) { return Conversions.ToString(SPACE); } else if (Conversions.ToString(Symbol) == "-") { Pointer = Dash[Pointer]; } else { Pointer = Dot[Pointer]; } } return Letter[Pointer]; } public static void ReceiveMorseCode(int[] Dash, string[] Letter, int[] Dot) { string PlainText = EMPTYSTRING; string MorseCodeString = EMPTYSTRING; string Transmission = GetTransmission(); int LastChar = Transmission.Length - 1; int i = 0; string CodedLetter; char PlainTextLetter; while (i < LastChar) { CodedLetter = GetNextLetter(ref i, Transmission); MorseCodeString = MorseCodeString + Conversions.ToString(SPACE) + CodedLetter; PlainTextLetter = Conversions.ToChar(Decode(CodedLetter, Dash, Letter, Dot)); PlainText = PlainText + Conversions.ToString(PlainTextLetter); } Console.WriteLine(MorseCodeString); Console.WriteLine(PlainText); } public static void SendMorseCode(string[] MorseCode) { string PlainText; int PlainTextLength; string MorseCodeString; char PlainTextLetter; string CodedLetter; int Index; Console.Write("Enter your message (uppercase letters and spaces only): "); PlainText = Console.ReadLine(); PlainTextLength = PlainText.Length; MorseCodeString = EMPTYSTRING; for (int i = 0, loopTo = PlainTextLength - 1; i <= loopTo; i++) { PlainTextLetter = PlainText[i]; if (PlainTextLetter == SPACE) { Index = 0; } else { Index = Strings.Asc(PlainTextLetter) - Strings.Asc("A") + 1; } CodedLetter = MorseCode[Index]; MorseCodeString = MorseCodeString + CodedLetter + Conversions.ToString(SPACE); } Console.WriteLine(MorseCodeString); } public static void DisplayMenu() { Console.WriteLine(); Console.WriteLine("Main Menu"); Console.WriteLine("========="); Console.WriteLine("R - Receive Morse code"); Console.WriteLine("S - Send Morse code"); Console.WriteLine("X - Exit program"); Console.WriteLine(); } public static string GetMenuOption() { string MenuOption = EMPTYSTRING; while (MenuOption.Length != 1) { Console.Write("Enter your choice: "); MenuOption = Console.ReadLine(); } return MenuOption; } public static void SendReceiveMessages() { var Dash = new[] { 20, 23, 0, 0, 24, 1, 0, 17, 0, 21, 0, 25, 0, 15, 11, 0, 0, 0, 0, 22, 13, 0, 0, 10, 0, 0, 0 }; var Letter = new[] { " ", "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" }; var Dot = new[] { 5, 18, 0, 0, 2, 9, 0, 26, 0, 19, 0, 3, 0, 7, 4, 0, 0, 0, 12, 8, 14, 6, 0, 16, 0, 0, 0 }; var MorseCode = new[] { " ", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." }; string MenuOption; bool ProgramEnd = false; while (!ProgramEnd) { DisplayMenu(); MenuOption = GetMenuOption(); if (MenuOption == "R") { ReceiveMorseCode(Dash, Letter, Dot); } else if (MenuOption == "S") { SendMorseCode(MorseCode); } else if (MenuOption == "X") { ProgramEnd = true; } } } public static void Main() { SendReceiveMessages(); Console.ReadLine(); } }