-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCT.cpp
105 lines (90 loc) · 3.13 KB
/
MCT.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
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
int pos(string s, string a[])
{
for (int i = 0; i < 37; i++)
{
if (s == a[i])
{
return i;
}
}
return -1;
}
int main()
{
string morse[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----",
".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "/"};
string letter[] = {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " "};
int choice, l;
string morse_code, t_morse, c, plain, t_plain;
do
{
cout << "1 for Morse code To Plain.\n";
cout << "2 for Plain To Morse code.\n";
cout << "3 for Quit.\n\n";
cout << "Enter your choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter Morse code(eg. .... . .-.. .-.. --- / .-- --- .-. .-.. -..):";
cin.ignore();
plain.clear();
getline(cin, morse_code);
morse_code.append(" ");
l = morse_code.length();
for (int i = 0; i < l; i++)
{
c = morse_code[i];
if (!(c == " " || c == "-" || c == "/" || c == "." || c == "\0"))
{
cout << "Morse code is not vaild.";
break;
}
if (c != " ")
t_morse.append(c);
else if (c == " " || c == "\0")
{
if (pos(t_morse, morse) == -1)
cout << "Morse code is not vaild.";
else
{
plain.append(letter[pos(t_morse, morse)]);
t_morse.clear();
}
}
}
cout << plain << endl;
break;
case 2:
cout << "Enter plain text:";
cin.ignore();
getline(cin, plain);
morse_code.clear();
transform(plain.begin(), plain.end(), plain.begin(), ::toupper);
l = plain.length();
for (int i = 0; i < l; i++)
{
c = plain[i];
if (!((c <= "z" && c >= "a") || (c <= "Z" && c >= "A") || (c <= "9" && c >= "0") || c == " " || c == "\0"))
{
cout << "Plain text is not vaild.";
break;
}
morse_code.append(morse[pos(c, letter)]);
morse_code.append(" ");
c.clear();
}
cout << morse_code << endl;
break;
default:
cout << "Entered choice is wrong...";
break;
}
} while (choice != 3);
return 0;
}