Sunteți pe pagina 1din 7

Module : Atelier de Programmation Avancée Correction TD/TP N° 3 : Les listes chainées

Correction proposée par Mme Saloua Guezguez Sections : SI1-TRC1

Exercice 1 void affichLigne(Ligne l)


typedef struct {
{ printf("libelle=%s\t|prix unitaire=%.3f\t|nombre
char libelle[30]; d'articles=%d\t|total ligne=%.3f\n",l.libelle,l.prix_u,l.nb, totalLigne(l));
float prix_u; }
int nb; void ajouterLigne(Facture *f, Ligne l)
}Ligne; {
typedef struct noeud noeud *p, *nouveau;
{ nouveau=(noeud*)malloc(sizeof(noeud));
Ligne valeur; nouveau->valeur=l;
struct noeud *suivant; nouveau->suivant=NULL;
}noeud; if(f->tete==NULL)
typedef struct f->tete=nouveau;
{ else
noeud *tete; {
}Facture; p=f->tete;
void saisirLigne(Ligne *l) while(p->suivant!=NULL)
{ p=p->suivant;
printf("libelle="); scanf("%s", l->libelle); p->suivant=nouveau;
printf("prix unitaire="); scanf("%f", &l->prix_u); }
printf("nombre d'articles="); scanf("%d", &l->nb); }
}
float totalLigne(Ligne l)
{
return l.nb*l.prix_u;
}
Module : Atelier de Programmation Avancée Correction TD/TP N° 3 : Les listes chainées
Correction proposée par Mme Saloua Guezguez Sections : SI1-TRC1

void saisirFacture(Facture *f, int n) void affichFacture (Facture f)


{ {
int i; noeud *p=f.tete;
Ligne l; while(p!=NULL)
f->tete=NULL; {
for(i=1;i<=n;i++) affichLigne(p->valeur);
{ p=p->suivant;
printf("saisir la ligne %d\n", i); }
saisirLigne(&l); printf("total facture=%f\n", totalFacture(f));
ajouterLigne(f,l); }
} void main()
} {
float totalFacture(Facture f) Facture f;
{ int n;
float total=0; f.tete=NULL;
noeud *p=f.tete; printf("combien de lignes ?");
while(p!=NULL) scanf("%d", &n);
{ saisirFacture(&f, n);
total+=totalLigne(p->valeur); printf("voici votre facture\n");
p=p->suivant; affichFacture(f);
} }
return total;
}
Module : Atelier de Programmation Avancée Correction TD/TP N° 3 : Les listes chainées
Correction proposée par Mme Saloua Guezguez Sections : SI1-TRC1

Exercice 2 float salaireEmploye(Employe E)


typedef struct {
{ return E.nb_h*E.taux_h;
char nom[30]; }
int matricule; void affichEmploye(Employe E)
float nb_h; {
float taux_h; printf("nom=%s\n", E.nom);
}Employe; printf("matricule=%d\n", E.matricule);
typedef struct noeud printf("nombre d'heures=%f\n", E.nb_h);
{ printf("prix de l'heure=%f\n", E.taux_h);
Employe valeur; printf("salaire total=%f\n", salaireEmploye(E));
struct noeud *suivant; }
}noeud; void ajouterEmploye(ListEmp *l, Employe E)
typedef struct {
{ noeud *p, *nouveau;
noeud *tete; nouveau=(noeud*)malloc(sizeof(noeud));
}ListEmp; nouveau->valeur=E;
void saisirEmploye(Employe *E) nouveau->suivant=NULL;
{ if(l->tete==NULL)
printf("nom="); scanf("%s", E->nom); l->tete=nouveau;
printf("matricule="); scanf("%d", &E->matricule); else
printf("nombre d'heures="); scanf("%f", &E->nb_h); {
printf("prix de l'heure="); scanf("%f", &E->taux_h); p=l->tete;
} while(p->suivant!=NULL)
p=p->suivant;
p->suivant=nouveau;
}
Module : Atelier de Programmation Avancée Correction TD/TP N° 3 : Les listes chainées
Correction proposée par Mme Saloua Guezguez Sections : SI1-TRC1

} float totalSalaire(ListEmp l)
void saisirListEmployes(ListEmp *l, int n) {
{ float total=0;
int i; noeud *p=l.tete;
Employe E; while(p!=NULL)
l->tete=NULL; {
for(i=1;i<=n;i++) total+=salaireEmploye(p->valeur);
{ p=p->suivant;
printf("saisir l'employe %d\n", i); }
saisirEmploye(&E); return total;
ajouterEmploye(l, E); }
} void main()
} {
void affichListEmployes(ListEmp l) ListEmp l;
{ int i,n;
noeud *p=l.tete; printf("combien d'employes ?");
while(p!=NULL) scanf("%d", &n);
{ saisirListEmployes(&l, n);
affichEmploye(p->valeur); printf("voici la liste des employes\n");
p=p->suivant; affichListEmployes(l);
} printf("salaire total=%f\n", totalSalaire(l));
} }
Module : Atelier de Programmation Avancée Correction TD/TP N° 3 : Les listes chainées
Correction proposée par Mme Saloua Guezguez Sections : SI1-TRC1

Exercice 3 p=p->suivant;
typedef struct p->suivant=nouveau;
{ }
char nom[30]; }
int rdv; void rendezVous (SalleDattente s, int *rdv, int *sansRdv)
}Patient; {
typedef struct noeud noeud *p=s.tete;
{ *rdv=0;
Patient valeur; *sansRdv=0;
struct noeud *suivant; while(p!=NULL)
}noeud; {
typedef struct if(p->valeur.rdv==1)
{ (*rdv)++;
noeud *tete; else
}SalleDattente; (*sansRdv)++;
void ajouterPatient(SalleDattente *s, Patient pat) p=p->suivant;
{ }
noeud *p, *nouveau; }
nouveau=(noeud*)malloc(sizeof(noeud));
nouveau->valeur=pat;
nouveau->suivant=NULL;
if(s->tete==NULL)
s->tete=nouveau;
else
{
p=s->tete;
while(p->suivant!=NULL)
Module : Atelier de Programmation Avancée Correction TD/TP N° 3 : Les listes chainées
Correction proposée par Mme Saloua Guezguez Sections : SI1-TRC1

void supprimerPatient(SalleDattente *s) while(p->valeur.rdv==0)


{ {
int i,j; precedent=p;
noeud *p, *precedent; p=p->suivant;
rendezVous(*s, &i, &j); }
if (i==0) //aucun patient avec rdv //c'est le noeud p qui doit etre libere
{ precedent->suivant=p->suivant;
//on supprime la tête de liste free(p);
p=s->tete; }
s->tete=s->tete->suivant; }
free(p); }
} void consulter (SalleDattente s)
else {
{ noeud *p=s.tete;
//si le premier patient de la liste a un rdv printf("Patients avec RDV\n");
if(s->tete->valeur.rdv==1) while(p!=NULL)
{ {
//on supprime la tête de liste if(p->valeur.rdv==1)
p=s->tete; printf("%s\n",p->valeur.nom);
s->tete=s->tete->suivant; p=p->suivant;
free(p); }
} p=s.tete;
else printf("Patients sans RDV\n");
{ while(p!=NULL)
//on recherche le premier patient avec rdv {
p=s->tete->suivant; if(p->valeur.rdv==0)
precedent=s->tete; printf("%s\n",p->valeur.nom);
Module : Atelier de Programmation Avancée Correction TD/TP N° 3 : Les listes chainées
Correction proposée par Mme Saloua Guezguez Sections : SI1-TRC1

p=p->suivant; printf("suppression 1\n");


} printf("voici les patients en attente\n");
} consulter(s);
//pour tester ces fonctions supprimerPatient(&s);
void saisirPatient(Patient *p) printf("suppression 2\n");
{ printf("voici les patients en attente\n");
printf("nom="); scanf("%s", p->nom); consulter(s);
printf("rdv o/n? taper 1 pour oui, 0 pour non:"); }
scanf("%d", &p->rdv);
}
void main()
{
SalleDattente s;
Patient p;
int i, n;
s.tete=NULL;
printf("combien de patients ?");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
printf("saisir le patient %d\n", i);
saisirPatient(&p);
ajouterPatient(&s, p);
}
printf("voici les patients en attente\n");
consulter(s);
supprimerPatient(&s);

S-ar putea să vă placă și