Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
zmx0142857
GitHub Repository: zmx0142857/mini-games
Path: blob/master/c/morse/morse.cpp
363 views
1
//Convert Morse signal into comprehensible text, vise versa.
2
3
/*
4
char MorseCode binary decimal
5
=======================================
6
a .- 1111 1101 253
7
b -... 0000 1000 8
8
c -.-. 0000 1010 10
9
d -.. 0000 0100 4
10
e . 1111 1110 254
11
f ..-. 1111 0010 242
12
g --. 0000 0110 6
13
h .... 1111 0000 240
14
i .. 1111 1100 252
15
j .--- 1111 0111 247
16
k -.- 0000 0101 5
17
l .-.. 1111 0100 244
18
m -- 0000 0011 3
19
n -. 0000 0010 2
20
o --- 0000 0111 7
21
p .--. 1111 0110 246
22
q --.- 0000 1101 13
23
r .-. 1111 1010 250
24
s ... 1111 1000 248
25
t - 0000 0001 1
26
u ..- 1111 1001 249
27
v ...- 1111 0001 241
28
w .-- 1111 1011 251
29
x -..- 0000 1001 9
30
y -.-- 0000 1011 11
31
z --.. 0000 1100 12
32
*/
33
/*
34
1 .---- 1110 1111 239
35
2 ..--- 1110 0111 231
36
3 ...-- 1110 0011 227
37
4 ....- 1110 0001 225
38
5 ..... 1110 0000 224
39
6 -.... 0001 0000 16
40
7 --... 0001 1000 24
41
8 ---.. 0001 1100 28
42
9 ----. 0001 1110 30
43
0 ----- 0001 1111 31
44
*/
45
/*
46
? ..--.. 1100 1100 204
47
_ ..--.- 1100 1101 205
48
" .-..-. 1101 0010 210
49
+ .-.-. 1110 1010 234
50
. .-.-.- 1101 0101 213
51
@ .--.-. 1101 1010 218
52
' .----. 1101 1110 222
53
= -...- 0001 0001 17
54
- -....- 0010 0001 33
55
/ -..-. 0001 0010 18
56
; -.-.-. 0010 1010 42
57
! -.-.-- 0010 1011 43
58
() -.--.- 0010 1101 45
59
, --..-- 0011 0011 51
60
: ---... 0011 1000 56
61
*/
62
/*
63
Ŝ ...-. 1110 0010 226
64
É ..-.. 1110 0100 228
65
Ü ..-- 1111 0011 243
66
Ð ..--. 1110 0110 230
67
È .-..- 1110 1001 233
68
Ä .-.- 1111 0101 245
69
Þ .--.. 1110 1100 236
70
À .--.- 1110 1101 237
71
Ĵ .---. 1110 1110 238
72
Ç -.-.. 0001 0100 20
73
Ĥ -.--. 0001 0110 22
74
Ĝ --.-. 0001 1010 26
75
Ñ --.-- 0001 1011 27
76
Ö ---. 0000 1110 14
77
CH ---- 0000 1111 15
78
*/
79
80
#include<iostream>
81
using namespace std;
82
83
class Morse
84
{
85
public:
86
static string decode(string);
87
static string encode(string);
88
protected:
89
static string arrDecode[256];
90
static string arrEncode[128];
91
static unsigned decodeUnit(string); //Convert a Morse unit into a number (0-255)
92
};
93
94
string Morse::arrDecode[256]
95
={" ","t","n","m","d","k","g","o", "b","x","c","y","z","q","","CH",// 0- 15
96
"6","" ,"/","" ,"" ,"" ,"" ,"" , "7","" ,"" ,"" ,"8","" ,"9","0",// 16- 31
97
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"-",";","!","(",")","" ,"" ,// 32- 47
98
"" ,"" ,"" ,",","" ,"" ,"" ,"" , ":","" ,"" ,"" ,"" ,"" ,"" ,"" ,// 48- 63
99
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,// 64- 79
100
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,// 80- 95
101
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,// 96-111
102
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,//112-127
103
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,//128-143
104
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,//144-159
105
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,//160-175
106
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,//176-191
107
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" , "" ,"" ,"" ,"" ,"?","_","" ,"" ,//192-207
108
"" ,"" ,"\"","","" ,".","" ,"" , "" ,"" ,"@","" ,"" ,"" ,"\'","",//208-223
109
"5","4","" ,"3","" ,"" ,"" ,"2", "" ,"" ,"+","" ,"" ,"" ,"" ,"1",//224-239
110
"h","v","f","" ,"l","" ,"p","j", "s","u","r","w","i","a","e","", //240-255
111
};
112
113
string Morse::decode(string str)
114
{
115
string s;
116
char c='\0';
117
unsigned spacecount=1, i=0;
118
for(; (c=str[i])!='\0'; i++)
119
{
120
if((c=='-' || c=='.') && spacecount>0)
121
{
122
s=s+arrDecode[decodeUnit(&str[i])];
123
// 将str的第i位作为新的str的开头传递给unit函数
124
spacecount=0;
125
}
126
else if(c!='-' && c!='.')
127
spacecount++;
128
129
if(c=='/' ||c=='\\' ||c=='|' ||spacecount==3)
130
s=s+' ';
131
}
132
return s;
133
}
134
135
unsigned Morse::decodeUnit(string str)
136
{
137
char n='\0';
138
if(str[0]=='.')
139
n='\xff';
140
char c=0;
141
for(unsigned i=0; (c=str[i])!='\0'; i++)
142
{
143
n<<=1; //低位补0
144
if(c=='-')
145
n^=0x1;
146
else if(c=='.')
147
n&=0xfe;
148
else
149
{
150
n>>=1; //原高位是什么,高位就补什么
151
break;
152
}
153
}
154
return unsigned(n & 0xff);
155
}
156
157
string Morse::arrEncode[128]
158
={"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,// 0- 7
159
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,// 8- 15
160
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,// 16- 23
161
"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,// 24- 31
162
" " ,"-.-.--",".-..-.","" ,"" ,"" ,"" ,".----.",// 32- 39
163
"-.--..","-.--.-","" ,".-.-." ,"--..--","-....-",".-.-.-","-..-." ,// 40- 47
164
"-----" ,".----" ,"..---" ,"...--" ,"....-" ,"....." ,"-...." ,"--..." ,// 48- 55
165
"---.." ,"----." ,"---...","-.-.-.","" ,"-...-" ,"" ,"--..--",// 56- 63
166
".--.-.",".-" ,"-..." ,"-.-." ,"-.." ,"." ,"..-." ,"--." ,// 64- 71
167
"...." ,".." ,".---" ,"-.-" ,".-.." ,"--" ,"-." ,"---" ,// 72- 79
168
".--." ,"--.-" ,".-." ,"..." ,"-" ,"..-" ,"...-" ,".--" ,// 80- 87
169
"-..-" ,"-.--" ,"--.." ,"" ,"" ,"" ,"" ,"..--.-",// 88- 95
170
"" ,".-" ,"-..." ,"-.-." ,"-.." ,"." ,"..-." ,"--." ,// 96-103
171
"...." ,".." ,".---" ,"-.-" ,".-.." ,"--" ,"-." ,"---" ,//104-111
172
".--." ,"--.-" ,".-." ,"..." ,"-" ,"..-" ,"...-" ,".--" ,//112-119
173
"-..-" ,"-.--" ,"--.." ,"" ,"" ,"" ,"" ,"" ,//120-127
174
};
175
176
string Morse::encode(string str)
177
{
178
string s="";
179
for(unsigned i=0; str[i]!='\0'; i++)
180
s=s+arrEncode[str[i]]+' ';
181
return s;
182
}
183
184
185
int main()
186
{
187
cout<<"******** Welcome to the Morse Code Translator 1.0 by CLAYCRAFT ********\n"
188
<<" You can type either Morse code or English text below.\n"
189
<<" Tips:\n"
190
<<" * Split the Morse units with either a space' ', or a comma','.\n"
191
<<" * A '/','\\' or '|' will separate the words.\n"
192
<<" * For example, try typing:\n"
193
<<" .... . .-.. .-.. --- --..-- /.-- --- .-. .-.. -.. -.-.--\n"
194
<<" * Then you can type the words you see to check if this translation is\n"
195
<<" reversible."<<endl<<endl<<">>> ";
196
string s;
197
char c;
198
while(1)
199
{
200
cin>>s;
201
c=cin.get();
202
if(s[0]=='-' || s[0]=='.' || s[0]=='/' || s[0]=='\\' || s[0]=='|')
203
{
204
cout<<Morse::decode(s);
205
}
206
else
207
{
208
cout<<Morse::encode(s);
209
if(c==' ')
210
cout<<'/';
211
}
212
if(c=='\n')
213
cout<<endl<<endl<<">>> ";
214
}
215
return 0;
216
}
217
218