SlideShare une entreprise Scribd logo
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

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)
rezgui mohamed
 
Scala : programmation fonctionnelle
Scala : programmation fonctionnelleScala : programmation fonctionnelle
Scala : programmation fonctionnelle
MICHRAFY MUSTAFA
 
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
 
Seance 4- Programmation en langage C
Seance 4- Programmation en langage CSeance 4- Programmation en langage C
Seance 4- Programmation en langage C
Fahad Golra
 
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
Microsoft
 
Résumé Algorithme et Programmation
Résumé Algorithme et ProgrammationRésumé Algorithme et Programmation
Résumé Algorithme et Programmation
borhen boukthir
 
Chap2fonctionscpp
Chap2fonctionscppChap2fonctionscpp
Chap2fonctionscpp
Aziz Darouichi
 
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
Microsoft
 
Polymorphisme, interface et classe abstraite
Polymorphisme, interface et classe abstraitePolymorphisme, interface et classe abstraite
Polymorphisme, interface et classe abstraite
ECAM Brussels Engineering School
 
Exercices pascal tous les chapitres
Exercices pascal tous les chapitresExercices pascal tous les chapitres
Exercices pascal tous les chapitres
borhen boukthir
 
Chapitre4: Pointeurs et références
Chapitre4: Pointeurs et références Chapitre4: Pointeurs et références
Chapitre4: Pointeurs et références
Aziz Darouichi
 
Chapitre2fonctionscppv2019
Chapitre2fonctionscppv2019Chapitre2fonctionscppv2019
Chapitre2fonctionscppv2019
Aziz Darouichi
 
Type abstrait de données
Type abstrait de donnéesType abstrait de données
Type abstrait de données
ECAM Brussels Engineering School
 
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
Benoit Lopez
 
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)
Stéphanie Hertrich
 
Corrige exercices pascal_fenni_2018
Corrige exercices pascal_fenni_2018Corrige exercices pascal_fenni_2018
Corrige exercices pascal_fenni_2018
salah fenni
 
09 big data mapreduce
09 big data mapreduce09 big data mapreduce
09 big data mapreduce
Patrick Bury
 

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

Transformaciones lineales
Transformaciones linealesTransformaciones lineales
Transformaciones lineales
Carlos Zambrano
 
Waardig Organiseren Artikel
Waardig Organiseren ArtikelWaardig Organiseren Artikel
Waardig Organiseren Artikel
Albert Jan Stam
 
Camilacjd
CamilacjdCamilacjd
Certingresos1
Certingresos1Certingresos1
Certingresos1
iejcg
 
Conexión a bases de datos
Conexión a bases de datosConexión a bases de datos
Conexión a bases de datos
Uniminuto - San Francisco
 
C:\Fakepath\Christie
C:\Fakepath\ChristieC:\Fakepath\Christie
C:\Fakepath\Christie
Nerissaemerald
 
Estrategias competitivas básicas
Estrategias competitivas básicasEstrategias competitivas básicas
Estrategias competitivas básicas
LarryJimenez
 

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

DartttttttttttttttttttttttversionFinal.pdf
DartttttttttttttttttttttttversionFinal.pdfDartttttttttttttttttttttttversionFinal.pdf
DartttttttttttttttttttttttversionFinal.pdf
zoulaikhibenaachourn
 
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
Loic Yon
 
C++ 11/14
C++ 11/14C++ 11/14
C++ 11/14
Alexandre Hamez
 
Algorithmique Amp Programmation (R Sum
Algorithmique  Amp  Programmation (R SumAlgorithmique  Amp  Programmation (R Sum
Algorithmique Amp Programmation (R Sum
Amy Isleb
 
09 big data mapreduce
09 big data mapreduce09 big data mapreduce
09 big data mapreduce
Patrick Bury
 
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)
Arnaud ASSAD ✅ Expert Open Source
 
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
c79024186
 
Chap 2--POO avec JAVA.pdf
Chap 2--POO avec JAVA.pdfChap 2--POO avec JAVA.pdf
Chap 2--POO avec JAVA.pdf
ramadanmahdi
 
Algo poo ts
Algo poo tsAlgo poo ts
Algo poo ts
mohamed El babili
 
Tour C++
Tour C++Tour C++
Tour C++
Sais Abdelkrim
 
cours-5.1.pdf
cours-5.1.pdfcours-5.1.pdf
cours-5.1.pdf
GonnaBe1
 
TAD (1).pptx
TAD (1).pptxTAD (1).pptx
TAD (1).pptx
SergeOngolo
 
C++11 en 12 exemples simples
C++11 en 12 exemples simplesC++11 en 12 exemples simples
C++11 en 12 exemples simples
Pethrvs
 
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
kemenaran
 
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
Abdelouahed Abdou
 
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
Thierry Gayet
 
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
Loic Yon
 
resume algo 2023.pdf
resume algo 2023.pdfresume algo 2023.pdf
resume algo 2023.pdf
salah fenni
 

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

Burkina Faso library newsletter May 2024
Burkina Faso library newsletter May 2024Burkina Faso library newsletter May 2024
Burkina Faso library newsletter May 2024
Friends of African Village Libraries
 
Formation Intelligence Artificielle pour dirigeants- IT6-DIGITALIX 24_opt OK_...
Formation Intelligence Artificielle pour dirigeants- IT6-DIGITALIX 24_opt OK_...Formation Intelligence Artificielle pour dirigeants- IT6-DIGITALIX 24_opt OK_...
Formation Intelligence Artificielle pour dirigeants- IT6-DIGITALIX 24_opt OK_...
cristionobedi
 
Cours de conjugaison des verbes du premier, deuxième et troisième groupe
Cours de conjugaison des verbes du premier, deuxième et troisième groupeCours de conjugaison des verbes du premier, deuxième et troisième groupe
Cours de conjugaison des verbes du premier, deuxième et troisième groupe
Yuma91
 
Iris van Herpen. pptx
Iris         van         Herpen.      pptxIris         van         Herpen.      pptx
Iris van Herpen. pptx
Txaruka
 
Iris et les hommes.pptx
Iris      et         les      hommes.pptxIris      et         les      hommes.pptx
Iris et les hommes.pptx
Txaruka
 
Conseils pour Les Jeunes | Conseils de La Vie| Conseil de La Jeunesse
Conseils pour Les Jeunes | Conseils de La Vie| Conseil de La JeunesseConseils pour Les Jeunes | Conseils de La Vie| Conseil de La Jeunesse
Conseils pour Les Jeunes | Conseils de La Vie| Conseil de La Jeunesse
Oscar Smith
 
Edito-B1-francais Manuel to learning.pdf
Edito-B1-francais Manuel to learning.pdfEdito-B1-francais Manuel to learning.pdf
Edito-B1-francais Manuel to learning.pdf
WarlockeTamagafk
 
SYLLABUS DU COURS MARKETING DTS 1-2.pdf
SYLLABUS DU COURS  MARKETING DTS 1-2.pdfSYLLABUS DU COURS  MARKETING DTS 1-2.pdf
SYLLABUS DU COURS MARKETING DTS 1-2.pdf
Moukagni Evrard
 
Mémoire de licence en finance comptabilité et audit
Mémoire de licence en finance comptabilité et auditMémoire de licence en finance comptabilité et audit
Mémoire de licence en finance comptabilité et audit
MelDjobo
 
Système de gestion des fichiers de amine
Système de gestion des fichiers de amineSystème de gestion des fichiers de amine
Système de gestion des fichiers de amine
sewawillis
 
Iris van Herpen. pptx
Iris            van        Herpen.     pptxIris            van        Herpen.     pptx
Iris van Herpen. pptx
Txaruka
 
Impact des Critères Environnementaux, Sociaux et de Gouvernance (ESG) sur les...
Impact des Critères Environnementaux, Sociaux et de Gouvernance (ESG) sur les...Impact des Critères Environnementaux, Sociaux et de Gouvernance (ESG) sur les...
Impact des Critères Environnementaux, Sociaux et de Gouvernance (ESG) sur les...
mrelmejri
 

Dernier (12)

Burkina Faso library newsletter May 2024
Burkina Faso library newsletter May 2024Burkina Faso library newsletter May 2024
Burkina Faso library newsletter May 2024
 
Formation Intelligence Artificielle pour dirigeants- IT6-DIGITALIX 24_opt OK_...
Formation Intelligence Artificielle pour dirigeants- IT6-DIGITALIX 24_opt OK_...Formation Intelligence Artificielle pour dirigeants- IT6-DIGITALIX 24_opt OK_...
Formation Intelligence Artificielle pour dirigeants- IT6-DIGITALIX 24_opt OK_...
 
Cours de conjugaison des verbes du premier, deuxième et troisième groupe
Cours de conjugaison des verbes du premier, deuxième et troisième groupeCours de conjugaison des verbes du premier, deuxième et troisième groupe
Cours de conjugaison des verbes du premier, deuxième et troisième groupe
 
Iris van Herpen. pptx
Iris         van         Herpen.      pptxIris         van         Herpen.      pptx
Iris van Herpen. pptx
 
Iris et les hommes.pptx
Iris      et         les      hommes.pptxIris      et         les      hommes.pptx
Iris et les hommes.pptx
 
Conseils pour Les Jeunes | Conseils de La Vie| Conseil de La Jeunesse
Conseils pour Les Jeunes | Conseils de La Vie| Conseil de La JeunesseConseils pour Les Jeunes | Conseils de La Vie| Conseil de La Jeunesse
Conseils pour Les Jeunes | Conseils de La Vie| Conseil de La Jeunesse
 
Edito-B1-francais Manuel to learning.pdf
Edito-B1-francais Manuel to learning.pdfEdito-B1-francais Manuel to learning.pdf
Edito-B1-francais Manuel to learning.pdf
 
SYLLABUS DU COURS MARKETING DTS 1-2.pdf
SYLLABUS DU COURS  MARKETING DTS 1-2.pdfSYLLABUS DU COURS  MARKETING DTS 1-2.pdf
SYLLABUS DU COURS MARKETING DTS 1-2.pdf
 
Mémoire de licence en finance comptabilité et audit
Mémoire de licence en finance comptabilité et auditMémoire de licence en finance comptabilité et audit
Mémoire de licence en finance comptabilité et audit
 
Système de gestion des fichiers de amine
Système de gestion des fichiers de amineSystème de gestion des fichiers de amine
Système de gestion des fichiers de amine
 
Iris van Herpen. pptx
Iris            van        Herpen.     pptxIris            van        Herpen.     pptx
Iris van Herpen. pptx
 
Impact des Critères Environnementaux, Sociaux et de Gouvernance (ESG) sur les...
Impact des Critères Environnementaux, Sociaux et de Gouvernance (ESG) sur les...Impact des Critères Environnementaux, Sociaux et de Gouvernance (ESG) sur les...
Impact des Critères Environnementaux, Sociaux et de Gouvernance (ESG) sur les...
 

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