Skip to content
Snippets Groups Projects
Commit 98336f2f authored by poulpe's avatar poulpe
Browse files

[Update] Fix error with \n for Z char

parent 207cfcb5
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
int main(int argc, char** argv) { int main(int argc, char** argv) {
arbre tree = arbre_build("code-morse.txt"); arbre tree = arbre_build("code-morse.txt");
// printf("%c\n",decoder("--..",tree));
decoder_fichier("text.morse",tree);
exit(0); exit(0);
// print(tree,1); // impression de l'arbre // print(tree,1); // impression de l'arbre
// if (2 == argc) { // if (2 == argc) {
......
...@@ -23,7 +23,7 @@ char decoder(char *code, arbre tree) ...@@ -23,7 +23,7 @@ char decoder(char *code, arbre tree)
uint32_t len = strlen(code); uint32_t len = strlen(code);
for (uint32_t i = 0; i < len; i += 1) for (uint32_t i = 0; i < len; i += 1)
{ {
tmp = tmp->child[(code[i] == '.') ? 0 : 1]; tmp = tmp->child[(code[i] == '-') ? 1 : 0];
} }
return tmp->lettre; return tmp->lettre;
} }
...@@ -37,17 +37,30 @@ void decoder_fichier(char *filename, arbre tree) ...@@ -37,17 +37,30 @@ void decoder_fichier(char *filename, arbre tree)
// lecture d'un mot de caract�res dans 'A'..'Z' // lecture d'un mot de caract�res dans 'A'..'Z'
FILE *fp = fopen(filename, "r"); FILE *fp = fopen(filename, "r");
assert(NULL != fp); assert(NULL != fp);
int lg = 0; // char* res = calloc(1024, sizeof(char));
uint32_t index = 0;
char code[6]; char code[6];
printf("Text : \n");
while (!feof(fp)) while (!feof(fp))
{ {
char ch = (char)fgetc(fp); char ch = (char)fgetc(fp);
switch (ch) if(ch == ' ')
{ {
//� compl�ter printf("%c",decoder(code,tree));
memset(code,0,6);
index = 0;
}else if(ch == '/')
{
printf(" ");
}
else if(ch == '-' || ch == '.')
{
code[index] = ch;
index += 1;
} }
} }
fclose(fp); fclose(fp);
printf("\n\n");
} }
//----------------------------------------------------------- //-----------------------------------------------------------
...@@ -79,8 +92,23 @@ void encoder_fichier(char *filename, int n, int m, char alphabet_morse[n][m]) ...@@ -79,8 +92,23 @@ void encoder_fichier(char *filename, int n, int m, char alphabet_morse[n][m])
void placer(char *chaine, arbre *tree) void placer(char *chaine, arbre *tree)
{ {
arbre tmp = *tree; arbre tmp = *tree;
int length = strlen(chaine); uint32_t length = strlen(chaine);
// � compl�ter
for (uint32_t i = 1; i < length-1; i += 1)
{
uint8_t pos = (chaine[i] == '-') ? 1 : 0;
if (tmp->child[pos] == NULL)
{
tmp->child[pos] = calloc(1, sizeof(node));
tmp = tmp->child[pos];
tmp->lettre = '?';
}
else
{
tmp = tmp->child[pos];
}
}
tmp->lettre = chaine[0]; tmp->lettre = chaine[0];
} }
...@@ -96,6 +124,7 @@ void table_build(char *filename, int n, int m, char alphabet_morse[n][m]) ...@@ -96,6 +124,7 @@ void table_build(char *filename, int n, int m, char alphabet_morse[n][m])
fclose(fp); fclose(fp);
} }
//---------------------------------------------------------- //----------------------------------------------------------
// procedure lisant le fichier code-morse.txt et cr�ant -- // procedure lisant le fichier code-morse.txt et cr�ant --
// l'arbre binaire correspondant -- // l'arbre binaire correspondant --
...@@ -109,15 +138,20 @@ arbre arbre_build(char *filename) ...@@ -109,15 +138,20 @@ arbre arbre_build(char *filename)
assert(NULL != fp); assert(NULL != fp);
char line[100]; char line[100];
uint32_t line_number = 0;
while (!feof(fp)) while (!feof(fp))
{ {
arbre tmp_ptr = tree; arbre tmp_ptr = tree;
line_number += 1;
fgets(line, 100, fp); fgets(line, 100, fp);
char cur_char = line[0]; char cur_char = line[0];
char *morse = calloc(strlen(line), sizeof(char)); char *morse = calloc(strlen(line), sizeof(char));
sprintf(morse, "%s", line + 1); sprintf(morse, "%s", line + 1);
printf("%c : %s : %d\n", cur_char, morse,strlen(morse)); if (morse[strlen(morse)-1] == '\n')
for(uint32_t i=0;i<strlen(morse)-1;i+=1) {
morse[strlen(morse)-1] = '\0';
}
for (uint32_t i = 0; i < strlen(morse); i += 1) // strlen() - 1 for decrement '\n' char
{ {
uint8_t pos = (morse[i] == '-') ? 1 : 0; uint8_t pos = (morse[i] == '-') ? 1 : 0;
if (tmp_ptr->child[pos] == NULL) if (tmp_ptr->child[pos] == NULL)
...@@ -131,7 +165,6 @@ arbre arbre_build(char *filename) ...@@ -131,7 +165,6 @@ arbre arbre_build(char *filename)
tmp_ptr = tmp_ptr->child[pos]; tmp_ptr = tmp_ptr->child[pos];
} }
} }
printf("Save value : '%c' in addr : %p\n",cur_char,tmp_ptr->lettre);
tmp_ptr->lettre = cur_char; tmp_ptr->lettre = cur_char;
free(morse); free(morse);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment