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)

Lect14 dev2

  • Upload
    moisko

  • View
    190

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lect14 dev2

INFORMATIQUE III

DEVOIR SURVEIEE 2Notes18 - 20 p. (excellent 6); 15 – 17 p. (très bien 5);12 – 14 p. (bien 4); 10 – 11 p. (passable 3)

Page 2: Lect14 dev2

I. (6 points) Indiquer précisément les affichages produits par les programmes suivants:

1.

#include <stdio.h>#include <string.h>#define N 3typedef struct el { int inf; char name[15]; }unit;unit f(unit a);int main(){

unit lst[N], *lstp[N];char p[]="Paris Sofia", x,y;for(x=0; x<N; x++){

lst[x].inf=x+1; strcpy(lst[x].name,p+x); printf("1: %s\n",lst[x].name+2); lstp[x]=lst+x;

}

Page 3: Lect14 dev2

I. (6 points) Indiquer précisément les affichages produits par les programmes suivants:

1.lstp[0]=lstp[2];for(x=0; x<N; x++){

printf("2:%d %s\n",lstp[x]->inf, lstp[x]->name);

}*lstp[1]=f(*lstp[0]); printf("4:%d %s\n",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("3:%d %s\n",a.inf,a.name);return b;

}

Page 4: Lect14 dev2

Explications

unit *lstp[3] unit lst[3]

inf name

1 Paris Sofia

2 aris Sofia

3 ris Sofia

Page 5: Lect14 dev2

Explications1. lst 1 Paris Sofia 2 aris Sofia 3 ris Sofia

lst[x].name+2

1: ris Sofia1: is Sofia1: s Sofia2. lstp[0]=lstp[2];2: 3 ris Sofia2: 2 aris Sofia2: 3 ris Sofia

Page 6: Lect14 dev2

Explications3. *lstp[0] (3 ris Sofia) - le paramètre

a.inf –> 3*2

3: 6 ris Sofia

4. b.inf –> 2*(a.inf)+1 -> 13a.name -> ris Sofia

b.name -> a.name+44: 13 Sofia

Page 7: Lect14 dev2

I. (6 points) Indiquer précisément les affichages produits par les programmes suivants:

2.

#include <stdio.h>int gcd(int, int);int main(){ int a=33, b=15; if (a < 1 || b < 1) { printf("Erreur! \n");

return 1; } printf("%d\n",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);}

Page 8: Lect14 dev2

Explicationsa b

1 33 > 15

2 18 > 15

3 3 < 15

4 3 < 12

5 3 < 9

6 3 < 6

7 3 3

Page 9: Lect14 dev2

• Ecrire un programme qui lit un fichier texte donné test.txt où sont enregistrés les noms et prénoms des étudiants (nombre inconnue). Lire et déplacer dans un tableau dynamique (nombre initial des éléments est=2) les éléments lu. Trier les noms en ordre alphabétique. Afficher les noms lus et les noms après le tri.

• Faire les manipulations en utilisant les fonctions différentes :

– Insertion dans le tableau dynamique;– Affichage du tableau dynamique;– Tri du tableau dynamique

Page 10: Lect14 dev2

• fichier texte donné: test.txt où sont enregistrés les noms et prénoms (nombre inconnue).

Maria AngelovaAni KolevaIvan PetrovAsen Milanov

Page 11: Lect14 dev2

#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",n_l); printf("\nListe originale des noms:\n"); output(ptext,n_l); sort(ptext,n_l); printf("Liste des nom tries:\n"); output(ptext,n_l); free(ptext); return(0); }

Page 12: Lect14 dev2

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.\n"); exit(1);

}

if((ptext=(char **)malloc(n_el*sizeof(char *)))==NULL){ printf("Erreur!\n"); exit(1);

}

Page 13: Lect14 dev2

do{ if( fgets(buf,sizeof(buf),fp)== NULL){

printf("Erreur lecture!\n");exit(1);

} if((p=(char*)malloc( sizeof(char)*(strlen(buf)+1)))== NULL){

printf("Erreur!\n"); 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!\n"); exit(1);

} } ptext[(*n_l)++]=p; } while(!feof(fp)); strcat(*(ptext+*n_l-1),"\n"); return ptext;}

Page 14: Lect14 dev2

void output(char **p,int k){ int j; for(j=0;j<k;j++)

printf("%s\n",*(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);}

Page 15: Lect14 dev2
Page 16: Lect14 dev2

• Ecrire un programme de faire les manipulations avec un fichier binaire des structures.

• Faire les manipulations en utilisant les fonctions différentes :

– Insertion dans le tableau dynamique en utilisant le clavier;

– Affichage du tableau dynamique;– Création du fichier binaire;– Tri du tableau dynamique;

• Selon les noms• Selon les notes

– Chercher dans le fichier les étudiants avec la note moyenne >=5.50 et déplacer dans un autre fichier

Page 17: Lect14 dev2

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <memory.h>#define NBR 5typedef 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 *);

Page 18: Lect14 dev2

int main(){

myMenu();return 0;

}/////////////////////////////////////////////////void myMenu(){ FILE *fp; short a,i; STUDENT *array; do { printf("\n1.Creer tableau dynamique des structures - entrees par clavier\

\n2.Afficher le tableau\ \n3.Ecrire le tableau dans un fichier binaire\ \n4.Trier le tableau dynamique des structures\ \n5.Chercher dans le fichier les etudiants avec notes >=5.50&ecrire\ \n6.Fin\ \nVotre choix :"); scanf("%d",&a);

Page 19: Lect14 dev2

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);

}

Page 20: Lect14 dev2

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("\nETUDIANT NOMBRE %d: ",*i+1); inputStudent(&a); memcpy(&array[*i],&a,sizeof(STUDENT)); (*i)++;}return array;

}

Page 21: Lect14 dev2

void inputStudent(STUDENT *a){ fflush(stdin);

printf("\nEntrer le nom:");gets(a->name);printf("Entrer la note moyenne:");scanf("%f",&a->averageMark);

}

//////////////////////////////////////////////////////////////////////////////////////////void printArray(STUDENT *array,short n){ int i; printf("\t\t\tLes elements du tableau dynamique\

\nETUDIANT NOMBRE\t ETUDIANT NOM \t\t ETUDIANT NOTE MOYENNE\n");

for(i=0;i<n;i++) printf("%-18d %-28s %-10.2f\n",i+1,array[i].name,array[i].averageMark);}

Page 22: Lect14 dev2

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\n" );exit(1);

}

for(i=0;i<n;i++)fwrite(&array[i],sizeof(STUDENT),1,fp);

return(fp);}

Page 23: Lect14 dev2

void sortArray(STUDENT *array,short n){ short k; printf("\nChoix du tri :\n0 - par nom;\n1 - par note moyenne\n"); 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;

elsereturn 0;

}

Page 24: Lect14 dev2

int sortAverageMark(const void *pa,const void *pb){

STUDENT*a=(STUDENT*)pa;STUDENT*b=(STUDENT*)pb;

if(a->averageMark > b->averageMark)return 1;

elseif(a->averageMark

< b->averageMark)return -1;

elsereturn 0;

}

Page 25: Lect14 dev2

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\n" );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\n",temp.name,temp.averageMark);fclose(fp);fclose(fp_new);

}

Page 26: Lect14 dev2
Page 27: Lect14 dev2
Page 28: Lect14 dev2
Page 29: Lect14 dev2
Page 30: Lect14 dev2
Page 31: Lect14 dev2
Page 32: Lect14 dev2

• Ecrire un programme qui lit un fichier texte donné b.txt où sont enregistrés les mots séparées par un ou plusieurs blancs ou nouvelle ligne (nombre inconnue). Lire les mots et si le mot ne présente pas dans un dictionnaire, déplacer le dans un tableau dynamique (dictionnaire) (nombre initial des éléments est=2). Trier les mots en ordre alphabétique. Afficher le dictionnaire après le tri des mots.

• Faire les manipulations en utilisant les fonctions différentes.

Page 33: Lect14 dev2

ppch[0] word0

ppch[1]

ppch[2]

word1

word2

ppch[i]

ppch[n]wordn

char **ppch

Page 34: Lect14 dev2

#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\n",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;}

Page 35: Lect14 dev2

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;}

Page 36: Lect14 dev2

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);}

Page 37: Lect14 dev2

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\n"); return 2; }if ( (ppch = (char **)malloc( n_el*sizeof(char*)))== NULL){printf("No memory\n"); 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\n");return 3;}

}}sort(ppch, n_words);prt(ppch, n_words);free_m(ppch,n_words);return 0;}

alfa alfa titagama beta

alfabetagamatita