-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
94 lines (71 loc) · 2.89 KB
/
main.cpp
File metadata and controls
94 lines (71 loc) · 2.89 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
#include "header.h"
unordered_map<string, string> fromMorseToNatMap = generateMorseToNatMap();
unordered_map<string, string> fromNatToMorseMap = generateNatToMorseMap();
int main(int argc, char* argv[])
{
// Argument checking
if (argc == 2){
std::cout << "Error, you must choose a string to convert as second argument";
return EXIT_FAILURE;
}
if (argc > 3) {
std::cout << "Error, more than three arguments are passed";
return EXIT_FAILURE;
}
if (argc == 3 && ((string(argv[1]) != "-mn") && (string(argv[1]) != "-nm"))) {
std::cout << "Error, you can choose argument -mn or -nm";
return EXIT_FAILURE;
}
// Checking arguments for different modes
if (argc == 3 && (string(argv[1]) == "-mn")) {
string ret = convertFromMorseToNat((string)argv[2]);
if (!ret.empty()) cout << ret << endl;
return EXIT_SUCCESS;
}
else if (argc == 3 && (string(argv[1]) == "-nm")) {
string ret = convertFromNatToMorse((string)argv[2]);
if (!ret.empty()) cout << ret << endl;
return EXIT_SUCCESS;
}
startMenu();
return EXIT_SUCCESS;
}
void startMenu() {
int choice;
cout << "Salve, questo programma ti permette di convertire il codice morse in linguaggio naturale o viceversa.\n";
while (true) {
cout << "\n__ Menu' principale __\n\t- 1 per convertire da naturale a morse\n\t- 2 per convertire da morse a naturale\n\t- 0 per uscire\nScegliere la modalita': ";
cin >> choice;
while (choice != EXIT_CHOICE && choice != FROM_NAT_TO_MORSE && choice != FROM_MORSE_TO_NAT) {
cout << "Hai scelto un valore non valido, prego riprovare: ";
cin >> choice;
}
if (choice == EXIT_CHOICE) break;
else if (choice == FROM_NAT_TO_MORSE || choice == FROM_MORSE_TO_NAT) selectedMode(choice);
}
return;
}
void selectedMode(int mode) {
if (mode != FROM_MORSE_TO_NAT && mode != FROM_NAT_TO_MORSE) {
cout << "Fatal error: a bad mode has been selected!\n";
return;
}
cin.ignore();
cout << (mode == FROM_MORSE_TO_NAT
? "\n\n__ Modalità Morse -> Linguaggio Naturale __\n"
: "\n\n__ Linguaggio Naturale -> Modalità Morse __\n"
);
while (true) {
string inputText, finalText = "";
cout << "Prego inserire la stringa da tradurre (cliccare su ESC e premere invio per tornare al menu principale):\n";
getline(cin, inputText);
while (inputText.empty()) {
cout << "Errore: non puoi tradurre una stringa vuota, riprovare: \n";
getline(cin, inputText);
}
if (inputText[0] == ESC_BUTTON) break;
finalText = (mode == FROM_MORSE_TO_NAT ? convertFromMorseToNat(inputText) : convertFromNatToMorse(inputText));
if (!finalText.empty()) cout << finalText << endl;
}
return;
}