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 ///
30 /// 待编码数据 key-value => 字母-mors
31 ///
32 public static Dictionary<string, string> UnCodeData
33 {
34 get{return unCodeData;}
35 private set{}
36 }
37 ///
38 /// 反编码数据 key-value => mors-字母
39 ///
40 public static Dictionary<string, string> CodeData
41 {
42 get{return codeData;}
43 private set{}
44 }
45 ///
46 /// 添加其他电码
47 ///
48 ///
49 ///
50 ///
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 ///
104 /// 编码为mors
105 ///
106 ///
107 ///
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 ///
124 /// 解码为mors电码
125 ///
126 ///
127 ///
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 }