-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorseCode.cpp
More file actions
292 lines (263 loc) · 7.53 KB
/
MorseCode.cpp
File metadata and controls
292 lines (263 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Alfred Ledgin
// 12/4/2015
// CS 303
// Project 3
#include "MorseCode.h"
#include "MorseLetter.h"
#include "Binary_Tree.h"
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>
using namespace std;
MorseCode::MorseCode()
{
defineStandardVec();
codeBuilt = false;
}
MorseCode::MorseCode(istream& input)
{
defineStandardVec();
buildCode(input);
}
void MorseCode::buildCode(istream& input)
{
if (!buildCodeList(input))
{
codeBuilt = false;
return;
}
codeList.sort();
buildCodeVec();
codeTree.read_tree(codeVec);
// References:
// Kuhail, Mohammad. "Binary_Tree.h." 2015. C++ header (.h) file.
// Kuhail, Mohammad. "Lecture 10: Introduction to Trees."
// CS 303 course materials, University of Missouri-Kansas City,
// Fall 2015. _Microsoft PowerPoint_ file. Slide 29.
codeBuilt = true;
}
const vector<string> MorseCode::parse(const string& input) const
{
vector<string> output;
int counter = 0;
while (counter < input.length())
{
string newMorseLetter;
while (input[counter] != ' ' && counter < input.length())
{ // Use spaces to separate needed substrings.
newMorseLetter += input[counter];
counter++;
}
if (newMorseLetter.length()) // Add non-empty strings to vector.
output.push_back(newMorseLetter);
counter++;
}
return output;
}
const char MorseCode::decodeLetter(const string& codedLetter) const
{
if (!testCode(codedLetter))
{
throw
std::exception("Invalid Morse Code.");
}
if (isBuilt())
{
try
{
return decodeLetterInternal(codedLetter, 0, codeTree)[0];
}
catch (std::exception)
{
throw
std::exception("Letter not found.");
}
}
else
{
throw
std::exception("Code set not found.");
}
}
const string MorseCode::decodeLetterInternal(const string& codedLetter, int index,
Binary_Tree<string> tempTree) const
{
if (index == codedLetter.length() - 1)
{
if (codedLetter[index] == '.')
return tempTree.get_left_subtree().get_data();
else
return tempTree.get_right_subtree().get_data();
}
else
{
if (codedLetter[index] == '.')
return decodeLetterInternal(codedLetter, index + 1,
tempTree.get_left_subtree());
else
return decodeLetterInternal(codedLetter, index + 1,
tempTree.get_right_subtree());
}
}
// Reference for Binary Tree Functions:
// Kuhail, Mohammad. "Lecture 10: Introduction to Trees."
// CS 303 course materials, University of Missouri-Kansas City,
// Fall 2015. _Microsoft PowerPoint_ file. Slide 24.
const string MorseCode::encodeLetter(char letter) const
{
if (isBuilt())
{
if (letter >= 'A' && letter <= 'Z') // Convert upper case to lower.
letter += 32;
// Reference for ASCII conversion:
// Wikipedia contributors. "ASCII." _Wikipedia, The Free
// Encyclopedia_. Wikipedia, The Free Encyclopedia,
// 20 Nov. 2015. Web. 3 Dec. 2015.
// <https://en.wikipedia.org/wiki/ASCII>.
// Distributed under Creative Commons
// Attribution-ShareAlike License:
// <http://creativecommons.org/licenses/by-sa/3.0/>.
try
{
return codeMap.at(letter);
// Reference:
// "std::map::at." _cplusplus.com_. cplusplus.com, 2015. Web.
// 29 Nov. 2015.
// <http://www.cplusplus.com/reference/map/map/at/>.
}
catch (std::exception)
{
throw
std::exception("Character is not a letter.");
}
}
else
{
throw
std::exception("Code set not found.");
}
}
const string MorseCode::decodeWord(const vector<string>& input) const
{
if (isBuilt())
{
string output;
for (int counter = 0; counter < input.size(); counter++)
{
output += decodeLetter(input[counter]);
}
return output;
}
else
{
throw
std::exception("Code set not found.");
}
}
const string MorseCode::encodeWord(const string& input) const
{
if (isBuilt())
{
string output;
for (int counter = 0; counter < input.length(); counter++)
{
output += encodeLetter(input[counter]);
if (counter < input.length() - 1)
output += " "; // Put spaces between Morse Code letters.
}
return output;
}
else
{
throw
std::exception("Code set not found");
}
}
const string MorseCode::interpret(const string& input) const
{
if (isBuilt())
{
vector<string> currentLetters = parse(input);
return decodeWord(currentLetters);
}
else
{
throw
std::exception("Code set not found.");
}
}
const bool MorseCode::buildCodeList(istream& input)
{
codeList.clear(); // Initialize list.
// Reference:
// "std::list." _cplusplus.com_. cplusplus.com, 2015.
// Web. 4 Dec. 2015.
// <http://www.cplusplus.com/reference/list/list/>.
string entry;
char letter = ' ';
string code;
MorseLetter pair;
while (input >> entry)
{
code = "";
letter = entry[0]; // The first character of each line is a letter.
for (int counter = 1; counter < entry.length(); counter++)
{
code += entry[counter]; // The following characters are Morse Code.
}
if (!testCode(code))
return false;
codeMap[letter] = code; // Build the map while building the list.
pair.define(letter, code);
codeList.push_back(pair);
}
return true;
}
const bool MorseCode::testCode(const string& inputCode) const
{
for (int counter = 0; counter < inputCode.length(); counter++)
{
if (inputCode[counter] != '.' && inputCode[counter] != '_')
return false;
}
return true;
}
void MorseCode::defineStandardVec()
{
standardVec = { "0", "00", "000", "0000",
"0001", "001", "0010", "0011", "01", "010", "0100", "0101",
"011", "0110", "0111", "1", "10", "100", "1000", "1001",
"101", "1010", "1011", "11", "110", "1100", "1101", "111",
"1110", "1111" }; // Checked against hand-drawn tree.
}
void MorseCode::buildCodeVec()
{
codeVec = {}; // Initialize vector.
codeVec.push_back(" "); // Put a space at the root of the tree.
list<MorseLetter>::iterator iter = codeList.begin();
int counter = 0;
while (iter != codeList.end())
{
if (iter->hasCode() == standardVec[counter]) // Match found.
{
codeVec.push_back(iter->hasLetter());
if (iter->hasCode().length() == 4) // Max depth.
{
for (int two = 0; two < 2; two++)
{
codeVec.push_back("NULL"); // Establish leaf nodes.
}
}
iter++;
}
else
codeVec.push_back("NULL"); // Where a non-leaf has only one child.
counter++;
}
}
// Reference for Idea:
// Kuhail, Mohammad. "Lecture 10: Introduction to Trees."
// CS 303 course materials, University of Missouri-Kansas City,
// Fall 2015. _Microsoft PowerPoint_ file. Slide 29.