-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.cpp
More file actions
54 lines (42 loc) · 1.47 KB
/
func.cpp
File metadata and controls
54 lines (42 loc) · 1.47 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
#include "header.h"
string convertFromNatToMorse(string str) {
string convertedText = "";
for (unsigned int i = 0; i <= str.length(); ++i) {
str[i] = toupper(str[i]);
if ((str[i] < LOWER_NUMBER_BOUND || str[i] > UPPER_NUMBER_BOUND)
&& (str[i] < LOWER_LETTER_BOUND || str[i] > UPPER_LETTER_BOUND)
&& str[i] != ' ' && str[i] != '\0')
{
cout << "Errore: Trovato un carattere non supportato\n";
return "";
}
if (str[i] == ' ') ++i;
if (str[i] == '\0') break;
convertedText.append(addMorseChar(string(str[i], 1) + " "));
}
return convertedText;
}
string convertFromMorseToNat(string str) {
string convertedText = "";
int pos = 0;
for (unsigned int i = 0; i <= str.length(); ++i) {
if (str[i] != '.' && str[i] != '-' && str[i] != ' ' && str[i] != '\0') {
cout << "Errore: Trovato un carattere non supportato\n";
return "";
}
if (str[i] == ' ' || str[i] == '\0') {
convertedText.append(addNatChar(str.substr(pos, i - pos)));
pos = i + 1;
}
}
return convertedText;
}
string addNatChar(string toAdd)
{
auto ret = fromMorseToNatMap.find(toAdd);
return (ret == fromMorseToNatMap.end() ? "" : ret->second);
}
string addMorseChar(string toAdd) {
auto ret = fromNatToMorseMap.find(toAdd);
return (ret == fromNatToMorseMap.end() ? "" : ret->second);
}