--- rpl/src/simplification.c 2019/11/04 10:05:25 1.69 +++ rpl/src/simplification.c 2019/11/05 15:37:51 1.70 @@ -49,7 +49,7 @@ affichage_arbre(struct_processus *s_etat if (niveau == 0) { - printf("Arbre $%016X\n", s_arbre); + printf("--- Arbre $%016X\n", s_arbre); } // Affichage de la feuille (fonction ou donnée générale s'il n'y @@ -87,7 +87,7 @@ affichage_arbre(struct_processus *s_etat if (niveau == 0) { - printf("---\n"); + printf("--- Fin de l'arbre\n"); } return; @@ -138,9 +138,31 @@ transcription_arbre(struct_processus *s_ } // Ajout des fonctions + // Arbre q-aire => si q branches, q-1 fonctions l_liste = (*s_arbre).feuille; + for(i = 0; i < (*s_arbre).nombre_branches - 2; i++) + { + if ((l_element_courant = allocation_maillon(s_etat_processus)) + == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return(NULL); + } + + (*l_element_courant).suivant = l_liste; + + if (((*l_element_courant).donnee = copie_objet(s_etat_processus, + (*l_liste).donnee, 'P')) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return(NULL); + } + + l_liste = l_element_courant; + } + free((*s_arbre).branches); free(s_arbre); @@ -185,10 +207,36 @@ transcription_arbre(struct_processus *s_ ================================================================================ */ -static void -inversion_fonctions_arbre(struct_arbre *s_arbre) +static int +ordonnancement_branches(const void *a1, const void *a2) { - return; + struct_arbre **_a1; + struct_arbre **_a2; + + _a1 = (struct_arbre **) a1; + _a2 = (struct_arbre **) a2; + + if (((**_a1).feuille != NULL) && ((**_a2).feuille != NULL)) + { + if ((*(*(**_a1).feuille).donnee).type == + (*(*(**_a2).feuille).donnee).type) + { + return(0); + } + + if (((*(*(**_a1).feuille).donnee).type == INT) || + ((*(*(**_a1).feuille).donnee).type == REL) || + ((*(*(**_a1).feuille).donnee).type == CPL)) + { + return(1); + } + else + { + return(-1); + } + } + + return(0); } @@ -197,6 +245,10 @@ simplification_arbre(struct_processus *s struct_arbre *s_arbre) { integer8 i; + integer8 j; + integer8 nouveaux_elements; + + struct_objet *s_objet; if ((*(*(*s_arbre).feuille).donnee).type != FCT) { @@ -206,20 +258,92 @@ simplification_arbre(struct_processus *s return; } - if ((strcmp((*((struct_fonction *) (*(*(*s_arbre).feuille).donnee).objet)) - .nom_fonction, "+") == 0) || (strcmp((*((struct_fonction *) - (*(*(*s_arbre).feuille).donnee).objet)).nom_fonction, "-") == 0)) + // La feuille est une fonction, on peut envisager la simplification + // de l'arbre. Pour cela, on descend d'un niveau pour greffer + // de nouvelles branches. + + if (strcmp((*((struct_fonction *) (*(*(*s_arbre).feuille).donnee).objet)) + .nom_fonction, "+") == 0) { for(i = 0; i < (*s_arbre).nombre_branches; i++) { + s_objet = (*((*((*s_arbre).branches[i])).feuille)).donnee; + + if ((*s_objet).type == FCT) + { + if (strcmp((*((struct_fonction *) (*s_objet).objet)) + .nom_fonction, "-") == 0) + { + } + + if (strcmp((*((struct_fonction *) (*s_objet).objet)) + .nom_fonction, "+") == 0) + { + simplification_arbre(s_etat_processus, + (*s_arbre).branches[i]); + + /* + On greffe. ++ + + + 2 + SIN + 3 + 10 + + doit donner : ++ + 2 + SIN + 3 + 10 + */ + + nouveaux_elements = (*(*s_arbre).branches[i]) + .nombre_branches; + + if (((*s_arbre).branches = realloc((*s_arbre).branches, + ((unsigned) ((*s_arbre).nombre_branches + + nouveaux_elements)) + * sizeof(struct_arbre *))) == NULL) + { + (*s_etat_processus).erreur_systeme = + d_es_allocation_memoire; + return; + } + + for(j = 0; j < nouveaux_elements; j++) + { + (*s_arbre).branches[(*s_arbre).nombre_branches++] = + (*(*s_arbre).branches[i]).branches[j]; + } + + free((*s_arbre).branches[i]); + + // Retrait de la branche + + for(j = i + 1; j < (*s_arbre).nombre_branches; j++) + { + (*s_arbre).branches[j - 1] = (*s_arbre).branches[j]; + } + + (*s_arbre).nombre_branches--; + + // Réorganisation des valeurs numériques en queue. + + qsort((*s_arbre).branches, (size_t) (*s_arbre) + .nombre_branches, sizeof(struct_arbre *), + ordonnancement_branches); + } + } } - } - else if ((strcmp((*((struct_fonction *) (*(*(*s_arbre).feuille).donnee) - .objet)).nom_fonction, "*") == 0) || (strcmp((*((struct_fonction *) - (*(*(*s_arbre).feuille).donnee).objet)).nom_fonction, "/") == 0)) - { - for(i = 0; i < (*s_arbre).nombre_branches; i++) + + if (((*s_arbre).branches = realloc((*s_arbre).branches, + ((unsigned) (*s_arbre).nombre_branches) + * sizeof(struct_arbre *))) == NULL) { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } } @@ -432,14 +556,15 @@ simplification(struct_processus *s_etat_ affichage_arbre(s_etat_processus, s_arbre, 0); -# ifdef EXPERIMENTAL_CODE +//# ifdef EXPERIMENTAL_CODE simplification_arbre(s_etat_processus, s_arbre); + affichage_arbre(s_etat_processus, s_arbre, 0); if ((*s_etat_processus).erreur_systeme != d_es) { return(NULL); } -# endif +//# endif /* * Transcription de l'arbre q-aire simplifié en expression algébrique.