-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorse_encode.m
More file actions
40 lines (35 loc) · 883 Bytes
/
morse_encode.m
File metadata and controls
40 lines (35 loc) · 883 Bytes
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
function x = morse_encode(c)
m = morse();
[aux str x] = DivEtImp(m, c, "", "");
endfunction
% parcurgere arbore prin Divide et Impera
function [ morse, str, rez ] = DivEtImp(m, ch, s, rez)
morse = m;
str = s;
if isempty(m)
str = '*';
return;
else
% parcurge cele 2 ramuri recursiv
[ m1 str rez ] = DivEtImp(m{2}, ch, strcat(s,'.'), rez);
[ m2 str rez ] = DivEtImp(m{3}, ch, strcat(s,'-'), rez);
% cautare caracter si generarea sirului morse
if ~isempty(m1)
if strcmp(m1{1}, upper(ch))
rez = strcat(s, '.');
return;
endif
endif
% cautare caracter si generarea sirului morse
if ~isempty(m2)
if strcmp(m2{1}, upper(ch))
rez = strcat(s, '-');
return;
endif
endif
endif
% rezultat gol => *
if isempty(rez)
rez = "*";
endif
endfunction