Savez vous ce que ce programme de bourrain fait ?
Les commentaires devraient vous mettre sur la voie... mais bonne chance qd mm...
#include <conio.h>
#include <process.h>
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <time.h>
#define MAXPATHLEN 200
#define NBMAX 20
char** lire_fic(char **tab, int *nbelt, int *tab_leng, int *tab_adr, char *chemin);
char** saisir_donnees( char **tab, int *nbelt, int *tab_leng, int *tab_adr);
char** trier_donnees(char **tab, int nbelt, int *tab_leng);
void ecrire_donnees(char **tab, int nbelt, int *tab_leng, char *chemin);
void aff_donnees(char **tab, int nbelt, int *tab_leng);
void aff_fichier(char *chemin);
void main(void) { int ok=0,x=0,nbelt=0;
char **tab=NULL;
int *tab_leng=NULL;
int tab_adr=0;
char chemin[MAXPATHLEN]={0};
system("cls" );
while(!ok)
{
x=0;
printf("\nVoulez vous :" ); printf("\n\t1 - lire un fichier" ); printf("\n\t2 - saisir des donnees au clavier." ); printf("\n\t3 - trier les donnees." ); printf("\n\t4 - ecrire les donnees dans le fichier" ); printf("\n\t5 - afficher les donnees du tableau" ); printf("\n\t6 - afficher les donnees du fichier" ); printf("\n\t7 - quitter le programme\n" ); fflush(stdin);
scanf("%d",&x);
system("cls" );
switch(x)
{
case 1: tab = lire_fic(tab,&nbelt,tab_leng, &tab_adr, chemin); tab_leng=(int*)tab_adr;
break;
case 2 : tab = saisir_donnees(tab,&nbelt, tab_leng, &tab_adr); tab_leng=(int*)tab_adr;
break;
case 3 : tab = trier_donnees(tab,nbelt, tab_leng);
break;
case 4 : ecrire_donnees(tab,nbelt,tab_leng,chemin);
break;
case 5 : aff_donnees(tab, nbelt, tab_leng);
break;
case 6 : aff_fichier(chemin);
break;
case 7: ok=1;
break;
default: printf("\n veuillez rentrer une valeur correcte\n" );
system("pause" );
break;
} }
} char** lire_fic(char **tab, int *nbelt, int *tab_leng, int *tab_adr, char *chemin)
{
//il s'agit ici de lire un fichier texte a partir d'un chemin et d'en sortir un tableau de char
//contenant les valeurs lus et un autre tableau contenant la longeuru de chaque ligne.
//la variable tab_adr n'est ici que pour simplifier l'ecriture et eviter de transporter des triples pointeurs.
int ok=0,i=0,j=0,k=0;
char ligne[NBMAX];
FILE *fichier=NULL;
free(tab);
free(tab_leng);
tab_leng=NULL;
tab=NULL;
do //on boucle jusqu'a ce que le fichier est put etre ouvert
{
fflush(stdin);
printf("entrer le chemin du fichier que vous voullez ouvrir : \n" );
gets(chemin);
if(chemin[0] != 0)
{
if( (fichier=fopen(chemin,"r" )) != NULL) ok=1;
}
} while (ok == 0);
j=0;
//on test si le fichier est correste c'est a dire qu'il n'y est pas de texte.
//si c bon on creer notre tableau dynamiqument
while(fgets(ligne, NBMAX, fichier) != NULL)
{
i=0;
while(ligne[i] != 10)
{
if( ligne[i]<48 || ligne[i]>57)
{
if(ligne[i]!=45)
{
printf("ce n'est pas le bon fichier!" );
break;
}
}
i++;
}
if( (tab = (char**)realloc(tab,(j+1)*sizeof(char*))) == NULL ) {
printf("Probleme d'allocation" );
break;
}
if( (tab[j] = (char*)malloc(i*sizeof(char))) == NULL )
{
printf("Probleme d'allocation" );
break;
}
for(k=0;k<i;k++)
{
tab[j][k]=ligne[k];
}
if( (tab_leng=(int*)realloc(tab_leng, (j+1)*sizeof(int))) == NULL )
{
printf("probleme d'allocation" );
break;
}
tab_leng[j]=k;
j++;
} *nbelt=j;
fclose(fichier);
*tab_adr=(int)tab_leng;
return(tab);
}
char** saisir_donnees( char **tab, int *nbelt, int *tab_leng, int *tab_adr)
{
int i=0,j=0,ok=1,k=0,leng=0;
char val[NBMAX]={0};
free(tab);
free(tab_leng);
tab_leng=NULL;
tab=NULL;
while(ok) //on boucle jusqu'a ce que l'utilisateur rnetre 'f'
{
fflush(stdin);
printf("\nRentrer une valeur (taper f pour quitter) : \n" );
gets(val);
if( (val[0] == 'f'
|| (val[0] == 'F'
)
ok=0;
else
{
j=0;
leng=strlen(val);
//on test si la valeur est correste
for(j=0;j<leng;j++)
{
if(val[j]<48 || val[j]>57)
{
if(val[j]!=45)
{
printf("ce n'est pas une valeur correcte!" );
break;
}
}
}
if( (tab = (char**)realloc(tab,(i+1)*sizeof(char*))) == NULL ) {
printf("Probleme d'allocation" );
break;
}
if( (tab[i] = (char*)malloc(leng*sizeof(char))) == NULL )
{
printf("Probleme d'allocation" );
break;
}
k=0;
for(k=0;k<leng;k++)
tab[i][k]=val[k];
if( (tab_leng=(int*)realloc(tab_leng, (i+1)*sizeof(int))) == NULL )
{
printf("probleme d'allocation" );
break;
}
tab_leng[i]=k;
i++;
}
} *nbelt=i;
*tab_adr=(int)tab_leng;
return(tab);
}
char** trier_donnees(char **tab, int nbelt, int *tab_leng)
{
int i=0,t=0,j=0,k=0,ok=0,x=0, y=0, z=0, a=0, diff=0; unsigned long *tabul=NULL, tmpul=0, *tmp_tabul=NULL, nbmax_ul=0;
signed long *tabld=NULL, tmpld=0, *tmp_tabld=NULL, nbmax_ld=0;
int val[NBMAX]={0};
int tablong[10]={0}, **tab_ind=NULL;
char *pt_tmp=NULL, **tmp_tab=NULL;
int tmp=0, nbmax=0, longmax=0, compt=0, *tmp_tab_leng=NULL;
//on regarde de quelle type sont les donnees, c'est a dire signed ou unsigned long
//pour cela on regarde si le signe '-' apparait for(i=0;i<nbelt;i++)
{
if(tab[i][0]==45) t=1;
}
//choix des differentes methodes de tri
while(!ok)
{
fflush(stdin);
system("cls" );
printf("Quel type de tri voulez vous effectuer :" );
printf("\n\t1 - Bucket sort" );
printf("\n\t2 - tri a bulles" );
printf("\n\t3 - tri par extraction\n" );
scanf("%d", &x);
if( (x<4) && (x!=0) );
ok=1;
}
if(t==1) //le type est signed long
{
if( (tabld= (signed long*)malloc(nbelt*sizeof( signed long ))) == NULL )
{
printf("probleme d'allocation" );
}
for(i=0;i<nbelt;i++)
{
sscanf(tab[i], "%ld", val);
tabld[i]=val[0];
}
//on creer un tableau du bon type sur lequel on va effectuer nos calculs.
switch(x)
{
case 1 : //bucket sort
tmp=0;
free(tmp_tabld);
free(tmp_tab_leng);
free(tmp_tab);
tmp_tabld=NULL;
tmp_tab_leng=NULL;
tmp_tab=NULL;
if( (tmp_tabld= (signed long*)malloc(nbelt*sizeof( signed long ))) == NULL )
{
printf("probleme d'allocation" );
}
if( (tmp_tab_leng = (int*)malloc(nbelt*sizeof(int))) == NULL)
{
printf("probleme d'allocation" );
}
if( (tmp_tab = (char**)malloc(nbelt*sizeof(char*))) == NULL)
{
printf("probleme d'allocation" );
}
for(j=0;j<nbelt;j++)
{
if( (tmp_tab[j] = (char*)malloc(tab_leng[j]*sizeof(char))) == NULL)
{
printf("probleme d'allocation" );
}
} //on place tous les nombres positifs dans le haut du tableau
for(i=0;i<nbelt;i++)
{
if(tab[i][0]!='-'
{
tmp_tab[tmp]=tab[i];
tmp_tabld[tmp]=tabld[i];
tmp_tab_leng[tmp]=tab_leng[i];
tmp++;
}
}
diff=tmp;
//puis tous les nombres negatifs dans le bas du tableau cela revient en fait a creer 2 tableau!
for(i=0;i<nbelt;i++)
{
if(tab[i][0]=='-'
{
tmp_tab[tmp]=tab[i];
tmp_tabld[tmp]=tabld[i];
tmp_tab_leng[tmp]=tab_leng[i];
tmp++;
}
}
for(i=0;i<nbelt;i++)
{
tab[i]=tmp_tab[i];
tabld[i]=tmp_tabld[i];
tab_leng[i]=tmp_tab_leng[i];
}
//on travail ici sur la premiere moitie du tableau celle des nombres positifs
for(i=0;i<diff;i++) // on cherche le nombre maximum
{
if( nbmax_ld<tabld[i])
nbmax_ld=tabld[i];
}
i=0;
while( (nbmax_ld % (signed long) (pow(10,i))) != nbmax_ld ) i++; //puis on calcul son nombre de characteres
longmax=i;
for(i=0;i<longmax;i++)
{
free(tab_ind);
tab_ind=NULL;
for(j=0;j<10;j++)
tablong[j]=0;
if( (tab_ind = (int**)malloc(10*sizeof(int*))) == NULL)
{
printf("probleme d'allocation" );
break;
}
for(tmp=0;tmp<10;tmp++)
tab_ind[tmp]=NULL;
for(j=0;j<diff;j++)
{
x = (int)( (tabld[j] % (signed long)(pow(10,i+1))) / pow(10,i) );
//x contient le ieme digit du nobre
tablong[x]++;
if( (tab_ind[x] = (int*)realloc(tab_ind[x],(tablong[x])*sizeof(int))) == NULL)
{
printf("probleme d'allocation" );
break;
}
tab_ind[x][tablong[x]-1]=j; //tab_ind n contient pas les valeurs des nombres mais leurs indices dans tab!
}
// il ne reste plus qu'a changer les nombres de place dans tab en respectant l'ordre donner par tab_ind
free(tmp_tabld);
free(tmp_tab_leng);
free(tmp_tab);
tmp_tabld=NULL;
tmp_tab_leng=NULL;
tmp_tab=NULL;
if( (tmp_tabld= (signed long*)malloc(nbelt*sizeof( signed long ))) == NULL )
{
printf("probleme d'allocation" );
}
if( (tmp_tab_leng = (int*)malloc(nbelt*sizeof(int))) == NULL)
{
printf("probleme d'allocation" );
}
if( (tmp_tab = (char**)malloc(nbelt*sizeof(char*))) == NULL) {
printf("probleme d'allocation" );
}
for(j=0;j<diff;j++)
{
if( (tmp_tab[j] = (char*)malloc(tab_leng[j]*sizeof(char))) == NULL)
{
printf("probleme d'allocation" );
}
} compt=0;
for(j=9;j>-1;j--)
{
for(k=0;k<tablong[j];k++)
{
tmp_tabld[compt]=tabld[tab_ind[j][k]];
tmp_tab[compt]=tab[tab_ind[j][k]];
tmp_tab_leng[compt]=tab_leng[tab_ind[j][k]];
compt++;
}
}
for(j=0;j<diff;j++)
{
tabld[j]=tmp_tabld[j];
tab[j]=tmp_tab[j];
tab_leng[j]=tmp_tab_leng[j];
}
}
//on refait la meme chose mais avec les nombres negatifs //pour cela on fait comme si ils etaient positifs mais on les ranges dans l'ordre inverse (croissant/decroissant)
//le principe de ctte partie etant deja commenter au dessus elle ne le seras pas ici.
for(i=diff;i<nbelt;i++)
{
if( nbmax_ld<-1*tabld[i])
nbmax_ld=-1*tabld[i];
}
i=0;
while( (nbmax_ld % (signed long) (pow(10,i))) != nbmax_ld ) i++;
longmax=i;
for(i=0;i<longmax;i++)
{
free(tab_ind);
tab_ind=NULL;
for(j=0;j<10;j++)
tablong[j]=0;
if( (tab_ind = (int**)malloc(10*sizeof(int*))) == NULL)
{
printf("probleme d'allocation" );
break;
}
for(tmp=0;tmp<10;tmp++)
tab_ind[tmp]=NULL;
for(j=diff;j<nbelt;j++)
{
x = -1*(int)( (tabld[j] % (signed long)(pow(10,i+1))) / pow(10,i) );
tablong[x]++;
if( (tab_ind[x] = (int*)realloc(tab_ind[x],(tablong[x])*sizeof(int))) == NULL)
{
printf("probleme d'allocation" );
break;
}
tab_ind[x][tablong[x]-1]=j;
}
free(tmp_tabld);
free(tmp_tab_leng);
free(tmp_tab);
tmp_tabld=NULL;
tmp_tab_leng=NULL;
tmp_tab=NULL;
if( (tmp_tabld= (signed long*)malloc(nbelt*sizeof( signed long ))) == NULL )
{
printf("probleme d'allocation" );
}
if( (tmp_tab_leng = (int*)malloc(nbelt*sizeof(int))) == NULL)
{
printf("probleme d'allocation" );
}
if( (tmp_tab = (char**)malloc(nbelt*sizeof(char*))) == NULL) {
printf("probleme d'allocation" );
}
for(j=0;j<nbelt;j++)
{
if( (tmp_tab[j] = (char*)malloc(tab_leng[j]*sizeof(char))) == NULL)
{
printf("probleme d'allocation" );
}
} compt=0;
for(j=0;j<10;j++)
{
for(k=0;k<tablong[j];k++)
{
tmp_tabld[compt]=tabld[tab_ind[j][k]];
tmp_tab[compt]=tab[tab_ind[j][k]];
tmp_tab_leng[compt]=tab_leng[tab_ind[j][k]];
compt++;
}
}
for(j=diff;j<nbelt;j++)
{
tabld[j]=tmp_tabld[j-diff];
tab[j]=tmp_tab[j-diff];
tab_leng[j]=tmp_tab_leng[j-diff];
}
}
break;
i=0;
//tri a bulle identique pour les nombres negatifs et positifs
case 2 : i=nbelt;
while(i>0)
{
for(j=0;j<i-1;j++)
{
if(tabld[j]>tabld[j+1])
{
pt_tmp=tab[j];
tab[j]=tab[j+1];
tab[j+1]=pt_tmp;
tmp=tab_leng[j];
tab_leng[j]=tab_leng[j+1];
tab_leng[j+1]=tmp;
tmpld=tabld[j];
tabld[j]=tabld[j+1];
tabld[j+1]=tmpld;
}
}
i--;
}
break;
//tri par extraction identique pour les nombres negatifs et positifs
case 3 : k=0;
while (k <(nbelt-1))
{
i=k+1;
while(i<nbelt)
{
if(tabld[i]<tabld[k])
{
pt_tmp=tab[i];
tab[i]=tab[k];
tab[k]=pt_tmp;
tmp=tab_leng[i];
tab_leng[i]=tab_leng[k];
tab_leng[k]=tmp;
tmpld=tabld[i];
tabld[i]=tabld[k];
tabld[k]=tmpld;
}
i++;
}
k++;
}
break;
}
}
else //le ype est unsigned long
{
if( (tabul= (unsigned long*)malloc(nbelt*sizeof( unsigned long ))) == NULL )
{
printf("probleme d'allocation" );
}
//on recupere les valeurs avec le bon type
for(i=0;i<nbelt;i++)
{
sscanf(tab[i], "%ld", val);
tabul[i]=val[0];
}
switch(x)
{
case 1 : //bucket sort
//le principe de ctte partie etant deja commenter au dessus elle ne le seras pas ici.
for(i=0;i<nbelt;i++)
{
if( nbmax_ul<tabul[i])
nbmax_ul=tabul[i];
}
i=0;
while( (nbmax_ul % (int) (pow(10,i))) != nbmax_ul ) i++;
longmax=i;
for(i=0;i<longmax;i++)
{
free(tab_ind);
tab_ind=NULL;
for(j=0;j<10;j++)
tablong[j]=0;
if( (tab_ind = (int**)malloc(10*sizeof(int*))) == NULL)
{
printf("probleme d'allocation" );
break;
}
for(tmp=0;tmp<10;tmp++)
tab_ind[tmp]=NULL;
for(j=0;j<nbelt;j++)
{
x = (int)( (tabul[j] % (unsigned long)(pow(10,i+1))) / pow(10,i) );
tablong[x]++;
if( (tab_ind[x] = (int*)realloc(tab_ind[x],(tablong[x])*sizeof(int))) == NULL)
{
printf("probleme d'allocation" );
break;
}
tab_ind[x][tablong[x]-1]=j;
}
free(tmp_tabul);
free(tmp_tab_leng);
free(tmp_tab);
tmp_tabul=NULL;
tmp_tab_leng=NULL;
tmp_tab=NULL;
if( (tmp_tabul= (unsigned long*)malloc(nbelt*sizeof( unsigned long ))) == NULL )
{
printf("probleme d'allocation" );
}
if( (tmp_tab_leng = (int*)malloc(nbelt*sizeof(int))) == NULL)
{
printf("probleme d'allocation" );
}
if( (tmp_tab = (char**)malloc(nbelt*sizeof(char*))) == NULL) {
printf("probleme d'allocation" );
}
for(j=0;j<nbelt;j++)
{
if( (tmp_tab[j] = (char*)malloc(tab_leng[j]*sizeof(char))) == NULL)
{
printf("probleme d'allocation" );
}
} compt=0;
for(j=0;j<10;j++)
{
for(k=0;k<tablong[j];k++)
{
tmp_tabul[compt]=tabul[tab_ind[j][k]];
tmp_tab[compt]=tab[tab_ind[j][k]];
tmp_tab_leng[compt]=tab_leng[tab_ind[j][k]];
compt++;
}
}
for(j=0;j<nbelt;j++)
{
tabul[j]=tmp_tabul[j];
tab[j]=tmp_tab[j];
tab_leng[j]=tmp_tab_leng[j];
}
}
break;
i=0;
//tri a bulle
case 2 : i=nbelt;
while(i>0)
{
for(j=0;j<i-1;j++)
{
if(tabul[j]>tabul[j+1])
{
pt_tmp=tab[j];
tab[j]=tab[j+1];
tab[j+1]=pt_tmp;
tmp=tab_leng[j];
tab_leng[j]=tab_leng[j+1];
tab_leng[j+1]=tmp;
tmpld=tabul[j];
tabul[j]=tabul[j+1];
tabul[j+1]=tmpld;
}
}
i--;
}
break;
//tri par extraction
case 3 : k=0;
while (k <(nbelt-1))
{
i=k+1;
while(i<nbelt)
{
if(tabul[i]<tabul[k])
{
pt_tmp=tab[i];
tab[i]=tab[k];
tab[k]=pt_tmp;
tmp=tab_leng[i];
tab_leng[i]=tab_leng[k];
tab_leng[k]=tmp;
tmpld=tabul[i];
tabul[i]=tabul[k];
tabul[k]=tmpld;
}
i++;
}
k++;
}
break;
}
}
return(tab);
}
void ecrire_donnees(char **tab, int nbelt, int *tab_leng, char *chemin)
{
//fonction qui permet d'ecrire les donnnees triées (ou non triées) dans le fichier
int ok=0,i=0,j=0;
FILE *fichier;
char fin[1] = {10};
if(chemin[0]==0) //on regarde si le chemin a deja ete demander ou non
{
do //on boucle tant que le fichier n'est pas ouvert
{
fflush(stdin);
printf("entrer le chemin du fichier ou vous voulez sauvegarder les donnees : \n" );
gets(chemin);
if(chemin[0] != 0)
{
if( (fichier=fopen(chemin,"w" )) != NULL) ok=1;
}
} while (ok == 0); }
if ((fichier = fopen(chemin,"w" )) == NULL)
{
printf("impossible d'ouvrir le fichier" );
}
else
{ for(i=0;i<nbelt;i++)
{
fwrite(tab[i], sizeof(char), tab_leng[i], fichier);
fprintf(fichier, "\n" );
}
fclose(fichier);
}
}
void aff_donnees(char **tab, int nbelt, int *tab_leng)
{
//on affiche ici les donnees en memoires (tab)
int i=0,j=0;
for(i=0;i<nbelt;i++)
{
for(j=0;j<tab_leng[i];j++)
printf("%c", tab[i][j]);
printf("\n" );
}
}
void aff_fichier(char *chemin)
{
//on affiche les donnees directement lus sur le fichier sans passer par tab
int i=0,ok=0;
char ligne[NBMAX]={0};
FILE *fichier;
if(chemin[0]==0) //ontest si le chemin a deja ete demander
{
do
{
fflush(stdin);
printf("entrer le chemin du fichier a afficher : \n" );
gets(chemin);
if(chemin[0] != 0)
{
if( (fichier=fopen(chemin,"r" )) != NULL) ok=1;
}
} while (ok == 0); }
else
{
if( (fichier=fopen(chemin, "r" )) == NULL)
{
printf("probleme d'allocation" );
}
}
while(fgets(ligne, NBMAX, fichier) != NULL)
{
i=0;
while(ligne[i] != 10)
{
if( ligne[i]<48 || ligne[i]>57)
{
if(ligne[i]!=45)
{
printf("ce n'est pas le bon fichier!" );
break;
}
}
printf("%c", ligne[i]);
i++;
}
printf("\n" );
}
fclose(fichier);
}
---------------
PDG du Microsoft's DestructorClan.