1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

23
1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES

Transcript of 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

Page 1: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

1

TABLEAU DE POINTEURS

• TRAITEMENT DE TEXTE

• TRAITEMENT DE STRUCTURES

Page 2: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

2

Initialisation de tableaux de pointeurs sur des chaînes

Chaîne constante était traduite par le compilateur en une adresse que l'on pouvait affecter à un pointeur sur une chaîne. Cela peut se généraliser à un tableau de pointeurs, comme dans:

Cette déclaration réalise la création des 7 chaînes constantes correspondant aux 7 jours de la semaine et l'initialisation du tableau jour avec les 7 adresses de ces 7 chaînes.

char * jour[7] = {"lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche" } ;

Page 3: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

3

Initialisation de tableaux de pointeurs sur des chaînes

Example:

#include <stdio.h>int main(void){ char * jour[7] = { "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi",

"dimanche"}; int i ; printf ("donnez un entier entre 1 et 7 : ") ; scanf ("%d", &i) ; printf ("Le jour numéro %d de la semaine est %s", i, jour[i-1] ) ; return 0 ;}

donnez un entier entre 1 et 7 : 6Le jour numéro 6 de la semaine est samedi

tableau de sept pointeurs, chacun d'entre eux désignant une chaîne constante

Page 4: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

4

Traitement de texte

Faire un programme à traiter 20 lignes (au maximum) de text – 62 caractères par ligne en utilisant les fonctions suvantes: Entrer une ligne de texte Entrer tout le text Afficher le text entré Afficher toutes les lignes qui commencent par une lettre donné

char *li(char *s);int te(char *s[ ]);void out(char *s[ ],int l);void find(char *list[ ],int l);

Les prototypes des fonctions

Page 5: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

5

Traitement de texte

char *s[20]

Les lignes de texte

Page 6: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

6

Traitement de texte

#include<stdio.h> 1/5#include<stdlib.h>#include<conio.h>#include <string.h>#define L 20#define C 63

char *li(char *s);int te(char *s[]);void out(char *s[],int l);void find(char *list[],int l);

Les directives

Les prototypes des fonctions

Page 7: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

7

Traitement de texte

void main() 2/5 {

char *t[L];int l, i;l = te(t);

out(t,l);

find(t,l);

}

Appel de la fonction d’entré texte

Appel de la fonction d’affichage texte

Appel de la fonction d’affichage des lignes

Page 8: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

8

Traitement de texte

//entrer une ligne de texte 3/5

char *li(char *s) { char c,*p=s;

int i=0;printf("Entrer ligne.\n");while(c=getchar(),c!='\n'&&c!=EOF&&i<C-1) { *s++=c; i++; }if(i==0&&c==EOF) return NULL;else {*s='\0';

return p; }

}

La définition de la fonction d’entré ligne

Page 9: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

9

Traitement de texte

//entrer tout le texte 4/5 int te(char *s[]) { int i=0,l;

char *buf; buf=(char*)malloc(63); while(1) { printf("li %d\n",i);

if(li(buf)==NULL || i==L)break; l=strlen(buf); s[i]=(char *)malloc(l+1); if(s[i]==NULL)

{ printf("Pas de place\n"); exit(1); }

strcpy(s[i],buf); i++;

}free (buf); free(s); return i;

}

La définition de la fonction d’entré texte

li 0Entrer ligne.programme traitement texteli 1Entrer ligne.affichage des lignesli 2Entrer ligne.qui commencent par une lettreli 3Entrer ligne. CTRL+Z

Page 10: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

10

Traitement de texte

void out(char *s[],int l) 5/5 { int i;

for(i=0;i<l;i++)puts(s[i]);

} void find(char *list[],int l) { int i,cle=0; char car; printf("Entrer caractère ="); car=getch(); printf("\n"); for(i=0;i<l;i++)

{ if(*(list[i])== car){ puts(list[i]); cle=1;}

} if(!cle)

printf(“Il n’y a aucune ligne qui commence avec %c\n“,car);}

La définition de la fonction d’affichage texte

La définition de la fonction d’affichage des lignes

programme traitement texteaffichage des lignesqui commencent par une lettreEntrer caractere = pprogramme traitement texte

Page 11: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

11

Traitement de structures modèle 1

Faire un programme C de création et traitement d'un tableau d'éléments de type structuré (avec un nombre maximal 30). Afficher tous les étudiants d’un faculté donné. Chaque structure est composée des champs suivants: a) nom - chaîne de caractères; b) faculté - chaîne de caractères; c) note moyenne - nombre réel.

Utiliser les fonctions suvantes: Entrer les éléments dans le tableau

Afficher le tableau Afficher les étudiants d’un faculté donné au bien un message (s’il n’y a aucun étudiant)

Page 12: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

12

Traitement de structures modèle 1

etudiant *tab[MAX]

Les structuresnom faculte note

Page 13: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

13

Traitement de structures

typedef struct{ char nom[30];

char faculte[10];float note;

} etudiant;

int entrer(etudiant *s[ ]);void sortir(etudiant *s[ ],int n);void sortir_el(etudiant *s);void chercher(etudiant *s[ ],int n,char fac_d[]);

Les prototypes des fonctions

La définition de la structure

Page 14: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

14

Traitement de structures

#include <stdio.h> 1/5#include <conio.h>#include <string.h>#include <stdlib.h>#define MAX 30#define RET ""

typedef struct{ char nom[30];

char faculte[10];float note;

} etudiant;

int entrer(etudiant *s[]);void sortir(etudiant *s[],int n);void sortir_el(etudiant *s);void chercher(etudiant *s[],int n,char fac_d[]);

Les prototypes des fonctions

La définition de la structure

Les directives

Page 15: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

15

Traitement de structures

void main() 2/5 { int n;

char fac_d[10];etudiant *tab[MAX];printf("Entrer les donnees des "

"etudiants.\n");n=entrer(tab);

printf("\n La liste donnee\n");sortir(tab,n);

printf("Entrer faculte donne:");gets(fac_d);chercher(tab,n,fac_d);

}

Appel de la fonction d’entré du tableau

Appel de la fonction d’affichage du tableau

Appel de la fonction d’affichage d’ éléments de la faculté donné

Page 16: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

16

Traitement de structures

int entrer(etudiant *s[]) 3/5 { char *buf=(char*)malloc(31);

int n=0;while(1)

{ printf("Entrer Nom ou bien RET:");if(strcmp(gets(buf),RET)==0 || n==MAX) break;else

{ s[n]=(etudiant*)malloc(sizeof(etudiant)); strcpy(s[n]->nom,buf); printf("Entrer faculte:"); gets(s[n]->faculte); printf("Entrer note:"); s[n]->note=atof(gets(buf));}

n++;}

free(buf); free(s); return n;

}

La définition de la fonction d’entré

Entrer les donnees des etudiants.Entrer Nom ou bien RET:AnaEntrer faculte: FOEEntrer note: 4.50Entrer Nom ou bien RET: IvanEntrer faculte: FETTEntrer note: 5.50Entrer Nom ou bien RET:LiliEntrer faculte: FOEEntrer note: 3.50

Page 17: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

17

Traitement de structures

void sortir(etudiant *s[],int n) 4/5 { int i; for(i=0;i<n;i++) {

sortir_el(s[i]); }}

void sortir_el(etudiant *s) { printf("Nom: %s\n",s -> nom);

printf("faculte: %s\n",s -> faculte);printf("note: %.2f\n",s -> note);

}

La définition de la fonction d’affichagedu tableau

La définition de la fonction d’affichaged’un élément

La liste donneeNom: Anafaculte: FOEnote: 4.50Nom: Ivanfaculte: FETTnote: 5.50 Nom: Lilifaculte: FOEnote: 3.50

Page 18: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

18

Traitement de structures

void chercher(etudiant *s[],int n,char fac_d[]) 5/5 { int trouve=0;

int i;for (i=0; i<n;i++)

if (strcmp(s[i]->faculte,fac_d)==0){ sortir_el(s[i]); trouve=1;}

if(!trouve)printf("On n'a pas trouve etudiant de faculte %s \n",fac_d);

}

La définition de la fonction d’affichage d’ éléments de la faculté donné

Entrer faculte donne:FETTNom: Ivanfaculte: FETTnote: 5.50

Page 19: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

19

Traitement de structures modèle 2

Faire un programme C de création et affichage d'un tableau dynamique d'éléments de type structuré. Chaque structure est composée des champs suivants: a) nom - chaîne de caractères; b) note. Utiliser les fonctions suvantes:

Création du tableau dynamique Affichage du tableau Réallocation de la zone dynamique

Page 20: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

20

Traitement de structures modèle 2

#include <stdio.h>#include <stdlib.h>typedef struct{ char nom[30];

float note;} etud;

etud **creation(int l);

void affichage(int l, etud **a);

etud **recreation(etud **x,int l);

Page 21: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

21

Traitement de structures modèle 2

void main(){ etud **x;

int l;printf("nombre de structures: ");scanf("%d",&l);x=creation(l);printf("Le tableau des structures cree.\n");affichage(l,x);l++;x=recreation(x,l);affichage(l,x);free(x);

}

Page 22: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

22

Traitement de structures modèle 2

etud **creation(int l){ etud **t;

int i; t=(etud **)malloc(l*sizeof(etud *)); for(i=0;i<l;i++)

{ t[i]=(etud *)malloc(sizeof(etud));printf("structure%d:",i);printf("nom:");fflush(stdin);gets(t[i]-> nom);printf("note:");scanf("%f",&(*t[i]).note);

}return t;

}

Page 23: 1 TABLEAU DE POINTEURS TRAITEMENT DE TEXTE TRAITEMENT DE STRUCTURES.

23

Traitement de structures modèle 2

void affichage(int l,etud **a){ int i; for(i=0;i<l;i++)

{ printf("%s %.2f\n",a[i] -> nom,a[i]->note); printf("\n");}

}

etud **recreation(etud **t,int l){ int i;

t=(etud **)realloc(t,sizeof(etud *));t[l-1]=(etud *)malloc(sizeof(etud));

printf("nom:");fflush(stdin);gets(t[l-1]-> nom);printf("note:");scanf("%f",&(*t[l-1]).note);return t;

}