Skip to content
Snippets Groups Projects
Commit 33e7d24d authored by Abdul-Malik Elmurzaev's avatar Abdul-Malik Elmurzaev
Browse files

Merge branch 'master' into 'main'

Master

See merge request !3
parents 3e3c414e 25fe2ce8
Branches
1 merge request!3Master
......@@ -27,44 +27,17 @@ bool est_lien_connaissance(rtype id){
return (id == EPOUX || id == AMI || id == CONNAIT || id == VIT);
}
typedef struct snoeud{
int type;
int val[3];
struct snoeuf* fils[4];
}arbre, *Arbre234;
typedef struct relations{
char* leurRelation[17];
}RelationsTable;
RelationsTable* newRelationsTable(){
RelationsTable* r = malloc(sizeof(RelationsTable));
r->leurRelation[0] = "NULL";
r->leurRelation[1] = "NULL";
return r;
}
RelationsTable* addRelationsTable(RelationsTable* r, char* name, int id){ // name of their relation
r->leurRelation[id] = name;
return r;
}
RelationsTable* completeTheRelationsTable(){
RelationsTable* r = newRelationsTable();
char* n2 = " frère ou soeur de "; char* n3 = "cousin ou cousine de "; char* n4 = "père ou mère ";
char* n5 = " oncle ou tante de "; char* n6 = "époux ou épouse de "; char* n7 = "ami de ";
char* n8 = " vit avec "; char* n9 = " connait "; char* n10 = " supérieur de ";
char* n11 = " collègue de "; char* n12 = " locataire de "; char* n13 = " travaille à ";
char* n14 = " propriétaire de "; char* n15 = " situé à "; char* n16 = " découvert à ";
char* table[15] = {n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16};
for(int i=0; i<15; i++){
r = addRelationsTable(r, table[i], i+2);
}
return r;
}
static char* RelationsTable[] = {
[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",
[11] = "collègue de", [12] = "locataire de", [13] = "travaille à",
[14] = "propriétaire de", [15] = "situé à", [16] = "découvert à"
};
// PRE CONDITION: id > 1
char* toStringRelation(rtype id){
RelationsTable* r = completeTheRelationsTable();
return r->leurRelation[id];
return RelationsTable[id];
}
////////////////////////////////////////
......@@ -80,12 +53,20 @@ listeg listegnouv(){
}
listeg adjtete(listeg lst, void *x){
listeg new = malloc(sizeof(struct s_node));
if (new == NULL) {
fprintf(stderr, " malloc == NULL fonction adjtete(lst,x) \n");
exit(EXIT_FAILURE);
}
new->val = x;
new->suiv = lst;
return new;
}
listeg adjqueue(listeg lst, void *x){
listeg new = malloc(sizeof(struct s_node));
if (new == NULL) {
fprintf(stderr, " malloc == NULL fonction adjqueue(lst,x) \n");
exit(EXIT_FAILURE);
}
new->val = x;
new->suiv = NULL;
if( lst != NULL ){
......@@ -94,10 +75,9 @@ listeg adjqueue(listeg lst, void *x){
copy = copy->suiv;
}
copy->suiv = new;
}else{
return lst;
}else
return new;
}
return lst;
}
listeg suptete(listeg lst){
listeg deuxieme = lst->suiv;
......@@ -126,15 +106,15 @@ bool estvide(listeg lst){
void detruire(listeg lst){
if( estvide(lst) )
return;
if(lst->suiv != NULL){
if(lst->suiv == NULL){
free(lst);
return;
}
listeg copy = lst;
while(copy != NULL){
listeg deathrowInmate = copy;
copy = copy->suiv;
free(lst);
lst = copy;
}
}
listeg rech(listeg lst, void *x, int(*comp)(void *, void *)){
......@@ -157,59 +137,155 @@ typedef enum{
PERSONNE=1, OBJET, ADRESSE, VILLE
}etype;
typedef struct s_entite{
char nom[LONG_NOM_MAX]; // le nom de lentit p.ex Peugeot 106
etype ident; // lidentifiant associ, p.ex OBJET
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 donnes
//3.1 les structures de donnees
typedef struct s_sommet{
// A DEFINIR
struct s_node* larcs;
Entite x;
}*Sommet;
typedef struct s_arc{
// A DEFINIR
rtype t;
struct s_entite* x;
}*Arc;
typedef struct s_relations{
// A DEFINIR
listeg listDeRelations;
}*Relations;
//3.2 les constructeurs
Entite creerEntite(char *s, etype e){
return NULL;
Entite newEntity = malloc(sizeof(struct s_entite));
if (newEntity == NULL) {
fprintf(stderr, " malloc == NULL fonction creerEntite(s,e) \n");
exit(EXIT_FAILURE);
}
strcpy(newEntity->nom, s);
newEntity->ident = e;
return newEntity;
}
Sommet nouvSommet(Entite e){
return NULL;
if(e == NULL){
fprintf(stderr, " nouvSommet(e) erreur e == NULL ici fonction nouvSommet(e) \n");
exit(EXIT_FAILURE);
}
Sommet s = malloc(sizeof(struct s_sommet));
if (s == NULL) {
fprintf(stderr, " malloc == NULL fonction nouvSommet(e) \n");
exit(EXIT_FAILURE);
}
s->x = e;
s->larcs = listegnouv();
return s;
}
Arc nouvArc(Entite e, rtype type){
return NULL;
if(e == NULL){
fprintf(stderr, " nouvSommet(e) erreur e == NULL ici fonction nouvArc(e,type) \n");
exit(EXIT_FAILURE);
}
Arc bow = malloc(sizeof(struct s_arc));
if (bow == NULL) {
fprintf(stderr, " malloc == NULL fonction nouvArc(e,type) \n");
exit(EXIT_FAILURE);
}
bow->t = type;
bow->x = e;
return bow;
}
void relationInit(Relations *g){
*g = malloc(sizeof(struct s_relations));
if(*g == NULL){
fprintf(stderr, " malloc == NULL function relationInit(*g) \n");
exit(EXIT_FAILURE);
}
(*g)->listDeRelations = listegnouv();
}
void relationFree(Relations *g){
// il faut free g, il faut aussi free g->listeDeRelations (qui est un listeg),
// 999 et il faut free g->listeDeRelations->val (qui est un sommet),
// 999 et il faut free g->listeDeRelations->val->x (qui est une entite),
// 999 et il faut free g->listeDeRelations->val->larcs (qui est un listeg),
// 999 et il faut free tous les g->listeDeRelations->val->larcs->val (qui sont des arcs),
// 999 et il faut free tous les g->listeDeRelations->val->larcs->val->x (qui sont des entites)
// commençont par tous les g->listeDeRelations->val->larcs->val->x et du val qui va avec
free( ((Sommet)((*g)->listDeRelations->val))->x );
listeg tete = (*g)->listDeRelations;
printf("check 0 \n");
while( tete != NULL ){
listeg LarcsDuSommet = ((Sommet)(tete->val))->larcs;
listeg LarcTete = LarcsDuSommet;
while( LarcsDuSommet != NULL ){
printf("check 1 \n");
free( ((Arc)( LarcsDuSommet->val ))->x );
printf("check 2 \n");
free( ((Arc)( LarcsDuSommet->val )) );
LarcsDuSommet = LarcsDuSommet->suiv;
}
printf("check 3 \n");
detruire(LarcTete);
printf("check 4 \n");
free( ((Sommet)(tete->val)) );
printf("check 5 \n");
tete = tete->suiv;
}
detruire( (*g)->listDeRelations );
free(*g);
}
//3.3 les comparaisons
int compEntite(void *e, void *string){
if (e == NULL || string == NULL) {
return 0;
}
if( strcmp( ((Entite)e)->nom, string ) == 0 ){
return 1;
}
return 0;
}
int compSommet(void *s, void *string){
if (s == NULL || string == NULL) {
return 0;
}
if( strcmp( ((Sommet)s)->x->nom, string ) == 0 ){
return 1;
}
return 0;
}
int compArc(void *a, void *string){
if (a == NULL || string == NULL) {
return 0;
}
if( strcmp( ((Arc)a)->x->nom, string ) == 0 ){
return 1;
}
return 0;
}
//3.4 ajout d'entites et de relations
void adjEntite(Relations g, char *nom, etype t){
g->listDeRelations = adjqueue(g->listDeRelations, nouvSommet(creerEntite(nom, t)));
}
// PRE CONDITION: id doit tre cohrent avec les types des sommets correspondants x et y
// PRE CONDITION: id doit etre coherent avec les types des sommets correspondants a x et y
// p.ex si x est de type OBJET, id ne peut pas etre une relation de parente
// PRE CONDITION: strcmp(nom1,nom2)!=0
void adjRelation(Relations g, char *nom1, char *nom2, rtype id){
Sommet sommet1 = NULL;
Sommet sommet2 = NULL;
Entite deuxiemeEntite = NULL;
listeg copy = g->listDeRelations;
while( copy != NULL ){
if( strcmp(((Sommet)(copy->val))->x->nom, nom1)==0 ){
sommet1 = (Sommet)(copy->val);
}
if( strcmp(((Sommet)(copy->val))->x->nom, nom2)==0 ){
sommet2 = (Sommet)(copy->val);
}
copy = copy->suiv;
}
sommet1->larcs = adjqueue(sommet1->larcs, (Arc)nouvArc(sommet2->x, id));
}
////////////////////////////////////////
......@@ -247,16 +323,22 @@ bool se_connaissent_peutetre(Relations g, char *x, char *y){
////////////////////////////////////////
// Exercice 5: Affichages
static char* etype_string[] = { [PERSONNE] = "personne", [OBJET] = "objet", [ADRESSE] = "adresse",
[VILLE] = "ville"};
void affichelg(listeg l, void(*aff)(void *)){
}
while( l != NULL ){
aff(l->val);
l = l->suiv;
}
}
void afficheEntite(void *x){
printf("%s :%s\n", ((Entite)x)->nom, etype_string[ ((Entite)x)->ident] );
}
void afficheArc(void *x){
printf(" --%s-->", toStringRelation(((Arc)x)->t));
afficheEntite( ((Arc)(x))->x );
}
////////////////////////////////////////
......@@ -295,9 +377,8 @@ int main(){
copy = copy->suiv;
}
printf("tete(liste) == %d \n", **(int**)tete(liste));
detruire(liste);
return 0;
detruire(liste);
Relations r; relationInit(&r);
// ajouter les entites de l'exemple
......@@ -320,7 +401,12 @@ int main(){
adjRelation(r, tabe[5], tabe[8], LOCATAIRE);
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);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment