-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorse_Coder.cpp
More file actions
183 lines (163 loc) · 4.77 KB
/
Morse_Coder.cpp
File metadata and controls
183 lines (163 loc) · 4.77 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
#include "Morse_Coder.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
Morse_Coder::Morse_Coder() {
letter_tree = build_morse_tree();
code_key = {};
}
Binary_Tree<string> Morse_Coder::build_morse_tree() {
// open file for reading & create ifstream object to work with
ifstream fin("morse.txt");
// create an empty tree to begin with
Binary_Tree<string> result("");
// declare variables to store parts of each line of the file in.
string current_line;
char temp_char;
string letter;
// for each line in the file
do { // for each line of file
// make the variable local_root a pointer to the tree's root
BTNode<string>* local_root = result.getRoot();
getline(fin, current_line); // get first line of file as string
stringstream line_of_file; // create a stringstream object
line_of_file.str(current_line); // place the current line in the stringstream
line_of_file >> temp_char; // read the first character (the letter) and strore it in temp_char
letter = char_to_str(temp_char); // convert the letter to a string
// read remaining character in line, creating/traversing left children
// for '.' characters, and right children for '_' characters.
while (line_of_file >> temp_char) {
if (temp_char == '.') {
if (local_root->left == NULL) {
local_root->left = new BTNode<string>("");
}
local_root = local_root->left;
}
if (temp_char == '_') {
if (local_root->right == NULL) {
local_root->right = new BTNode<string>("");
}
local_root = local_root->right;
}
}
local_root->data = letter;
} while (!fin.eof()); // there are still lines in the file (Could hard-code this to exactly 26 iterations)
return result;
}
string Morse_Coder::char_to_str(char the_char)
{
stringstream ss;
string s;
ss << the_char;
ss >> s;
return s;
}
//conver text into the morse code by using map
string Morse_Coder::encode_to_code(string text)
{
// convert text to lower letters
for (int j = 0; text[j] != '\0'; j++)
{
if (!isalpha(text[j]))
{
return"ERROR: Only latin alphabet can be entered";
}
text[j] = tolower(text[j]);
}
//decalaring a map
map<const char, string> encode;
//initializing map
encode['a'] = ".-";
encode['b'] = "-...";
encode['c'] = "-.-.";
encode['d'] = "-..";
encode['e'] = ".";
encode['f'] = "..-.";
encode['g'] = "--.";
encode['h'] = "....";
encode['i'] = "..";
encode['j'] = ".---";
encode['k'] = "-.-";
encode['l'] = ".-..";
encode['m'] = "--";
encode['n'] = "-.";
encode['o'] = "---";
encode['p'] = ".--.";
encode['q'] = "--.-";
encode['r'] = ".-.";
encode['s'] = "...";
encode['t'] = "-";
encode['u'] = "..-";
encode['v'] = "...-";
encode['w'] = ".--";
encode['x'] = "-..-";
encode['y'] = "-.--";
encode['z'] = "--..";
// iterator to iterate through map in order to find the right code for letter
map<char, string>::iterator iter;
string result;
for (int i = 0; text[i]; i++)
{
result += encode.find(text[i])->second + " ";
}
return result;
}
// convert Morse code into text
string Morse_Coder::decode_to_text(string text)
{
// create a binary tree
Binary_Tree<string> Btree = build_morse_tree();
string the_result;
// initialize current node with a root of binary tree
BTNode<string>* current = Btree.getRoot();
// if current node is null return an empty sting
if (current == NULL)
{
return "";
}
//loop through text in order to fing coresponding letter to code
for (int i = 0; i <= text.length(); i++)
{
if (text[i] == '.' && i!=text.length() && current->left==NULL || text[i] == '-' && i!=text.length() && current->right==NULL)
{
return "ERROR: Incorrect code been entered";
}
// error checking if inccorect code been entered
if (text[i] != '.' && text[i] != '-' && text[i] != ' ' && text[i] != '\0')
{
return "ERROR: Incorrect code been entered.";
}
// if code equals . go left
if (text[i] == '.')
{
if (current->left != NULL)
{
current = current->left;
}
}
// if code equals - go right
if (text[i] == '-')
{
if (current->right != NULL)
{
current = current->right;
}
}
// if space or end of text is reached get the data
if (text[i] == ' ' || text[i]=='\0')
{
the_result += current->data;
if (text[i] == '\n')
{
return the_result;
}
else if(text[i]==' ')
{
// make a current node point to the root
current = Btree.getRoot();
}
}
}
return the_result;
}