-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
134 lines (122 loc) · 2.76 KB
/
main.cpp
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
#include<iostream>
#include<vector>
#include<map>
#include <functional>
#include <sstream>
using namespace std;
enum child {
left,
right
};
//Available Commands
const string ENCODE_COMMAND = "encode";
const string DECODE_COMMAND = "decode";
const string HELP_COMMAND = "--help";
void decode(string input) {
map<string, vector<string>> morseTree = {
{"ROOT", {"E", "T"}},
{"E", {"I", "A"}},
{"T", {"N", "M"}},
{"I", {"S", "U"}},
{"A", {"R", "W"}},
{"N", {"D", "K"}},
{"M", {"G", "O"}},
{"S", {"H", "V"}},
{"U", {"F", "NIL"}},
{"R", {"L", "NIL"}},
{"W", {"P", "J"}},
{"D", {"B", "X"}},
{"K", {"C", "Y"}},
{"G", {"Z", "Q"}}
};
string output = "", cur = "ROOT";
int spcount = 0;
for (auto C : input) {
if (C == ' ') {
if (!spcount) { if (cur != "ROOT") output += cur; spcount++; }
else { output += " "; spcount = 0; }
cur = "ROOT";
}
else {
spcount = 0;
if (C == '.')cur = morseTree[cur][child::left];
else cur = morseTree[cur][child::right];
}
}
if (cur != "ROOT") output += cur;
cout << "Decoded Text: " << output << endl;
}
void encode(string input) {
map<char, string> dec = {
{'A', ".-"},
{'B', "-..."},
{'C', "-.-."},
{'D', "-.."},
{'E', "."},
{'F', "..-."},
{'G', "--."},
{'H', "...."},
{'I', ".."},
{'J', ".---"},
{'K', "-.-"},
{'L', ".-.."},
{'M', "--"},
{'N', "-."},
{'O', "---"},
{'P', ".--."},
{'Q', "--.-"},
{'R', ".-."},
{'S', "..."},
{'T', "-"},
{'U', "..-"},
{'V', "...-"},
{'W', ".--"},
{'X', "-..-"},
{'Y', "-.--"},
{'Z', "--.."}
};
string output = "";
for (auto i : input) {
if (i != ' ') {
output += dec[toupper(i)] + " ";
}
else {
output += " ";
}
}
cout << "Encoded Text: " << output << endl;
}
void printHelp() {
const string helpText(
"usage: morseconverter\t[decode <text>] Use one space to seperate letters and two spaces to seperate words.\n"
"\t\t\t[encode <text>] Only alphabet conversion supported. Example: encode 'HeY'\n"
);
std::cout << helpText << endl;
}
//Map to bind commands to functions
map<string, function<void(string)>> funcMap =
{ { ENCODE_COMMAND, encode},
{ DECODE_COMMAND, decode}
};
int main(int argc, char** argv) {
if (argc > 1)
{
string operation = argv[1];
if (operation == ENCODE_COMMAND || operation == DECODE_COMMAND)
{
if (argc < 3)
{
cout << "Too few arguments!\n";
exit(0);
}
else {
string input = argv[2];
funcMap[operation](input);
}
}
else if (operation == HELP_COMMAND) printHelp();
else cout << "Operation not identified! Use --help for more information" << endl;
}
else cout << "Not command found, use --help for more information" << endl;
return 0;
}