void tri_insertion(int nb_All, t_VectAllumettes Allumettes){ int i,j; int aux_couleur_i, aux_couleur_j; for(i = 0 ; i < nb_All ; i++){ j = 0; while(j < i && Allumettes[j].taille <= Allumettes[i].taille){ j++; } aux_couleur_i = Allumettes[i].couleur; aux_couleur_j = Allumettes[j].couleur; Allumettes[i].couleur = BLANC; Allumettes[j].couleur = BLANC; affiche(Allumettes,nb_All); Allumettes[i].couleur = aux_couleur_i; Allumettes[j].couleur = aux_couleur_j; inserer(Allumettes,i,j); } return; }
// OK // Probleme avec le return temp ajoute un '%' à la fin. // Ce n'est pas un problème d'affichage. // Si l'on fait un simple return arbre2 dans la fonction on se retrouve aussi avec ce '%'. Problème non corrigé. Arbre* fusion(Arbre* arbre1, Arbre* arbre2) { Arbre * temp = arbre2; if(arbre2 != NULL && arbre1 != NULL) { // On recherche pour voir si la valeur arbre 1 est dans l'arbre 2 if (chercher(arbre2,arbre1->valeur) == NULL) { // Dans le cas non on l'insère inserer(arbre2, arbre1->valeur); } // On fait de même pour l'arbre gauche et droite arbre2 = fusion(arbre1->gauche, arbre2); arbre2 = fusion(arbre1->droit, arbre2); temp = arbre2; } else { if (arbre2 == NULL ) { temp = arbre1; } else if (arbre1 == NULL) { temp = arbre2; } } return temp; }
//Inserer valeur arbre binaire de recherche (ABR) void inserer (arbre *a, int val) { if (*a==NULL) { *a = malloc (sizeof (struct noeud)); (*a)->val=val; (*a)->gauche=NULL; (*a)->droit=NULL; } else { if (val <= (*a)->val) inserer (&(*a)->gauche,val); else inserer (&(*a)->droit,val); } }
//OK struct Noeud* inserer(Arbre* noeud, int valeur) { if (noeud == NULL) { return(creer(valeur)); } else { // On rajoute la valeur à gauche si la valeur est inférieur if (valeur < noeud->valeur) { noeud->gauche = inserer(noeud->gauche, valeur); } // On rajoute la valeur à droite si la valeur est supérieur else { noeud->droit = inserer(noeud->droit, valeur); } return(noeud); } }
int main() { Listed liste = NULL; inserer(&liste,9); afficher(liste); inserer(&liste,5); afficher(liste); inserer(&liste,14); afficher(liste); inserer(&liste,12); inserer(&liste,2); afficher(liste); //supprimer(&liste,12); afficher(liste); //supprimer(&liste,5); afficher(liste); //supprimer(&liste,14); afficher(liste); return 1; }
int main() { int k; Liste_proc processus = NULL; // Créer un premier processus inserer(99, PRET, &processus); // Créer 10 processus dans la liste for(k = 0; k < 10; k++){ inserer(k, PRET, &processus); } // Afficher la liste de processus afficher(processus); // Modifier l'état des processus 1, 4, 7 modifierEtat(1, SUSPENDU, processus); modifierEtat(4, SUSPENDU, processus); modifierEtat(7, SUSPENDU, processus); // Afficher à nouveau la liste de processus afficher(processus); // Afficher le pid du premier processus printf("Le processus en tête a pour pid %d\n\n", lireNumTete(processus)); // Supprimer le processus 5 supprimer(5, &processus); // Dire si les processus 99, 5, 7, 13 appartiennent aux processus listés printf("%d : 99 est dans la liste des processus (1=true, 0=false)\n\n", appartient(99, processus)); printf("%d : 5 est dans la liste des processus (1=true, 0=false)\n\n", appartient(5, processus)); printf("%d : 7 est dans la liste des processus (1=true, 0=false)\n\n", appartient(7, processus)); printf("%d : 13 est dans la liste des processus (1=true, 0=false)\n\n", appartient(13, processus)); return 0; }
t_graphe *parcours_profondeur(t_graphe* graphe, char *n_s) { int *visite; int *names; t_pile *pile; int s; int u; int v; int i; int succ; int size; t_ch_int *tmp; t_ch_ch_int *ttmp; t_graphe *T; void *data; u = 0; v = 0; data = 0; succ = 0; size = taille(graphe); visite = xmalloc(sizeof(*visite)*size); names = xmalloc(sizeof(*names)*size); pile = xmalloc(sizeof(*pile)); pile->pile = xmalloc(sizeof(*pile->pile)*size); pile->taille_pile = 0; T = init(GO); empiler(name_to_number(graphe, n_s), pile); for (i = 0, ttmp = graphe->graphe; i < size; i++, ttmp = ttmp->next) names[i] = ttmp->number; inserer(get_index(s, names, size), visite); while (pile->taille_pile != 0) { u = sommet(pile); succ = 0; for (ttmp = graphe->graphe; ttmp != NULL; ttmp = ttmp->next) if (ttmp->number == u) break; for (tmp = ttmp->liste; tmp != NULL; tmp = tmp->next) if (!visite[get_index(tmp->number, names, size)]) succ++; if (succ != 0) { for (ttmp = graphe->graphe; ttmp != NULL; ttmp = ttmp->next) if (ttmp->number == u) break; for (tmp = ttmp->liste; tmp != NULL; tmp = tmp->next) if (!visite[get_index(tmp->number, names, size)]) { v = tmp->number; data = tmp->data; break; } ajouterArc(T, number_to_name(graphe, u), number_to_name(graphe, v), data); empiler(v,pile); inserer(get_index(v, names, size), visite); } else depiler(pile); } return (T); }
int main() { arbre a,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8,tmp9; tmp1 = (arbre)malloc(sizeof(struct noeud)); tmp1->val = 1; tmp1->gauche = NULL; tmp1->droit = NULL; tmp2 = (arbre)malloc(sizeof(struct noeud)); tmp2->val = 3; tmp2->gauche = NULL; tmp2->droit = NULL; tmp3 = (arbre)malloc(sizeof(struct noeud)); tmp3->val = 7; tmp3->gauche = NULL; tmp3->droit = NULL; tmp4 = (arbre)malloc(sizeof(struct noeud)); tmp4->val = 9; tmp4->gauche = NULL; tmp4->droit = NULL; tmp5 = (arbre)malloc(sizeof(struct noeud)); tmp5->val = 2; tmp5->gauche = tmp1; tmp5->droit = tmp2; tmp6 = (arbre)malloc(sizeof(struct noeud)); tmp6->val = 6; tmp6->gauche = NULL; tmp6->droit = tmp3; tmp7 = (arbre)malloc(sizeof(struct noeud)); tmp7->val = 10; tmp7->gauche = tmp4; tmp7->droit = NULL; tmp8 = (arbre)malloc(sizeof(struct noeud)); tmp8->val = 4; tmp8->gauche = tmp5; tmp8->droit = NULL; tmp9 = (arbre)malloc(sizeof(struct noeud)); tmp9->val = 8; tmp9->gauche = tmp6; tmp9->droit = tmp7; a = (arbre)malloc(sizeof(struct noeud)); a->val = 5; a->gauche = tmp8; a->droit = tmp9; printf("La hauteur de 10 est %d\n",hauteur(a,10)); printf("Taille de l'arbre a = %d\n",taille(a)); printf("Affiche prefixe : "); affiche_prefixe (a); inserer (&a,1); printf("\n"); printf("Affiche infixe : "); affiche_infixe (a); inserer (&a,11); printf("\n"); printf("Affiche suffixe : "); affiche_suffixe (a); printf("\n"); printf ("Recherche de 2 = %d\n",recherche(a,2)); printf ("Recherche de 12 = %d\n",recherche(a,12)); printf ("Recherche de 2 = %d\n",recherche(a,5)); printf ("Recherche de 12 = %d\n",recherche(a,0)); detruit(&a); printf("Affiche suffixe : "); affiche_suffixe (a); printf("\n"); return 0; }
/** * @brief Résout un puzzle * @param[in,out] p le puzzle à résoudre * @param[in] t le damier initial * @param[in,out] os flux de sortie */ void jouer(Puzzle& p, const Tab2D& t, std::ostream& os) { Etat etatInitial; Etat etatCourant; Tab2D damierFinal; Etat etatDerive; double tempsDebutRecherche = getTime(); but(damierFinal, t.nbL, t.nbC); initialiser(etatInitial.damier, t.nbL, t.nbC); etatInitial.mouvement = FIXE; etatInitial.precedent = 0; etatInitial.g = 0; //Copie du damier inititial dans etatInitial for (unsigned int l = 0; l < t.nbL; ++l) { for (unsigned int c = 0; c < t.nbC; ++c) { etatInitial.damier.tab[l][c] = t.tab[l][c]; } } etatInitial.h = manhattan(etatInitial.damier, damierFinal); initialiser(etatDerive.damier, t.nbL, t.nbC); inserer(p.lEAE, 0, etatInitial); //étatInitial dans LEAE bool solutionTrouvee = false; bool mvtPossible; unsigned int pos; while (p.lEAE.nb != 0) { pos = minimal(p.lEAE); etatCourant = lire(p.lEAE, pos); //on prend le 1er état à explorer //insérer étatCourant dans LEE inserer(p.lEE, longueur(p.lEE), etatCourant); supprimer(p.lEAE, pos); //supprimer étatCourant de LEAE if (etatCourant.h == 0) { // le damier de étatCourant est le damier but solutionTrouvee = true; break; //sortir de la boucle while } /*pour_tout (mouvement possible associé à étatCourant) mouvement possible relatif à damier de étatCourant (etatCourant.damier) ordre d'exploration (obligatoire) NORD, EST, SUD, OUEST */ //initialiser un étatDérivé // d'après le mouvement for(int m = OUEST; m >= NORD; --m) { mvtPossible = deriver(etatCourant, (Mouvement) m, etatDerive); if (mvtPossible && !rechercher(etatDerive, p.lEAE)\ && !rechercher(etatDerive, p.lEE)) { etatDerive.precedent = longueur(p.lEE) - 1; etatDerive.h = manhattan(etatDerive.damier, damierFinal); etatDerive.g = etatCourant.g + 1; //insérer étatDérivé dans LEAE inserer(p.lEAE, longueur(p.lEAE), etatDerive); } } } double tempsFinRecherche = getTime(); cout << "Durée de recherche : " << tempsFinRecherche - tempsDebutRecherche <<" seconde(s)."<< endl; if (solutionTrouvee) { Pile sol; Etat etatSol; initialiser(sol, 3, 2); initialiser(etatSol.damier, t.nbL, t.nbC); //Stockage de la solution etatSol = lire(p.lEE, longueur(p.lEE)-1); empiler(sol, etatSol); while (etatSol.precedent != 0) { etatSol = lire(p.lEE, etatSol.precedent); empiler(sol, etatSol); } empiler(sol, etatInitial); //Affichage de la solution os << "Damier : " << t.nbL << " lignes " << t.nbC << " colonnes" << endl; os << "Solution en " << sol.sommet << " mouvements" << endl; while (!estVide(sol)) { afficher(sommet(sol), os); os << endl; depiler(sol); } detruire(sol); detruire(etatSol.damier); } else { os << "Solution non trouvée" << endl; } detruire(etatInitial.damier); detruire(etatCourant.damier); detruire(etatDerive.damier); detruire(damierFinal); }
arbre inserer(objet p) { objet element = p, finpar = NULL; arbre rac = NULL, temp = NULL; int nbPar = 1, jetonPrec = -1; erreur = -1; // TANT QU'ON N'EST PAS ARRIVE A LA FIN while (element->jeton.lexem != FIN) { switch(element->jeton.lexem) { case OPERATEUR : // ON CHECKE LE PRECEDENT if(jetonPrec == -1 || ((jetonPrec == REEL) || (jetonPrec == PAR_FERM) || (jetonPrec == VARIABLE) || (jetonPrec == FIN))) { if(rac != NULL) { temp = rac; rac = NULL; } rac = (arbre) malloc(sizeof(feuille)); rac->jeton.lexem = OPERATEUR; rac->jeton.valeur.operateur = element->jeton.valeur.operateur; rac->fils_d = NULL; if(temp != NULL) { rac->fils_g = temp; temp = NULL; } } else { switch(jetonPrec) { case OPERATEUR : //DOUBLE OPERATEUR erreur = 10; break; case FONCTION : //FONCTION SUIVI D'UN OPERATEUR erreur = 11; break; case PAR_OUV : //PARENTHÈSE SUIVIE D'UN OPERATEUR erreur = 12; break; default: erreur = 100; break; } } break; case FONCTION : // ON CHECKE LE PRECEDENT if(jetonPrec == -1 || ((jetonPrec == OPERATEUR) || (jetonPrec == PAR_OUV) || (jetonPrec == FIN))) { rac = (arbre) malloc(sizeof(feuille)); rac->jeton.lexem = FONCTION; rac->jeton.valeur.fonction = element->jeton.valeur.fonction; rac->fils_g = NULL; rac->fils_d = NULL; // DÉTERMINATION DE LA PARENTHESE FERMANTE CORRESPONDANTE finpar = element->suiv->suiv; while ((nbPar != 0) && (finpar->jeton.lexem != FIN)){ if(finpar->jeton.lexem == PAR_OUV){ nbPar = nbPar + 1 ; } if(finpar->jeton.lexem == PAR_FERM){ nbPar = nbPar - 1 ; } if (nbPar != 0){ finpar = finpar->suiv ; } } if(nbPar != 0) { //NOMBRE DE PARENTHESES INCORRECTES erreur = 101; break; } else { nbPar++; finpar->jeton.lexem = FIN; //PASSAGE DU SOUS-ARBRE AU FILS GAUCHE rac->fils_g = inserer(element->suiv->suiv); element = finpar; } } else { switch(jetonPrec) { case REEL : //REEL SUIVI PAR UNE FONCTION erreur = 20; break; case FONCTION : //FONCTION SUIVIE d'UNE FONCTION erreur = 21; break; case PAR_FERM : //PARENTHÈSE FERMANTE SUIVIE D'UNE FONCTION erreur = 22; break; case VARIABLE : //VARIABLE SUIVIE D'UNE FONCTION erreur = 23; break; default: erreur = 100; break; } } break; case PAR_OUV : // ON CHECKE LE PRECEDENT if(jetonPrec == -1 || ((jetonPrec == OPERATEUR) || (jetonPrec == FONCTION) || (jetonPrec == PAR_OUV) || (jetonPrec == FIN))) { // DÉTERMINATION DE LA PARENTHESE FERMANTE CORRESPONDANTE finpar = element->suiv; while ((nbPar != 0) && (finpar->jeton.lexem != FIN)){ if(finpar->jeton.lexem == PAR_OUV){ nbPar = nbPar + 1 ; } if(finpar->jeton.lexem == PAR_FERM){ nbPar = nbPar - 1 ; } if (nbPar != 0){ finpar = finpar->suiv ; } } if(nbPar != 0) { //NOMBRE DE PARENTHESES INCORRECTES erreur = 101; break; } else { nbPar++; finpar->jeton.lexem = FIN; if(rac == NULL) { rac = inserer(element->suiv); } else { rac->fils_d = inserer(element->suiv); } element = finpar; } } else { switch(jetonPrec) { case REEL : //REEL SUIVI PAR UNE PARENTHÈSE erreur = 30; break; case PAR_FERM : //PARENTHESE FERMANTE SUIVIE PAR UNE PARENTHESE OUVRANTE erreur = 31; break; case VARIABLE : //VARIABLE SUIVIE D'UNE PARENTHESE OUVRANTE erreur = 32; break; default: erreur = 100; break; } } break; case FIN : // ON CHECKE LE PRECEDENT switch(jetonPrec) { case OPERATEUR : //OPERATEUR SUIVI DE LA FIN DE L'EXPRESSION erreur = 40; break; case FONCTION : //FONCTION SUIVIE DE LA FIN DE L'EXPRESSION erreur = 41; break; case PAR_OUV : //PARENTHÈSE OUVRANTE SUIVIE DE LA FIN DE L'EXPRESSION erreur = 42; break; default: erreur = 0; break; } break; case PAR_FERM : //NOMBRE DE PARENTHESES INCORRECTES erreur = 101; break; case REEL : // ON CHECKE LE PRECEDENT if(jetonPrec == -1 || ((jetonPrec == OPERATEUR) || (jetonPrec == PAR_OUV) || (jetonPrec == FIN))) { if(rac != NULL) { temp = rac; rac = NULL; } rac = (arbre) malloc(sizeof(feuille)); rac->jeton.lexem = REEL; rac->jeton.valeur.reel = element->jeton.valeur.reel; rac->fils_g = NULL; rac->fils_d = NULL; if(temp != NULL) { temp->fils_d = rac; rac = temp; temp = NULL; } } else { switch(jetonPrec) { case REEL : //DEUX REELS QUI SE SUIVENT erreur = 50; break; case FONCTION: //FONCTION SUIVI D'UN REEL erreur = 51; break; case PAR_FERM : //PARENTHESE FERMANTE SUIVIE D'UN REEL erreur = 52; break; case VARIABLE : //REEL SUIVIE D'UNE VARIABLE erreur = 53; break; default: erreur = 100; break; } } break; case VARIABLE : // ON CHECKE LE PRECEDENT if(jetonPrec == -1 || ((jetonPrec == OPERATEUR) || (jetonPrec == PAR_OUV) || (jetonPrec == FIN))) { if(rac != NULL) { temp = rac; rac = NULL; } rac = (arbre) malloc(sizeof(feuille)); rac->jeton.lexem = VARIABLE; rac->fils_g = NULL; rac->fils_d = NULL; if(temp != NULL) { temp->fils_d = rac; rac = temp; temp = NULL; } } else { switch(jetonPrec) { case REEL : //VARIABLE SUIVIE D'UN REEL erreur = 60; break; case FONCTION: //FONCTION SUIVI D'UNE VARIABLE erreur = 61; break; case PAR_FERM : //PARENTHESE FERMANTE SUIVIE D'UNE VARIABLE erreur = 62; break; case VARIABLE : //DEUX VARIABLE QUI SE SUIVENT erreur = 63; break; default: erreur = 100; break; } } break; } if(erreur != -1) { element->jeton.lexem = FIN; } else { jetonPrec = element->jeton.lexem; element = element->suiv; } } if(erreur == -1) { return rac; } else { return NULL; } }