-
Notifications
You must be signed in to change notification settings - Fork 0
/
toMorse.cpp
63 lines (58 loc) · 1.22 KB
/
toMorse.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
#include <iostream>
using namespace std;
#define SIZE 80 //this many characters will it read at once
//translates one character to Morse
void charToMorse(char c)
{
string morse[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "..._", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."};
//letters
if(c >= 'a' && c <= 'z')
{
c = c - 'a';
}
else if (c >= 'A' && c <= 'Z')
{
c = c - 'A';
}
//numbers
else if (c >= '0' && c <= '9')
{
c = c - '0' + 'Z' - 'A';
}
else
{
//special symbols
switch(c)
{
case '+': cout << ".-.-./"; return;
case '=': cout << "-...-/"; return;
case '/': cout << "-..-./"; return;
default: return;
}
}
cout << morse[(int)c] << "/";
}
//goes through the chracters and then translates them one by one
void toMorse(char s[SIZE])
{
for(int i = 0; i < SIZE; ++i)
{
if(s[i] == 0)
{
break;
}
charToMorse(s[i]);
}
cout << endl;
}
int main(void)
{
cout << "Write something and it will be translated to morse" << endl;
while(!cin.eof())
{
char s[SIZE] = {0};
cin >> s;
toMorse(s);
}
return 0;
}