SlideShare une entreprise Scribd logo
1  sur  37
INFORMATIQUE III  DEVOIR SURVEIEE  2 Notes 18  -  20   p.  ( excellent  6);  15  –  17   p . ( tr è s bien  5); 12 – 14 p. (bien 4); 10 – 11 p. (passable 3)
[object Object],[object Object],#include <stdio.h> #include <string.h> #define N 3 typedef struct el { int  inf; char name[15]; }unit; unit f(unit a); int main(){ unit lst[N], *lstp[N]; char p[]=&quot;Paris Sofia&quot;, x,y; for(x=0; x<N; x++){   lst[x].inf=x+1;   strcpy(lst[x].name,p+x);   printf(&quot;1: %s&quot;,lst[x].name+2);   lstp[x]=lst+x; }
[object Object],[object Object],lstp[0]=lstp[2]; for(x=0; x<N; x++){ printf(&quot;2:%d %s&quot;,lstp[x]->inf, lstp[x]->name); } *lstp[1]=f(*lstp[0]);   printf(&quot;4:%d %s&quot;,lstp[1]->inf,lstp[1]->name);   return 0; } unit f(unit a) {  unit b; a.inf*=2; b.inf=(2*a.inf)+1; strcpy(b.name,a.name+4); printf(&quot;3:%d %s&quot;,a.inf,a.name); return b; }
[object Object],unit *lstp[3] unit  lst[3] inf name 1  Paris Sofia 2  aris Sofia 3  ris Sofia
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],#include <stdio.h> int gcd(int, int); int main() { int a=33, b=15; if (a < 1 || b < 1) {  printf(&quot;Erreur! &quot;);   return 1; } printf(&quot;%d&quot;,gcd(a, b)); return 0; } int gcd(int a, int b) {if (a == b) return a; if (a > b) return gcd(a-b, b);   return gcd(a, b-a); }
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],Maria Angelova Ani Koleva Ivan Petrov Asen Milanov
#include<stdio.h> #include<stdlib.h> #include <string.h> #define  NUM 2 void output(char **p,int k); void sort(char **p,int k); char ** insert(char **ptext,int *n_l); int main(void) {  int n_l; char **ptext; ptext=insert(ptext,&n_l); printf(&quot;%d chaines (noms) ont lus&quot;,n_l); printf(&quot;Liste originale des noms:&quot;); output(ptext,n_l); sort(ptext,n_l); printf(&quot;Liste des nom tries:&quot;); output(ptext,n_l); free(ptext);  return(0);  }
char ** insert(char **ptext,int *n_l) { char *p; FILE *fp; int  n_el=NUM ; *n_l=0; char buf[SIZE]; if((fp=fopen(&quot;test.txt&quot;, &quot;r&quot;)) == NULL) {   printf(&quot;Fichier n'est pas ouvert.&quot;);   exit(1); } if((ptext=(char **) malloc ( n_el *sizeof(char *)))==NULL){   printf(&quot;Erreur!&quot;);   exit(1); }
do{ if( fgets(buf,sizeof(buf),fp)== NULL){ printf(&quot;Erreur lecture!&quot;); exit(1); } if((p=(char*)malloc( sizeof(char)*(strlen(buf)+1)))== NULL){ printf(&quot;Erreur!&quot;);   exit(1); } strcpy(p,buf); if(*n_l==n_el) {  n_el+=NUM ; if (ptext=(char**) realloc (ptext, n_el *sizeof(char*)))==NULL){ printf(&quot;Erreur!&quot;);   exit(1); } } ptext[(*n_l)++]=p; } while(!feof(fp)); strcat(*(ptext+*n_l-1),&quot;&quot;); return ptext; }
void output(char **p,int k) { int j; for(j=0;j<k;j++)   printf(&quot;%s&quot;,*(p+j)); } void sort(char **p,int k) { int j,flag; char st[80]; do{  flag=0;   for(j=0;j<k-1;j++)   if(strcmp(*(p+j),*(p+j+1))>0){   flag=1;   strcpy(st,*(p+j));   strcpy(*(p+j),*(p+j+1));   strcpy(*(p+j+1),st);   } }  while(flag); }
 
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <memory.h> #define NBR 5 typedef struct { char name[20]; float averageMark; }STUDENT; void myMenu(); STUDENT * addStudentFromKey(short *); void inputStudent(STUDENT*); void printArray(STUDENT *,short ); FILE *writeStructFile(STUDENT *,short); void sortArray(STUDENT *,short); int sortName(const void *,const void *); int sortAverageMark(const void *,const void *); void SearchFile(FILE *);
int main() { myMenu(); return 0; } ///////////////////////////////////////////////// void myMenu() {  FILE *fp; short a,i; STUDENT *array; do { printf(&quot;1.Creer tableau dynamique des structures - entrees par clavier  2.Afficher le tableau  3.Ecrire le tableau dans un fichier binaire  4.Trier le tableau dynamique des structures  5.Chercher dans le fichier les etudiants avec notes >=5.50&ecrire  6.Fin  Votre choix :&quot;);   scanf(&quot;%d&quot;,&a);
switch(a) { case 1:array=addStudentFromKey(&i);break; case 2:printArray(array,i);break; case 3:fp=writeStructFile(array,i);break; case 4:sortArray(array,i); break; case 5:SearchFile(fp); } }while(a!=6); free(array); }
STUDENT* addStudentFromKey(short *i) {  STUDENT *array,a; char word[4]; int n=NBR; *i=0; array=(STUDENT*)malloc(n*sizeof(STUDENT)); if(array == NULL) exit(1); while(printf(&quot;La structure suivante dans le tableau? - (oui/no)&quot;),    fflush(stdin),strcmp(gets(word),&quot;oui&quot;)==0)   {  if(*i == n)   {  n=n*2;   array=(STUDENT*)realloc(array,n*sizeof(STUDENT));   if(array == NULL) exit(1);   }   printf(&quot;ETUDIANT  NOMBRE %d: &quot;,*i+1);   inputStudent(&a);   memcpy(&array[*i],&a,sizeof(STUDENT));   (*i)++; } return array; }
void inputStudent(STUDENT *a) { fflush(stdin); printf(&quot;Entrer le nom:&quot;); gets(a->name); printf(&quot;Entrer la note moyenne:&quot;); scanf(&quot;%f&quot;,&a->averageMark); } ////////////////////////////////////////////////////////////////////////////////////////// void printArray(STUDENT *array,short n) { int i; printf(&quot;Les elements du tableau dynamique  ETUDIANT  NOMBRE  ETUDIANT NOM    ETUDIANT NOTE MOYENNE&quot;); for(i=0;i<n;i++) printf(&quot;%-18d %-28s %-10.2f&quot;,i+1,array[i].name,array[i].averageMark); }
FILE* writeStructFile(STUDENT *array,short n) { FILE *fp; char fname[15]; short i; puts(&quot;Entrer le nom du fichier pour ecrire&quot;); fflush(stdin); gets(fname); fp=fopen(fname,&quot;w+b&quot;); if(fp==NULL) { printf( &quot;Problem d'ouverture&quot; ); exit(1); } for(i=0;i<n;i++) fwrite(&array[i],sizeof(STUDENT),1,fp); return(fp); }
void sortArray(STUDENT *array,short n) {  short k; printf(&quot;Choix du tri :0 - par nom;1 - par note moyenne&quot;); fflush(stdin); scanf(&quot;%d&quot;,&k); qsort((void*)array,n,sizeof(STUDENT),k?sortAverageMark:sortName); } ////////////////////////////////////////////////////////////////////////////// int sortName(const void *pa,const void *pb) { char x; STUDENT*a=(STUDENT*)pa; STUDENT*b=(STUDENT*)pb; x=strcmp(a->name,b->name); if(x>0) return 1; else if(x<0) return -1; else return 0; }
int sortAverageMark(const void *pa,const void *pb) { STUDENT*a=(STUDENT*)pa; STUDENT*b=(STUDENT*)pb; if(a->averageMark > b->averageMark) return 1; else if(a->averageMark < b->averageMark) return -1; else return 0; }
void SearchFile(FILE *fp) {  FILE *fp_new; char fname_new[15]; STUDENT temp; rewind(fp); puts(&quot;Entrer le nom du fichier pour ecrire&quot;); fflush(stdin); gets(fname_new); fp_new=fopen(fname_new,&quot;w+b&quot;); if(fp_new==NULL) { printf( &quot;Problem problem d'ouverture&quot; ); exit(1); } while(fread(&temp,sizeof(STUDENT),1,fp)==1)   if(temp.averageMark >= 5.50)   fwrite(&temp,sizeof(STUDENT),1,fp_new); rewind(fp_new); while(fread(&temp,sizeof(STUDENT),1,fp_new)==1)   printf(&quot;%-28s %-10.2f&quot;,temp.name,temp.averageMark); fclose(fp); fclose(fp_new); }
 
 
 
 
 
 
[object Object],[object Object]
ppch[0] word0 ppch[1] ppch[2] word1 word2 ppch[i] ppch[n] wordn char **ppch
#include <stdio.h> #include <string.h> #include <stdlib.h> #define NUM 2 void prt(char **ppch,int n){ int i; for(i=0;i<n;i++){ printf(&quot;%s&quot;,ppch[i]); } } int exist(char **ppch, int n, char *w){ int i; for(i=0; i<n;i++){ if(!strcmp(ppch[i],w)) return 1; } return 0; }
char ** insert(char **ppch, int *n_w,int *n_el, char *w){ char *p; if ((p = (char *)malloc(sizeof(char)*(strlen(w)+1)))== NULL){ return NULL; } strcpy(p, w); if(*n_w == *n_el){ *n_el+=NUM; if ( (ppch = (char **)realloc( ppch,*n_el*sizeof(char*)))==NULL)  { return NULL; } } ppch[(*n_w)++]= p; return ppch; }
void sort(char **ppch, int n_w){ int i,ok; char *help; do {  ok=1; for(i=0;i<n_w-1;i++){ if(strcmp(ppch[i],ppch[i+1])>0){ help=ppch[i]; ppch[i]=ppch[i+1]; ppch[i+1]=help; ok=0; } } }while (!ok); } void free_m(char **ppch,int n_words){ int i; for(i=0;i<n_words;i++){ free(ppch[i]); } free(ppch); }
int main(){ int n_words=0,  n_el=NUM ; char buf[251], **ppch; FILE *f; if((f=fopen(&quot;b.txt&quot;,&quot;rt&quot;))==NULL){ printf (&quot;The file cannot be read&quot;); return 2; } if ( (ppch = (char **)malloc( n_el*sizeof(char*)))== NULL){ printf(&quot;No memory&quot;); return 3; } while(!feof(f)) { if( fscanf(f,&quot;%250s&quot;,buf) == EOF){ break; } if(!exist(ppch,n_words,buf)){ if((ppch=insert(ppch, &n_words,&n_el,buf))==NULL){ printf(&quot;No memory&quot;); return 3; } } } sort(ppch, n_words); prt(ppch, n_words); free_m(ppch,n_words); return 0; } alfa alfa  tita gama  beta alfa beta gama tita

Contenu connexe

Tendances

Seance 3- Programmation en langage C
Seance 3- Programmation en langage C Seance 3- Programmation en langage C
Seance 3- Programmation en langage C
Fahad Golra
 

Tendances (20)

Cours structures des données (langage c)
Cours structures des données (langage c)Cours structures des données (langage c)
Cours structures des données (langage c)
 
Scala : programmation fonctionnelle
Scala : programmation fonctionnelleScala : programmation fonctionnelle
Scala : programmation fonctionnelle
 
Seance 3- Programmation en langage C
Seance 3- Programmation en langage C Seance 3- Programmation en langage C
Seance 3- Programmation en langage C
 
Seance 4- Programmation en langage C
Seance 4- Programmation en langage CSeance 4- Programmation en langage C
Seance 4- Programmation en langage C
 
Les nouveautés de C++11 : Ecrire du C++ Moderne
Les nouveautés de C++11 : Ecrire du C++ ModerneLes nouveautés de C++11 : Ecrire du C++ Moderne
Les nouveautés de C++11 : Ecrire du C++ Moderne
 
Résumé Algorithme et Programmation
Résumé Algorithme et ProgrammationRésumé Algorithme et Programmation
Résumé Algorithme et Programmation
 
Le langage C
Le langage CLe langage C
Le langage C
 
C4 fonctions
C4 fonctionsC4 fonctions
C4 fonctions
 
Chap2fonctionscpp
Chap2fonctionscppChap2fonctionscpp
Chap2fonctionscpp
 
Développer en natif avec C++11
Développer en natif avec C++11Développer en natif avec C++11
Développer en natif avec C++11
 
Polymorphisme, interface et classe abstraite
Polymorphisme, interface et classe abstraitePolymorphisme, interface et classe abstraite
Polymorphisme, interface et classe abstraite
 
Exercices pascal tous les chapitres
Exercices pascal tous les chapitresExercices pascal tous les chapitres
Exercices pascal tous les chapitres
 
Chapitre4: Pointeurs et références
Chapitre4: Pointeurs et références Chapitre4: Pointeurs et références
Chapitre4: Pointeurs et références
 
Chapitre2fonctionscppv2019
Chapitre2fonctionscppv2019Chapitre2fonctionscppv2019
Chapitre2fonctionscppv2019
 
Type abstrait de données
Type abstrait de donnéesType abstrait de données
Type abstrait de données
 
Chapitre2 prog dsplf3
Chapitre2 prog dsplf3Chapitre2 prog dsplf3
Chapitre2 prog dsplf3
 
Implémentation optimale de filtres linéaires en arithmétique virgule fixe
Implémentation optimale de filtres linéaires en arithmétique virgule fixeImplémentation optimale de filtres linéaires en arithmétique virgule fixe
Implémentation optimale de filtres linéaires en arithmétique virgule fixe
 
Initiation au code : Ateliers en C# (applications desktop et mobile native)
Initiation au code : Ateliers en C# (applications desktop et mobile native)Initiation au code : Ateliers en C# (applications desktop et mobile native)
Initiation au code : Ateliers en C# (applications desktop et mobile native)
 
Corrige exercices pascal_fenni_2018
Corrige exercices pascal_fenni_2018Corrige exercices pascal_fenni_2018
Corrige exercices pascal_fenni_2018
 
09 big data mapreduce
09 big data mapreduce09 big data mapreduce
09 big data mapreduce
 

En vedette (7)

Transformaciones lineales
Transformaciones linealesTransformaciones lineales
Transformaciones lineales
 
Waardig Organiseren Artikel
Waardig Organiseren ArtikelWaardig Organiseren Artikel
Waardig Organiseren Artikel
 
Camilacjd
CamilacjdCamilacjd
Camilacjd
 
Certingresos1
Certingresos1Certingresos1
Certingresos1
 
Conexión a bases de datos
Conexión a bases de datosConexión a bases de datos
Conexión a bases de datos
 
C:\Fakepath\Christie
C:\Fakepath\ChristieC:\Fakepath\Christie
C:\Fakepath\Christie
 
Estrategias competitivas básicas
Estrategias competitivas básicasEstrategias competitivas básicas
Estrategias competitivas básicas
 

Similaire à Lect14 dev2

Similaire à Lect14 dev2 (20)

DartttttttttttttttttttttttversionFinal.pdf
DartttttttttttttttttttttttversionFinal.pdfDartttttttttttttttttttttttversionFinal.pdf
DartttttttttttttttttttttttversionFinal.pdf
 
C1 - Langage C - ISIMA - Première partie
C1 - Langage C - ISIMA - Première partieC1 - Langage C - ISIMA - Première partie
C1 - Langage C - ISIMA - Première partie
 
C++ 11/14
C++ 11/14C++ 11/14
C++ 11/14
 
Algorithmique Amp Programmation (R Sum
Algorithmique  Amp  Programmation (R SumAlgorithmique  Amp  Programmation (R Sum
Algorithmique Amp Programmation (R Sum
 
09 big data mapreduce
09 big data mapreduce09 big data mapreduce
09 big data mapreduce
 
Google Developer Group (GDG) Aix-Marseille #1 (27/08/2018)
Google Developer Group (GDG) Aix-Marseille #1 (27/08/2018)Google Developer Group (GDG) Aix-Marseille #1 (27/08/2018)
Google Developer Group (GDG) Aix-Marseille #1 (27/08/2018)
 
Cours C Avancé chapitre 2 et chapitre.pdf
Cours C Avancé  chapitre 2 et chapitre.pdfCours C Avancé  chapitre 2 et chapitre.pdf
Cours C Avancé chapitre 2 et chapitre.pdf
 
Theme 6
Theme 6Theme 6
Theme 6
 
Chap 2--POO avec JAVA.pdf
Chap 2--POO avec JAVA.pdfChap 2--POO avec JAVA.pdf
Chap 2--POO avec JAVA.pdf
 
Algo poo ts
Algo poo tsAlgo poo ts
Algo poo ts
 
Tour C++
Tour C++Tour C++
Tour C++
 
Theme 7
Theme 7Theme 7
Theme 7
 
cours-5.1.pdf
cours-5.1.pdfcours-5.1.pdf
cours-5.1.pdf
 
TAD (1).pptx
TAD (1).pptxTAD (1).pptx
TAD (1).pptx
 
C++11 en 12 exemples simples
C++11 en 12 exemples simplesC++11 en 12 exemples simples
C++11 en 12 exemples simples
 
Formation C# - Cours 2 - Programmation procédurale
Formation C# - Cours 2 - Programmation procéduraleFormation C# - Cours 2 - Programmation procédurale
Formation C# - Cours 2 - Programmation procédurale
 
Chapitre 3 tableaux et pointeurs en C
Chapitre 3 tableaux et pointeurs en CChapitre 3 tableaux et pointeurs en C
Chapitre 3 tableaux et pointeurs en C
 
Interception de signal avec dump de la pile d'appel
Interception de signal avec dump de la pile d'appelInterception de signal avec dump de la pile d'appel
Interception de signal avec dump de la pile d'appel
 
Cours de C++ / Tronc commun deuxième année ISIMA
Cours de C++ / Tronc commun deuxième année ISIMACours de C++ / Tronc commun deuxième année ISIMA
Cours de C++ / Tronc commun deuxième année ISIMA
 
resume algo 2023.pdf
resume algo 2023.pdfresume algo 2023.pdf
resume algo 2023.pdf
 

Dernier

Bilan énergétique des chambres froides.pdf
Bilan énergétique des chambres froides.pdfBilan énergétique des chambres froides.pdf
Bilan énergétique des chambres froides.pdf
AmgdoulHatim
 
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptxCopie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
ikospam0
 

Dernier (20)

Bilan énergétique des chambres froides.pdf
Bilan énergétique des chambres froides.pdfBilan énergétique des chambres froides.pdf
Bilan énergétique des chambres froides.pdf
 
Chapitre 2 du cours de JavaScript. Bon Cours
Chapitre 2 du cours de JavaScript. Bon CoursChapitre 2 du cours de JavaScript. Bon Cours
Chapitre 2 du cours de JavaScript. Bon Cours
 
Sidonie au Japon . pptx Un film français
Sidonie    au   Japon  .  pptx  Un film françaisSidonie    au   Japon  .  pptx  Un film français
Sidonie au Japon . pptx Un film français
 
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptxCopie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
Copie de Engineering Software Marketing Plan by Slidesgo.pptx.pptx
 
RAPPORT DE STAGE D'INTERIM DE ATTIJARIWAFA BANK
RAPPORT DE STAGE D'INTERIM DE ATTIJARIWAFA BANKRAPPORT DE STAGE D'INTERIM DE ATTIJARIWAFA BANK
RAPPORT DE STAGE D'INTERIM DE ATTIJARIWAFA BANK
 
Formation qhse - GIASE saqit_105135.pptx
Formation qhse - GIASE saqit_105135.pptxFormation qhse - GIASE saqit_105135.pptx
Formation qhse - GIASE saqit_105135.pptx
 
Computer Parts in French - Les parties de l'ordinateur.pptx
Computer Parts in French - Les parties de l'ordinateur.pptxComputer Parts in French - Les parties de l'ordinateur.pptx
Computer Parts in French - Les parties de l'ordinateur.pptx
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI DẠY BUỔI 2) - TIẾNG ANH 6, 7 GLOBAL SUCCESS (2...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI DẠY BUỔI 2) - TIẾNG ANH 6, 7 GLOBAL SUCCESS (2...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI DẠY BUỔI 2) - TIẾNG ANH 6, 7 GLOBAL SUCCESS (2...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI DẠY BUỔI 2) - TIẾNG ANH 6, 7 GLOBAL SUCCESS (2...
 
Conférence Sommet de la formation 2024 : Développer des compétences pour la m...
Conférence Sommet de la formation 2024 : Développer des compétences pour la m...Conférence Sommet de la formation 2024 : Développer des compétences pour la m...
Conférence Sommet de la formation 2024 : Développer des compétences pour la m...
 
La nouvelle femme . pptx Film français
La   nouvelle   femme  . pptx  Film françaisLa   nouvelle   femme  . pptx  Film français
La nouvelle femme . pptx Film français
 
Formation échiquéenne jwhyCHESS, parallèle avec la planification de projet
Formation échiquéenne jwhyCHESS, parallèle avec la planification de projetFormation échiquéenne jwhyCHESS, parallèle avec la planification de projet
Formation échiquéenne jwhyCHESS, parallèle avec la planification de projet
 
La mondialisation avantages et inconvénients
La mondialisation avantages et inconvénientsLa mondialisation avantages et inconvénients
La mondialisation avantages et inconvénients
 
Intégration des TICE dans l'enseignement de la Physique-Chimie.pptx
Intégration des TICE dans l'enseignement de la Physique-Chimie.pptxIntégration des TICE dans l'enseignement de la Physique-Chimie.pptx
Intégration des TICE dans l'enseignement de la Physique-Chimie.pptx
 
L application de la physique classique dans le golf.pptx
L application de la physique classique dans le golf.pptxL application de la physique classique dans le golf.pptx
L application de la physique classique dans le golf.pptx
 
les_infections_a_streptocoques.pptkioljhk
les_infections_a_streptocoques.pptkioljhkles_infections_a_streptocoques.pptkioljhk
les_infections_a_streptocoques.pptkioljhk
 
L'expression du but : fiche et exercices niveau C1 FLE
L'expression du but : fiche et exercices  niveau C1 FLEL'expression du but : fiche et exercices  niveau C1 FLE
L'expression du but : fiche et exercices niveau C1 FLE
 
CompLit - Journal of European Literature, Arts and Society - n. 7 - Table of ...
CompLit - Journal of European Literature, Arts and Society - n. 7 - Table of ...CompLit - Journal of European Literature, Arts and Society - n. 7 - Table of ...
CompLit - Journal of European Literature, Arts and Society - n. 7 - Table of ...
 
Les roches magmatique géodynamique interne.pptx
Les roches magmatique géodynamique interne.pptxLes roches magmatique géodynamique interne.pptx
Les roches magmatique géodynamique interne.pptx
 
Apolonia, Apolonia.pptx Film documentaire
Apolonia, Apolonia.pptx         Film documentaireApolonia, Apolonia.pptx         Film documentaire
Apolonia, Apolonia.pptx Film documentaire
 
Cours ofppt du Trade-Marketing-Présentation.pdf
Cours ofppt du Trade-Marketing-Présentation.pdfCours ofppt du Trade-Marketing-Présentation.pdf
Cours ofppt du Trade-Marketing-Présentation.pdf
 

Lect14 dev2

  • 1. INFORMATIQUE III DEVOIR SURVEIEE 2 Notes 18 - 20 p. ( excellent 6); 15 – 17 p . ( tr è s bien 5); 12 – 14 p. (bien 4); 10 – 11 p. (passable 3)
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11. #include<stdio.h> #include<stdlib.h> #include <string.h> #define NUM 2 void output(char **p,int k); void sort(char **p,int k); char ** insert(char **ptext,int *n_l); int main(void) { int n_l; char **ptext; ptext=insert(ptext,&n_l); printf(&quot;%d chaines (noms) ont lus&quot;,n_l); printf(&quot;Liste originale des noms:&quot;); output(ptext,n_l); sort(ptext,n_l); printf(&quot;Liste des nom tries:&quot;); output(ptext,n_l); free(ptext); return(0); }
  • 12. char ** insert(char **ptext,int *n_l) { char *p; FILE *fp; int n_el=NUM ; *n_l=0; char buf[SIZE]; if((fp=fopen(&quot;test.txt&quot;, &quot;r&quot;)) == NULL) { printf(&quot;Fichier n'est pas ouvert.&quot;); exit(1); } if((ptext=(char **) malloc ( n_el *sizeof(char *)))==NULL){ printf(&quot;Erreur!&quot;); exit(1); }
  • 13. do{ if( fgets(buf,sizeof(buf),fp)== NULL){ printf(&quot;Erreur lecture!&quot;); exit(1); } if((p=(char*)malloc( sizeof(char)*(strlen(buf)+1)))== NULL){ printf(&quot;Erreur!&quot;); exit(1); } strcpy(p,buf); if(*n_l==n_el) { n_el+=NUM ; if (ptext=(char**) realloc (ptext, n_el *sizeof(char*)))==NULL){ printf(&quot;Erreur!&quot;); exit(1); } } ptext[(*n_l)++]=p; } while(!feof(fp)); strcat(*(ptext+*n_l-1),&quot;&quot;); return ptext; }
  • 14. void output(char **p,int k) { int j; for(j=0;j<k;j++) printf(&quot;%s&quot;,*(p+j)); } void sort(char **p,int k) { int j,flag; char st[80]; do{ flag=0; for(j=0;j<k-1;j++) if(strcmp(*(p+j),*(p+j+1))>0){ flag=1; strcpy(st,*(p+j)); strcpy(*(p+j),*(p+j+1)); strcpy(*(p+j+1),st); } } while(flag); }
  • 15.  
  • 16.
  • 17. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <memory.h> #define NBR 5 typedef struct { char name[20]; float averageMark; }STUDENT; void myMenu(); STUDENT * addStudentFromKey(short *); void inputStudent(STUDENT*); void printArray(STUDENT *,short ); FILE *writeStructFile(STUDENT *,short); void sortArray(STUDENT *,short); int sortName(const void *,const void *); int sortAverageMark(const void *,const void *); void SearchFile(FILE *);
  • 18. int main() { myMenu(); return 0; } ///////////////////////////////////////////////// void myMenu() { FILE *fp; short a,i; STUDENT *array; do { printf(&quot;1.Creer tableau dynamique des structures - entrees par clavier 2.Afficher le tableau 3.Ecrire le tableau dans un fichier binaire 4.Trier le tableau dynamique des structures 5.Chercher dans le fichier les etudiants avec notes >=5.50&ecrire 6.Fin Votre choix :&quot;); scanf(&quot;%d&quot;,&a);
  • 19. switch(a) { case 1:array=addStudentFromKey(&i);break; case 2:printArray(array,i);break; case 3:fp=writeStructFile(array,i);break; case 4:sortArray(array,i); break; case 5:SearchFile(fp); } }while(a!=6); free(array); }
  • 20. STUDENT* addStudentFromKey(short *i) { STUDENT *array,a; char word[4]; int n=NBR; *i=0; array=(STUDENT*)malloc(n*sizeof(STUDENT)); if(array == NULL) exit(1); while(printf(&quot;La structure suivante dans le tableau? - (oui/no)&quot;), fflush(stdin),strcmp(gets(word),&quot;oui&quot;)==0) { if(*i == n) { n=n*2; array=(STUDENT*)realloc(array,n*sizeof(STUDENT)); if(array == NULL) exit(1); } printf(&quot;ETUDIANT NOMBRE %d: &quot;,*i+1); inputStudent(&a); memcpy(&array[*i],&a,sizeof(STUDENT)); (*i)++; } return array; }
  • 21. void inputStudent(STUDENT *a) { fflush(stdin); printf(&quot;Entrer le nom:&quot;); gets(a->name); printf(&quot;Entrer la note moyenne:&quot;); scanf(&quot;%f&quot;,&a->averageMark); } ////////////////////////////////////////////////////////////////////////////////////////// void printArray(STUDENT *array,short n) { int i; printf(&quot;Les elements du tableau dynamique ETUDIANT NOMBRE ETUDIANT NOM ETUDIANT NOTE MOYENNE&quot;); for(i=0;i<n;i++) printf(&quot;%-18d %-28s %-10.2f&quot;,i+1,array[i].name,array[i].averageMark); }
  • 22. FILE* writeStructFile(STUDENT *array,short n) { FILE *fp; char fname[15]; short i; puts(&quot;Entrer le nom du fichier pour ecrire&quot;); fflush(stdin); gets(fname); fp=fopen(fname,&quot;w+b&quot;); if(fp==NULL) { printf( &quot;Problem d'ouverture&quot; ); exit(1); } for(i=0;i<n;i++) fwrite(&array[i],sizeof(STUDENT),1,fp); return(fp); }
  • 23. void sortArray(STUDENT *array,short n) { short k; printf(&quot;Choix du tri :0 - par nom;1 - par note moyenne&quot;); fflush(stdin); scanf(&quot;%d&quot;,&k); qsort((void*)array,n,sizeof(STUDENT),k?sortAverageMark:sortName); } ////////////////////////////////////////////////////////////////////////////// int sortName(const void *pa,const void *pb) { char x; STUDENT*a=(STUDENT*)pa; STUDENT*b=(STUDENT*)pb; x=strcmp(a->name,b->name); if(x>0) return 1; else if(x<0) return -1; else return 0; }
  • 24. int sortAverageMark(const void *pa,const void *pb) { STUDENT*a=(STUDENT*)pa; STUDENT*b=(STUDENT*)pb; if(a->averageMark > b->averageMark) return 1; else if(a->averageMark < b->averageMark) return -1; else return 0; }
  • 25. void SearchFile(FILE *fp) { FILE *fp_new; char fname_new[15]; STUDENT temp; rewind(fp); puts(&quot;Entrer le nom du fichier pour ecrire&quot;); fflush(stdin); gets(fname_new); fp_new=fopen(fname_new,&quot;w+b&quot;); if(fp_new==NULL) { printf( &quot;Problem problem d'ouverture&quot; ); exit(1); } while(fread(&temp,sizeof(STUDENT),1,fp)==1) if(temp.averageMark >= 5.50) fwrite(&temp,sizeof(STUDENT),1,fp_new); rewind(fp_new); while(fread(&temp,sizeof(STUDENT),1,fp_new)==1) printf(&quot;%-28s %-10.2f&quot;,temp.name,temp.averageMark); fclose(fp); fclose(fp_new); }
  • 26.  
  • 27.  
  • 28.  
  • 29.  
  • 30.  
  • 31.  
  • 32.
  • 33. ppch[0] word0 ppch[1] ppch[2] word1 word2 ppch[i] ppch[n] wordn char **ppch
  • 34. #include <stdio.h> #include <string.h> #include <stdlib.h> #define NUM 2 void prt(char **ppch,int n){ int i; for(i=0;i<n;i++){ printf(&quot;%s&quot;,ppch[i]); } } int exist(char **ppch, int n, char *w){ int i; for(i=0; i<n;i++){ if(!strcmp(ppch[i],w)) return 1; } return 0; }
  • 35. char ** insert(char **ppch, int *n_w,int *n_el, char *w){ char *p; if ((p = (char *)malloc(sizeof(char)*(strlen(w)+1)))== NULL){ return NULL; } strcpy(p, w); if(*n_w == *n_el){ *n_el+=NUM; if ( (ppch = (char **)realloc( ppch,*n_el*sizeof(char*)))==NULL) { return NULL; } } ppch[(*n_w)++]= p; return ppch; }
  • 36. void sort(char **ppch, int n_w){ int i,ok; char *help; do { ok=1; for(i=0;i<n_w-1;i++){ if(strcmp(ppch[i],ppch[i+1])>0){ help=ppch[i]; ppch[i]=ppch[i+1]; ppch[i+1]=help; ok=0; } } }while (!ok); } void free_m(char **ppch,int n_words){ int i; for(i=0;i<n_words;i++){ free(ppch[i]); } free(ppch); }
  • 37. int main(){ int n_words=0, n_el=NUM ; char buf[251], **ppch; FILE *f; if((f=fopen(&quot;b.txt&quot;,&quot;rt&quot;))==NULL){ printf (&quot;The file cannot be read&quot;); return 2; } if ( (ppch = (char **)malloc( n_el*sizeof(char*)))== NULL){ printf(&quot;No memory&quot;); return 3; } while(!feof(f)) { if( fscanf(f,&quot;%250s&quot;,buf) == EOF){ break; } if(!exist(ppch,n_words,buf)){ if((ppch=insert(ppch, &n_words,&n_el,buf))==NULL){ printf(&quot;No memory&quot;); return 3; } } } sort(ppch, n_words); prt(ppch, n_words); free_m(ppch,n_words); return 0; } alfa alfa tita gama beta alfa beta gama tita