1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char* nom;
unsigned int age;
unsigned int number;
char* adresse;
Personne** amis;
}Personne;
/*Prototypes*/
/*Au début une personne n'a pas d'amis, son nom ne change pas mais son adresse peut*/
void naissance(Personne** quidam,char* nom, unsigned int age, char* adresse);
/*Crée un lien d'amitié symétrique */
void sont_amis(Personne** a,Personne** b);
/*prend deux personnes en paramètres et retourne le nombre damis communs*/
int affinites(Personne* a,Personne* b);
void demenage(Personne** a, char* adresse);
void affiche(Personne* a);
/*désalloue tout*/
void clear(Personne** quidam);
int main(void){
/*
Nelly Bord (NB) et ALbert Zweinstein (AZ) sont amis
Ahmas Pamouzh (AP) et Pierre Kiroul sont amis
Claude During (CD), Georges Chatnonne (GC) et Alan Boule (AB) sont tous 3 amis
NB, AP, GC et CD sont amis
Affichez le nombre d'amis communs à AB et AP puis le nombre d'amis communs à GC et CD
Faire déménager NB et l'afficher.
*/
Personne* NB;
naissance(&NB,"Bord Nelly", 28, "Rue du Petit-chêne");
affiche(NB);
Personne* AZ;
naissance(&AZ, "Albert Zweinstein",34,"Rue de Lyon");
affiche(AZ);
Personne* AP;
naissance(&AP, "Albert Zweinstein",45,"Rue du Lac");
affiche(AP);
Personne* PK;
naissance(&PK, "Pierre Kiroul",23,"Rue des Alpes");
affiche(PK);
Personne* CD;
naissance(&CD, "Claude During",89,"Rue des Oliviers");
affiche(CD);
Personne* GC;
naissance(&GC, "Georges Chatnonne",67,"Rue du Port");
affiche(GC);
Personne* AB;
naissance(&AB, "Alan Boule",7,"Rue du Commerce");
affiche(AB);
sont_amis(&NB,&AZ);
printf(NB.amis[1].nom);
demenage(&NB,"Rue des Acacias");
affiche(NB);
clear(&NB);
clear(&AB);
clear(&GC);
clear(&CD);
clear(&PK);
clear(&AP);
clear(&AZ);
clear(&NB);
return 0;
}
/* ================================================================== */
void naissance(Personne** quidam,char* nom,unsigned int age, char* adresse)
{
*quidam=malloc(sizeof(Personne));
(*quidam)->nom = calloc(strlen(nom)+1,sizeof(char));
if((*quidam)->nom !=NULL)
{
strcpy((*quidam)->nom, nom);
}
(*quidam)->adresse = calloc (strlen(adresse)+1, sizeof(char));
if((*quidam)->adresse != NULL)
{
strcpy((*quidam)->adresse, adresse);
}
(*quidam)->age = age;
(*quidam)->number = 0;
}
/* ================================================================== */
void sont_amis(Personne** a,Personne** b){
(*a)->amis= malloc(((*a)->number+1))
(*a)->amis[(*a)->number+1]=*b;
(*b)->amis= malloc(((*b)->number+1))
(*b)->amis[(*b)->number+1]=*a;
(*a)->number++;
(*b)->number++;;
}
/* ================================================================== */
void affiche(Personne* a){
printf("nom : %s\n",a->nom);
printf("age : %d ans\n",a->age);
printf("adresse : %s\n",a->adresse);
putchar('\n');
}
/* ================================================================== */
void demenage(Personne** a, char* adresse){
if((*a) != NULL)
(*a)->adresse = realloc ((*a)->adresse ,(strlen(adresse)+1) *sizeof(char));
if((*a)->adresse != NULL){
strcpy((*a)->adresse,adresse);
}
printf("%s déménage %s : \n",(*a)->nom,adresse);
putchar('\n');
}
/* ================================================================== */
void clear(Personne** quidam){
if((*quidam) != NULL){
if((*quidam)->adresse !=NULL){
free((*quidam)->adresse);
(*quidam)->adresse=NULL;
}
if((*quidam)->nom !=NULL){
free((*quidam)->nom);
(*quidam)->nom=NULL;
}
free((*quidam));
(*quidam)=NULL;
}
} |
Partager