The main idea of the program is to code text into morse and decode morse into text.
But the program is broke, i don't know why the program doesn't work when i write the space between two words, and i have this problem with the code and decode.
void encode_out(const char *s){
for(;;++s){
char ch = *s;
if(ch == '\0')
break;
if(isalpha(ch)){
ch = toupper(ch);
fputs(table[ALPHA][ch - 'A'], stdout);//`-'A'` depend on the sequence of character code
} else if(isdigit(ch))
fputs(table[NUM][ch - '0'], stdout);
else if(ch == ' ')
fputc('/', stdout);//need rest space skip ?
else
;//invalid character => ignore
fputc(' ', stdout);
}
fputc('\n', stdout);
}
static void decode_out_aux(MTree *tree, const char *s){
if(tree == NULL) return;
if(*s == '\0')
fputc(tree->value, stdout);
else if(*s == '/')
fputc(' ', stdout);
else if(*s == '.')
decode_out_aux(tree->dot, ++s);
else if(*s == '-')
decode_out_aux(tree->bar, ++s);
}
void decode_out(const char *s){
char *p;
while(*s){
p = strchr(s, ' ');
if(p){
if(p-s != 0){
char code[p-s+1];
memcpy(code, s, p-s);
code[p-s]='\0';
decode_out_aux(root, code);
}
s = p + 1;
} else {
decode_out_aux(root, s);
break;
}
}
fputc('\n', stdout);
}
static void insert_aux(MTree **tree, char ch, const char *s){
if(*tree == NULL)
*tree = calloc(1, sizeof(**tree));
if(*s == '\0')
(*tree)->value = ch;
else if(*s == '.')
insert_aux(&(*tree)->dot, ch, ++s);
else if(*s == '-')
insert_aux(&(*tree)->bar, ch, ++s);
}
static inline void insert(char ch, const char *s){
if(*s == '.')
insert_aux(&root->dot, ch, ++s);
else if(*s == '-')
insert_aux(&root->bar, ch, ++s);
}
void make_tree(void){
root = calloc(1, sizeof(*root));
//root->value = '/';//anything
int i;
for(i = 0; i < 26; ++i)
insert('A'+i, table[ALPHA][i]);
for(i = 0; i < 10; ++i)
insert('0'+i, table[NUM][i]);
}
static void drop_tree_aux(MTree *root){
if(root){
drop_tree_aux(root->dot);
drop_tree_aux(root->bar);
free(root);
}
}
void drop_tree(void){
drop_tree_aux(root);
}