Jakub Brodský Ondřej Radosta The morse coder
Zadání Zkuste navrhnout algoritmus pro čtení textu v trojkové soustavě a jeho zápis do binární soustavy. Pokud byste dospěli k závěru, že je převod příliš komplikovaný, zkuste využít soustavu o základu 4 pro skupiny znaků (".","-","/","//"); koncový řetězec "///" lze vypustit a nahradit koncem souboru.
Pro chytré hlavy
Postup řešení Převod textu do Morseovky: public String ConvertTextToMorse(String text) { text = text.ToLower(); String result = ""; int index = -1; for (int i = 0; i <= text.Length - 1; i++) index = Array.IndexOf(Letters, text[i]); if (index != -1) result += MorseCode[index] + "/"; } return result.Trim();
Postup řešení Komprese morseovky: if (result2.Length % 8 != 0) { Length = result2.Length / 8 + 1; if (result2.Length % 8 == 1) result2 = "0000000" + result2; } else if (result2.Length % 8 == 2) result2 = "000000" + result2; else if (result2.Length % 8 == 3) result2 = "00000" + result2; else if (result2.Length % 8 == 4) result2 = "0000" + result2; else if (result2.Length % 8 == 5) result2 = "000" + result2; else if (result2.Length % 8 == 6) result2 = "00" + result2; else if (result2.Length % 8 == 7) result2 = "0" + result2; else Length = result2.Length / 8; Length = result2.Length; string ascii = ""; while (Length > 0) string pokus = result2.Substring(Length - 8, 8); ascii += BinaryToInt(pokus); result += char.ConvertFromUtf32(BinaryToInt(pokus)); Length = Length - 8; return result; Komprese morseovky: public String EncodeMorse2(String Morse) { int Length = Morse.Length; string result = ""; string result2 = ""; int i = 0; while (Length != 0) if (Length >= 2 && Morse.Substring(i, 2) == "//") result2 += "01"; Length = Length - 2; i = i + 2; } else if (Morse.Substring(i, 1) == ".") result2 += "10"; Length--; i++; else if (Morse.Substring(i, 1) == "-") result2 += "11"; else if (Morse.Substring(i, 1) == "/") result2 += "00";
Postup řešení Dekomprese morseovky: public string DecodeMorse2(String EncodedMorse) { string result = ""; string result3 = ""; System.Text.Encoding ascii = System.Text.Encoding.Unicode; Byte[] encodedBytes = ascii.GetBytes(EncodedMorse); foreach (Byte b in encodedBytes) string result2 = IntToBinary(b); if (b != 0) if (result2.Length % 8 != 0) if (result2.Length % 8 == 1) result2 = "0000000" + result2; } else if (result2.Length % 8 == 2) result2 = "000000" + result2; else if (result2.Length % 8 == 3) result2 = "00000" + result2; else if (result2.Length % 8 == 4) result2 = "0000" + result2; else if (result2.Length % 8 == 5) result2 = "000" + result2; else if (result2.Length % 8 == 6) result2 = "00" + result2; else if (result2.Length % 8 == 7) result2 = "0" + result2; result3 = result2 + result3; while (result3.StartsWith("0")) { result3 = result3.Remove(0, 1); } if (result3.Length % 2 != 0) result3 = result3.Insert(0, "0"); int Length = result3.Length; int i = 0; while (Length > 0) if (result3.Substring(i, 2) == "01") result += "//"; Length = Length - 2; i = i + 2; else if (result3.Substring(i, 2) == "10") result += "."; else if (result3.Substring(i, 2) == "11") result += "-"; else if (result3.Substring(i, 2) == "00") result += "/"; return result;
Prográmek
Děkujeme za pozornost Zdroje informací: www.google.com