124 lines
3.7 KiB
C
124 lines
3.7 KiB
C
#include "display.h"
|
|
#include "common.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <locale.h>
|
|
|
|
/**
|
|
* Détermine la profondeur de l'arbre binaire
|
|
* à partir du sommet donné
|
|
*/
|
|
static int _maxDepth(HufVertex);
|
|
|
|
/**
|
|
* Affiche l'arborescence à partir du sommet donné,
|
|
* indenté sur le niveau donné
|
|
*/
|
|
static void _printVertex(HufVertex, wchar_t*, int);
|
|
|
|
/**
|
|
* Transforme le caractère donné en espace s'il est
|
|
* non-imprimable, ou le laisse tel quel sinon
|
|
*/
|
|
static char _safeChar(char input);
|
|
|
|
void printTree(HufTree tree) {
|
|
// Allocation d'un tampon suffisamment grand pour contenir
|
|
// les indentations des sommets les plus profonds de l'arbre
|
|
wchar_t* buffer = malloc(_maxDepth(*tree.root) * 4 * sizeof(*buffer));
|
|
_printVertex(*tree.root, buffer, 0);
|
|
free(buffer);
|
|
}
|
|
|
|
void _printVertex(HufVertex vert, wchar_t* buffer, int length) {
|
|
if (vert.child_l != NULL && vert.child_r != NULL) {
|
|
// Affichage d'un sommet avec enfants, on montre
|
|
// la probabilité somme des enfants
|
|
printf("■\n");
|
|
|
|
// Affichage du fils gauche. Augmentation du préfixe
|
|
// pour l'affichage des enfants du fils gauche
|
|
buffer[length] = '\0';
|
|
printf("%ls├── ", buffer);
|
|
|
|
buffer[length] = L'│';
|
|
buffer[length + 1] = ' ';
|
|
buffer[length + 2] = ' ';
|
|
buffer[length + 3] = ' ';
|
|
buffer[length + 4] = '\0';
|
|
_printVertex(*vert.child_l, buffer, length + 4);
|
|
|
|
// Affichage du fils droite. Augmentation du préfixe
|
|
// pour l'affichage des enfants du fils droit
|
|
buffer[length] = '\0';
|
|
printf("%ls└── ", buffer);
|
|
|
|
buffer[length] = ' ';
|
|
_printVertex(*vert.child_r, buffer, length + 4);
|
|
} else {
|
|
// Affichage d'une feuille de l'arbre, correspondant
|
|
// à un caractère du corpus
|
|
printf("%c\n", _safeChar(vert.character));
|
|
}
|
|
}
|
|
|
|
int _maxDepth(HufVertex vert) {
|
|
if (vert.child_l != NULL && vert.child_r != NULL) {
|
|
int left = _maxDepth(*vert.child_l);
|
|
int right = _maxDepth(*vert.child_r);
|
|
|
|
return (left > right ? left : right) + 1;
|
|
} else {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
void printFrequenciesTable(double* table, size_t size) {
|
|
printf("┌─┬────┬─────────┐\n");
|
|
printf("│C│code│fréquence│\n");
|
|
printf("├─┼────┼─────────┤\n");
|
|
|
|
double sum = 0;
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
if (table[i] != 0) {
|
|
printf("│%c│%4d│%9.4lf│\n", _safeChar(i), (int) i, table[i]);
|
|
sum += table[i];
|
|
}
|
|
}
|
|
|
|
printf("├─┴────┼─────────┤\n");
|
|
printf("│ Total│%9.4lf│\n", sum);
|
|
printf("└──────┴─────────┘\n");
|
|
}
|
|
|
|
void printLabelsTable(char** table, size_t size) {
|
|
printf("┌─┬────┬────────────────┐\n");
|
|
printf("│C│code│ étiquette│\n");
|
|
printf("├─┼────┼────────────────┤\n");
|
|
|
|
double total_length = 0;
|
|
size_t label_count = 0;
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
if (table[i] != 0) {
|
|
printf("│%c│%4d│%16s│\n", _safeChar(i), (int) i, table[i]);
|
|
|
|
total_length += strlen(table[i]);
|
|
label_count++;
|
|
}
|
|
}
|
|
|
|
printf("├─┴────┴───┬────────────┤\n");
|
|
printf("│Long. moy.│%12.4lf│\n", total_length / label_count);
|
|
printf("└──────────┴────────────┘\n");
|
|
}
|
|
|
|
char _safeChar(char input) {
|
|
if (input < 32 || input == 127) {
|
|
return ' ';
|
|
}
|
|
|
|
return input;
|
|
}
|