一分钟记住常用的摩尔斯电码

1:今天刷知乎时看到了个摩尔斯电码的对照表,感觉还是很好记忆的。练习几十个字母就差不多了。如下图:是不是很好记~~
2:在电影星际穿越中男主cooper就是通过钟表的滴答进行信息传递的,还有去年is杀害日本人质时,媒体猜测被害人在遇害时通过眨眼睛对世人说“不要救我,放弃我”。
3:上大图
4:上大图
  1 /*简单问题要复杂会,看起来很厉害的样子,有时候仅仅是打印了一个helloworld而已~~*/   
  2  class Program
  3     {
  4         static void Main(string[] args)
  5         {
  6             Mors.AddCodeData("testkey", "testvalue");
  7             var testStr="hello boy";
  8             var resultEncode=Mors.Encode(testStr);
  9             var resultDecode=Mors.Decode(resultEncode);
 10             Console.WriteLine("Encode:" + resultEncode+"\n"+"Decode"+resultDecode);
 11             Console.ReadLine();
 12         }
 13     }
 14     public static class Mors
 15     {
 16         private static Dictionary<string, string> unCodeData = new Dictionary<string, string>()
 17         {
 18             {"a",".-"},{"b","-..."},{"c","-.-."},{"d","-.."},{"e","."},{"f","..-."},{"g","--."},{"h","...."},{"i",".."},{"j",".---"},{"k","-.-"},
 19             {"l",".-.."},{"m","--"},{"n","-."},{"o","---"},{"p",".--."},{"q","--.-"},{"r",".-."},{"s","..."},{"t","-"},{"u","..-"},{"v","...-"},
 20             {"w",".--"},{"x","-..-"},{"y","-.--"},{"z","--.."},{",","--..--"},{".",".-.-.-"}
 21         };
 22         private static Dictionary<string, string> codeData = new Dictionary<string, string>()
 23         {
 24             {".-","a"},{"-...","b"},{"-.-.","c"},{"-..","d"},{".","e"},{"..-.","f"},{"--.","g"},{"....","h"},{"..","i"},{".---","j"},{"-.-","k"},
 25             {".-..","l"},{"--","m"},{"-.","n"},{"---","o"},{".--.","p"},{"--.-","q"},{".-.","r"},{"...","s"},{"-","t"},{"..-","u"},{"...-","v"},
 26             {".--","w"},{"-..-","x"},{"-.--","y"},{"--..","z"},{"--..--",","},{".-.-.-","."}
 27         };
 28        
 29         /// <summary>
 30         /// 待编码数据 key-value => 字母-mors
 31         /// </summary>
 32         public  static Dictionary<string, string> UnCodeData
 33         { 
 34             get{return unCodeData;}
 35             private set{}
 36         }
 37         /// <summary>
 38         /// 反编码数据 key-value => mors-字母
 39         /// </summary>
 40         public  static Dictionary<string, string> CodeData
 41         { 
 42             get{return codeData;}
 43             private set{}
 44         }
 45         /// <summary>
 46         /// 添加其他电码
 47         /// </summary>
 48         /// <param name="key"></param>
 49         /// <param name="value"></param>
 50         /// <returns></returns>
 51         public static bool AddUncodeData(string key,string value)
 52         {
 53             if(!UnCodeData.Keys.Contains(key))
 54             {
 55                 UnCodeData.Add(key,value);
 56                 if(CodeData.Keys.Contains(value))
 57                     throw new Exception("data error");
 58                 CodeData.Add(value,key);
 59                 return true;
 60             }
 61             return false;
 62         }
 63         public static bool AddCodeData(string key,string value)
 64         {
 65             if(!CodeData.Keys.Contains(key))
 66             {
 67                 CodeData.Add(key,value);
 68                 if(UnCodeData.Keys.Contains(value))
 69                     throw new Exception("data error");
 70                 UnCodeData.Add(value,key);
 71                 return true;
 72             }
 73             return false;
 74         }
 75         public static bool RemoveUncodeData(string key)
 76         {
 77             if(UnCodeData.Keys.Contains(key))
 78             {
 79                 var value=UnCodeData[key];
 80                 if(codeData.Keys.Contains(value))
 81                 {
 82                     UnCodeData.Remove(key);
 83                     codeData.Remove(value);
 84                 }
 85                 return true;
 86             }
 87             return false;
 88         }
 89         public static bool RemoveCodeData(string key)
 90         {
 91             if (CodeData.Keys.Contains(key))
 92             {
 93                 var value = CodeData[key];
 94                 if (UnCodeData.Keys.Contains(value))
 95                 {
 96                     CodeData.Remove(key);
 97                     UnCodeData.Remove(value);
 98                 }
 99                 return true;
100             }
101             return false;
102         }
103         /// <summary>
104         /// 编码为mors
105         /// </summary>
106         /// <param name="str"></param>
107         /// <returns></returns>
108         public static string Encode(string str)
109         {
110             /// hello word
111             if (str.Length < 0)
112                 return string.Empty;
113             var words = Regex.Replace(str.Trim(), @"\s+", " ").Split(' ');
114             var result=string.Join("/",words.Select(word=>
115                            {
116                                return string.Join(" ", word.ToCharArray().Select(letter =>
117                                         {
118                                             return UnCodeData[letter.ToString()];
119                                         }));  
120                            }));
121             return result;
122         }
123         /// <summary>
124         /// 解码为mors电码
125         /// </summary>
126         /// <param name="str"></param>
127         /// <returns></returns>
128         public static string Decode(string str)
129         {
130             //..- ---- .--. .-
131             var words = Regex.Replace(str.Trim(), @"\s+", " ").Split('/').Select(word=>word.Trim()).ToArray();
132             var result=string.Join(" ", words.Select(word=>
133                      {
134                       return string.Join("",  word.Split(' ').Select(letter=>
135                          {
136                            return CodeData[letter.ToString()];
137                          }));
138                      }));
139             return result;
140         }
141     }
View Code

 点赞的伙伴今年都能找到像楼上一样漂亮的女神~~(我不是来骗赞的~~不信你试试)

posted @ 2016-07-23 18:07  李辉健  阅读(101600)  评论(0编辑  收藏  举报