diff --git a/relations.c b/relations.c index 223ed919b7486e799874b99749bbcd6370bb1425..f8dabd6a58dd609bf5b92e3aa007b9de3ff54957 100644 --- a/relations.c +++ b/relations.c @@ -28,6 +28,7 @@ bool est_lien_connaissance(rtype id){ } static char* RelationsTable[] = { + [0] = "omega", [1] = "inconnue", [2] = "frère ou soeur de", [3] = "cousin ou cousine de", [4] = "père ou mère", [5] = "oncle ou tante de", [6] = "époux ou épouse de", [7] = "ami de", [8] = "vit avec", [9] = "connait", [10] = "supérieur de", @@ -47,6 +48,28 @@ typedef struct s_node{ void *val; // pointeur vers objet quelconque struct s_node *suiv; }*listeg; +#define LONG_NOM_MAX 64 +typedef enum{ + PERSONNE=1, OBJET, ADRESSE, VILLE +}etype; +typedef struct s_entite{ + char nom[LONG_NOM_MAX]; // le nom de l entite p.ex " Peugeot 106 " + etype ident; // l identifiant associe, p.ex OBJET +}*Entite; +//3.1 les structures de donnees +typedef struct s_sommet{ + struct s_node* larcs; + Entite x; +}*Sommet; + +typedef struct s_arc{ + rtype t; + struct s_entite* x; +}*Arc; + +typedef struct s_relations{ + listeg listDeRelations; +}*Relations; listeg listegnouv(){ return NULL; @@ -116,22 +139,24 @@ void detruire(listeg lst){ free(lst); lst = copy; } -} +}// listeg rech(listeg lst, void *x, int(*comp)(void *, void *)){ - listeg copy = lst; - while(copy != NULL){ - if( comp(x, copy->val) ){ - return lst; - }else{ - copy = copy->suiv; + listeg resultat = listegnouv(); + int i=0; + while(lst != NULL){ + if( comp(lst->val, x)==0 ){ + resultat = adjqueue(resultat, lst->val); + i++; } + lst = lst->suiv; } - return NULL; + printf(" %d \n", i); + return resultat; } //////////////////////////////////////// // Exercice 3: Construction du graphe - +/* #define LONG_NOM_MAX 64 typedef enum{ PERSONNE=1, OBJET, ADRESSE, VILLE @@ -154,7 +179,7 @@ typedef struct s_arc{ typedef struct s_relations{ listeg listDeRelations; }*Relations; - +*/ //3.2 les constructeurs Entite creerEntite(char *s, etype e){ Entite newEntity = malloc(sizeof(struct s_entite)); @@ -265,6 +290,14 @@ int compArc(void *a, void *string){ //3.4 ajout d'entites et de relations void adjEntite(Relations g, char *nom, etype t){ + listeg copy = g->listDeRelations; + while(copy != NULL){ + if( ((Sommet)(copy->val))->x->nom == nom ){ + printf("check \n"); + return; + } + copy = copy->suiv; + } g->listDeRelations = adjqueue(g->listDeRelations, nouvSommet(creerEntite(nom, t))); } // PRE CONDITION: id doit etre coherent avec les types des sommets correspondants a x et y @@ -284,7 +317,16 @@ void adjRelation(Relations g, char *nom1, char *nom2, rtype id){ } copy = copy->suiv; } + listeg copyLarcs = ((Sommet)(g->listDeRelations->val))->larcs; + while( copyLarcs != NULL ){ + if( ((Arc)(copyLarcs))->x->nom == nom2 ){ + ((Arc)(copyLarcs))->t = id; + return; + } + copyLarcs = copyLarcs->suiv; + } sommet1->larcs = adjqueue(sommet1->larcs, (Arc)nouvArc(creerEntite(sommet2->x->nom, sommet2->x->ident), id)); + sommet2->larcs = adjqueue(sommet2->larcs, (Arc)nouvArc(creerEntite(sommet1->x->nom, sommet1->x->ident), id)); } //////////////////////////////////////// @@ -292,10 +334,54 @@ void adjRelation(Relations g, char *nom1, char *nom2, rtype id){ // 4.1 listes de relations listeg en_relation(Relations g, char *x){ - return NULL; + listeg res = listegnouv(); + listeg copyTete = g->listDeRelations; + while(copyTete != NULL){ + if( strcmp( ((Sommet)(copyTete->val))->x->nom, x ) == 0 ){ + return ((Sommet)(copyTete->val))->larcs; + } + copyTete = copyTete->suiv; + } + return res; } -listeg chemin2(Relations g, char *x, char *y){ - return NULL; + +listeg chemin2(Relations g, char *x, char *y){ // on renvoit une LISTE D ENTITES + listeg LarcsOfX = en_relation(g, x); + listeg LarcsOfY = en_relation(g, y); + + listeg XEntites = listegnouv(); // on va stocker toutes les entites en relation avec x et y + listeg YEntites = listegnouv(); + listeg RES = listegnouv(); // puis mettres dans RES les entités en commun sauf x et y + listeg LarcsOfX_copy = LarcsOfX; + listeg LarcsOfY_copy = LarcsOfY; + + while(LarcsOfX_copy != NULL){ + XEntites = adjqueue(XEntites, ((Arc)(LarcsOfX_copy->val))->x ); + LarcsOfX_copy = LarcsOfX_copy->suiv; + } + while(LarcsOfY_copy != NULL){ + YEntites = adjqueue(YEntites, ((Arc)(LarcsOfY_copy->val))->x ); + LarcsOfY_copy = LarcsOfY_copy->suiv; + } + listeg XEntites_copy = XEntites; + listeg YEntites_copy = YEntites; + + while(XEntites_copy != NULL){ + YEntites_copy = YEntites; + while(YEntites_copy != NULL){ + //printf("a \n"); + if( compEntite(XEntites_copy->val, ((Entite)(YEntites_copy->val))->nom) ){ + RES = adjqueue(RES, XEntites_copy->val ); + printf("check \n"); + //printf(" ___________ %s \n", ((Entite)(RES->val))->nom ); + } + YEntites_copy = YEntites_copy->suiv; + } + XEntites_copy = XEntites_copy->suiv; + } + detruire(XEntites); + detruire(YEntites); + return RES; } // 4.2 verifier un lien de parente // PRE CONDITION: strcmp(x,y)!=0 @@ -307,8 +393,9 @@ bool ont_lien_parente(Relations g, char *x, char *y){ // PRE CONDITION: les sommets correspondants � x et y sont de type PERSONNE // PRE CONDITION: strcmp(x,y)!=0 bool se_connaissent(Relations g, char *x, char *y){ - return false; + return 0; } + // PRE CONDITION: les sommets correspondants � x et y sont de type PERSONNE // PRE CONDITION: strcmp(x,y)!=0 bool se_connaissent_proba(Relations g, char *x, char *y){ @@ -350,35 +437,6 @@ void affiche_degre_relations(Relations r, char *x){ int main(){ int i,j; - printf("test toStringRelation(16) == %s \n", toStringRelation(16)); - printf("test est_lien_parente : %d %d %d \n",est_lien_parente(COUSIN),est_lien_parente(LOCATAIRE), - est_lien_parente(EPOUX)); - printf("test est_lien_professionel : %d %d %d \n", est_lien_professionel(LOCATAIRE), - est_lien_professionel(CHEF), est_lien_professionel(COLLEGUE)); - printf("test est_lien_connaissance : %d %d %d \n", est_lien_connaissance(AMI), - est_lien_connaissance(CONNAIT), est_lien_connaissance(COLLEGUE)); - - listeg liste = listegnouv(); - int x5 = 5, x4 = 4, x19 = 19, x3 = 3, x33 = 33; - int *px5, *px4, *px19, *px3, *px33; - px5 = &x5; px4 = &x4; px19 = &x19; px3 = &x3; px33 = &x33; - int x0 = 0; int* px0 = &x0; - liste = adjqueue(liste, &px5); - liste = adjtete(liste, &px4); - liste = adjqueue(liste, &px19); - liste = adjtete(liste, &px3); - liste = adjqueue(liste, &px33); - liste = adjtete(liste, &px0); - liste = suptete(liste); - listeg copy = liste; - for(int i=0; i<4; i++){ - printf("liste->val == %d \n", **(int**)copy->val); - copy = copy->suiv; - } - printf("tete(liste) == %d \n", **(int**)tete(liste)); - - detruire(liste); - Relations r; relationInit(&r); // ajouter les entites de l'exemple char *tabe[] ={"KARL","LUDOVIC","CELINE","CHLOE","GILDAS","CEDRIC","SEVERINE", @@ -401,21 +459,11 @@ int main(){ adjRelation(r, tabe[7], tabe[8], DECOUVERT); adjRelation(r, tabe[8], tabe[9], SITUE); - affichelg( ((Sommet)(r->listDeRelations->val))->larcs, afficheArc ); - printf("\n %s \n", ((Arc)(((Sommet)(r->listDeRelations->val))->larcs->val))->x->nom ); - relationFree(&r); - return 0;////// - - - - // explorer les relations printf("%s est en relation avec:\n", tabe[0]); affichelg(en_relation(r, tabe[0]),afficheArc); printf("\n"); - - for (i = 0; i < 7; i++) for (j = i + 1; j < 10; j++) - { + for (i = 0; i < 7; i++) for (j = i + 1; j < 10; j++){ printf("<%s> et <%s> ont les relations communes:\n", tabe[i], tabe[j]); listeg ch = chemin2(r, tabe[i], tabe[j]); affichelg(ch, afficheEntite); @@ -423,7 +471,8 @@ int main(){ detruire(ch); } printf("\n\n"); - + relationFree(&r); + return 0; ///// for (i = 0; i < 10; i++) for (j = i + 1; j < 10; j++) { printf("<%s> et <%s> ont lien de parente: %s\n",