Contenu connexe Similaire à Lect14 dev2 (20) Lect14 dev21. 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) 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("%d chaines (noms) ont lus",n_l); printf("Liste originale des noms:"); output(ptext,n_l); sort(ptext,n_l); printf("Liste des nom tries:"); 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("test.txt", "r")) == NULL) { printf("Fichier n'est pas ouvert."); exit(1); } if((ptext=(char **) malloc ( n_el *sizeof(char *)))==NULL){ printf("Erreur!"); exit(1); } 13. do{ if( fgets(buf,sizeof(buf),fp)== NULL){ printf("Erreur lecture!"); exit(1); } if((p=(char*)malloc( sizeof(char)*(strlen(buf)+1)))== NULL){ printf("Erreur!"); exit(1); } strcpy(p,buf); if(*n_l==n_el) { n_el+=NUM ; if (ptext=(char**) realloc (ptext, n_el *sizeof(char*)))==NULL){ printf("Erreur!"); exit(1); } } ptext[(*n_l)++]=p; } while(!feof(fp)); strcat(*(ptext+*n_l-1),""); return ptext; } 14. void output(char **p,int k) { int j; for(j=0;j<k;j++) printf("%s",*(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); } 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("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 :"); scanf("%d",&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("La structure suivante dans le tableau? - (oui/no)"), fflush(stdin),strcmp(gets(word),"oui")==0) { if(*i == n) { n=n*2; array=(STUDENT*)realloc(array,n*sizeof(STUDENT)); if(array == NULL) exit(1); } printf("ETUDIANT NOMBRE %d: ",*i+1); inputStudent(&a); memcpy(&array[*i],&a,sizeof(STUDENT)); (*i)++; } return array; } 21. void inputStudent(STUDENT *a) { fflush(stdin); printf("Entrer le nom:"); gets(a->name); printf("Entrer la note moyenne:"); scanf("%f",&a->averageMark); } ////////////////////////////////////////////////////////////////////////////////////////// void printArray(STUDENT *array,short n) { int i; printf("Les elements du tableau dynamique ETUDIANT NOMBRE ETUDIANT NOM ETUDIANT NOTE MOYENNE"); for(i=0;i<n;i++) printf("%-18d %-28s %-10.2f",i+1,array[i].name,array[i].averageMark); } 22. FILE* writeStructFile(STUDENT *array,short n) { FILE *fp; char fname[15]; short i; puts("Entrer le nom du fichier pour ecrire"); fflush(stdin); gets(fname); fp=fopen(fname,"w+b"); if(fp==NULL) { printf( "Problem d'ouverture" ); 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("Choix du tri :0 - par nom;1 - par note moyenne"); fflush(stdin); scanf("%d",&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("Entrer le nom du fichier pour ecrire"); fflush(stdin); gets(fname_new); fp_new=fopen(fname_new,"w+b"); if(fp_new==NULL) { printf( "Problem problem d'ouverture" ); 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("%-28s %-10.2f",temp.name,temp.averageMark); fclose(fp); fclose(fp_new); } 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("%s",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("b.txt","rt"))==NULL){ printf ("The file cannot be read"); return 2; } if ( (ppch = (char **)malloc( n_el*sizeof(char*)))== NULL){ printf("No memory"); return 3; } while(!feof(f)) { if( fscanf(f,"%250s",buf) == EOF){ break; } if(!exist(ppch,n_words,buf)){ if((ppch=insert(ppch, &n_words,&n_el,buf))==NULL){ printf("No memory"); 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