posts - 183,  comments - 10,  trackbacks - 0

来自于《编码》

电报中用的莫尔斯编码,对电报一直感到很神奇,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<charstring>& encoding)
 9 {
10     string tmp;
11     for (string::size_type i = 0; i != sentence.size(); ++i)
12     {
13         map<charstring>::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<stringchar>& 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<stringchar>::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<charstring> encoding;
64     map<stringchar> 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 阅读(458) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理