#include #include #include #include #include #include "ec_tailles.h" #include "lire_chaine.h" static char *symboles[] = { "A_t", "EXIT", "Z1", "Z2", "__", "__au", "_exit", "_z2", "ar", "a_t", "au", "ex__it", "z_1", "z_2" }; static int comparer_classes(const void *e1, const void *e2) { char *p1 = *((char **) e1); char *p2 = *((char **) e2); while (true) { char c1; char c2; while (*p1 == '_') p1++; while (*p2 == '_') p2++; c1 = toupper(*p1); c2 = toupper(*p2); if (c1 != c2) return c1 - c2; if (c1 == '\0') /* et par conséquent c2 == '\0' */ return 0; p1++; p2++;; } } static int comparer_elements(const void *e1, const void *e2) { int c = comparer_classes(e1, e2); if (c != 0) return c; char *p1 = *((char **) e1); char *p2 = *((char **) e2); return strcmp(p1, p2); } static void trier(void) { qsort(symboles, EC_NOMBRE_D_ELEMENTS(symboles), sizeof symboles[0], comparer_elements); } static void chercher(void) { while (true) { char *cherche = lire_chaine("-> "); if (cherche == NULL) break; char **trouve = bsearch(&cherche, symboles, EC_NOMBRE_D_ELEMENTS(symboles), sizeof symboles[0], comparer_elements); if (trouve == NULL) { trouve = bsearch(&cherche, symboles, EC_NOMBRE_D_ELEMENTS(symboles), sizeof symboles[0], comparer_classes); if (trouve == NULL) { printf("Pas de symbole correspondant à \"%s\"\n", cherche); free(cherche); continue; } } printf("\"%s\" -> \"%s\"\n", cherche, *trouve); free(cherche); } printf("\n"); } int main(void) { trier(); for (unsigned int i = 0; i < EC_NOMBRE_D_ELEMENTS(symboles); i++) printf("%s ", symboles[i]); printf("\n\n"); chercher(); return EXIT_SUCCESS; }