來自于《編碼》
電報中用的莫爾斯編碼,對電報一直感到很神奇,dita data...
基本原理就是用的二進制方式,用兩種形式,即短信號和長信號,分別用兩種形式表示
可以是
點和劃
也可以是
0 和 1
| 和 -
a 和 b
等。
一種編碼最重要的是如何對每個符號進行編碼,這里需要考慮
編碼的效率,哪些是常用的?哪些是非常用的?
編碼的形式,前綴碼?
如何編碼,編碼的實現?
如何解碼,解碼的實現?
莫爾斯已經給出了各個符號的編碼,例如 morseCode.txt:
1 A .-
2 B -
3 C -.-.
4 D -..
5 E .
6 F ..-.
7 G --.
8 H
.
9 I ..
10 J .---
11 K -.-
12 L .-..
13 M --
14 N -.
15 O ---
16 P .--.
17 Q --.-
18 R .-.
19 S 
20 T -
21 U ..-
22 V
-
23 W .--
24 X -..-
25 Y -.--
26 Z --..
27 1 .----
28 2 ..---
29 3
--
30 4
.-
31 5
..
32 6 -
.
33 7 --
34 8 ---..
35 9 ----.
36 10 -----
37 . .-.-.-
38 , --..--
39 ? ..--..
40 : ---
41 ; -.-.-.
42 - -
.-
43 / -..-.
44 " .-..-.
45 ' .----.
46 ( -.--.
47 ) -.--.-
48 = -
-
49 + .-.-.
50 $
-..-
51 | .-.-..
52 _ ..--.-
這樣針對一句話,可以進行編碼了,例如這句話:
Before the police moved in, they set up a battery of klieg lights and aimed them into the park.
編碼得到:
-... . ..-. --- .-. . - .... . .--. --- .-.. .. -.-. . -- --- .
..- . -.. .. -. --..-- - .... . -.-- ... . - ..- .--.
.- -... .- - - . .-. -.-- --- ..-. -.- .-.. .. . --. .-.. ..
--. .... - ... .- -. -.. .- .. -- . -.. - .... . -- .. -. - ---
- .... . .--. .- .-. -.- .-.-.-
在針對這個編碼進行解碼得到:
BEFORE THE POLICE MOVED IN, THEY SET UP A BATTERY OF KLIEG LIGHTS AND AIMED THEM
INTO THE PARK.
字母都是按照大寫處理的。
Before the police moved in, they set up a battery of klieg lights and aimed them
into the park.
-... . ..-. --- .-. . - .... . .--. --- .-.. .. -.-. . -- --- .
..- . -.. .. -. --..-- - .... . -.-- ... . - ..- .--.
.- -... .- - - . .-. -.-- --- ..-. -.- .-.. .. . --. .-.. ..
--. .... - ... .- -. -.. .- .. -- . -.. - .... . -- .. -. - ---
- .... . .--. .- .-. -.- .-.-.-
BEFORE THE POLICE MOVED IN, THEY SET UP A BATTERY OF KLIEG LIGHTS AND AIMED THEM
INTO THE PARK.
這就是莫爾斯電碼。
實現:
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <map>
5 #include <cctype>
6 using namespace std;
7
8 string encode(const string& sentence, const map<char, string>& encoding)
9 {
10 string tmp;
11 for (string::size_type i = 0; i != sentence.size(); ++i)
12 {
13 map<char, string>::const_iterator cit = encoding.find(static_cast<char>(toupper(sentence[i])));
14 if (cit != encoding.end())
15 {
16 tmp += cit->second;
17 tmp += ' ';
18 }
19 else
20 {
21 tmp += '\t';
22 }
23 }
24 return tmp;
25 }
26
27 string decode(const string& mc, const map<string, char>& decoding)
28 {
29 string tmp, m;
30 for (string::size_type i = 0; i != mc.size(); ++i)
31 {
32 if (mc[i] == ' ')
33 {
34 map<string, char>::const_iterator cit = decoding.find(m);
35 if (cit != decoding.end())
36 {
37 tmp += cit->second;
38 }
39 m.clear();
40 }
41 else if (mc[i] == '\t')
42 {
43 tmp += ' ';
44 }
45 else
46 {
47 m += mc[i];
48 }
49 }
50 return tmp;
51 }
52
53 int main()
54 {
55 ifstream fin("morseCode.txt");
56 if (!fin)
57 {
58 cerr << "File error!" << endl;
59 return 1;
60 }
61 char a;
62 string b;
63 map<char, string> encoding;
64 map<string, char> decoding;
65 while (fin >> a >> b)
66 {
67 encoding[a] = b;
68 decoding[b] = a;
69 }
70 fin.close();
71 string sentence;
72 string mc;
73 while (getline(cin, sentence))
74 {
75 mc = encode(sentence, encoding);
76 cout << mc << endl;
77 sentence = decode(mc, decoding);
78 cout << sentence << endl;
79 }
80 return 0;
81 }
82
posted on 2011-11-15 17:13
unixfy 閱讀(486)
評論(0) 編輯 收藏 引用