-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
67 lines (58 loc) · 1.74 KB
/
main.c
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
#include <stdio.h>
#include <ctype.h>
#include <AudioToolbox/AudioToolbox.h>
#include <unistd.h>
#include <string.h>
const char* convertTextToMorse(char letter) {
static const char* morseTable[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
if (letter >= 'a' && letter <= 'z') {
return morseTable[letter - 'a'];
} else if (letter >= 'A' && letter <= 'Z') {
return morseTable[letter - 'A'];
} else {
return "";
}
}
void playDot() {
AudioServicesPlaySystemSound(kSystemSoundID_UserPreferredAlert);
usleep(100000); // Joue pendant 100 ms
}
void playDash() {
AudioServicesPlaySystemSound(kSystemSoundID_UserPreferredAlert);
usleep(300000); // Joue pendant 300 ms
}
void playMorseCode(const char* morse) {
size_t length = strlen(morse);
for(int i = 0; i < length; i++) {
switch(morse[i]) {
case '.':
playDot();
break;
case '-':
playDash();
break;
case ' ':
usleep(200000); // Pause pendant 200 ms
break;
}
usleep(100000); // Espace entre chaque symbole
}
}
int main() {
char* text = (char*) malloc(60 * sizeof(char));;
printf("word : ");
fgets(text,sizeof(strlen(text)),stdin);
size_t length = strlen(text);
for(int i = 0; i < length; i++) {
const char* morseCode = convertTextToMorse(tolower(text[i]));
printf("%c\n", text[i]);
printf("%s\n", morseCode);
playMorseCode(morseCode);
}
free(text);
return 0;
}