1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
void erreur_memoire(void)
{
printf("Erreur Mémoire\n");
exit(1);
}
void fichier_non_trouve(void)
{
printf("fichier non trouvé\n");
}
void info_fichier(const char *fichier, int *nb_lignes, int *nb_colonnes, int *nb_lignes_vides, char separateur)
{
char c;
int nb_sep, cpt_col;
FILE *f;
if (nb_lignes)
*nb_lignes=0;
if(nb_colonnes)
*nb_colonnes=0;
if(nb_lignes_vides)
*nb_lignes_vides=0;
if ( f=fopen(fichier,"rt") )
{
c=fgetc(f);
nb_sep=0;
cpt_col=1;
;
while( c!=EOF )
{
if ( c=='\n' && nb_lignes )
(*nb_lignes)++;
if ( c==separateur && nb_colonnes && cpt_col )
(*nb_colonnes)++;
if ( cpt_col && c== '\n' && (*nb_colonnes) )
cpt_col=!cpt_col;
if( c==separateur )
nb_sep++;
c=fgetc(f);
}
(*nb_colonnes)++;
(*nb_lignes_vides)=((*nb_colonnes-1)*(*nb_lignes)-nb_sep)/(*nb_colonnes-1);
fclose(f);
}
else
fichier_non_trouve();
}
void printf_tableau(int nb_lignes, int nb_colonnes, double **tableau)
{
int i,j;
printf("\n");
for(i=0;i<nb_lignes;i++)
{
printf("ligne %2.2d : ",i+1);
for(j=0;j<nb_colonnes;j++)
printf("%g ",tableau[i][j]);
printf("\n");
}
printf("\n");
}
char *bouche_tours(char *e, char separateur, char remplacement)
{
int i, j;
char *s,*p, *v, rempl[2], sep[3];
size_t taille;
sep[0]=separateur;
sep[1]=separateur;
sep[2]='\0';
rempl[0]=separateur;
rempl[1]=remplacement;
p=e;
if ( !(s=(char *)malloc((2*strlen(e)+1)*sizeof(char))) )
erreur_memoire();
*s='\0';
if (*e==separateur)
strcat(s,&(rempl[1]));
i=0;
while(p!=(e+strlen(e)))
{
v=strstr(p,sep);
if (v==p)
strcat(s,rempl);
else
strncat(s,p,1);
p++;
}
if (*(e+strlen(e)-1)==separateur)
strcat(s,&(rempl[1]));
if (!(s=(char *)realloc(s,sizeof(char)*strlen(s))))
erreur_memoire();
return s;
}
void enregistre_ligne(double **sortie, char *buffer,int ligne_courante, char separateur)
{
int nb_sep,i,deb;
char *attribut, sep[2];
nb_sep=0;
attribut=strtok(buffer, ",");
while(attribut!=NULL)
{
sortie[ligne_courante][nb_sep]=atof(attribut);
attribut=strtok(NULL, ",");
nb_sep++;
}
}
double **lecture_cvs(const char *fichier, int nbl, int nbc, char separateur, int *ligne_tableau)
{
FILE *f;
int ligne, i, nb_char, nb_sep;
char *buf1,c;
double **sortie;
if ( !(sortie=(double **)malloc(sizeof(double *)*nbl)) )
erreur_memoire();
if ( f=fopen(fichier,"rt") )
{
/* On initialise la ligne courante du tableau */
ligne=0;
for(i=0;i<=nbl;i++)
{
/* On calcule le nombre de caractères de la ligne */
c=fgetc(f);
nb_char=1;
nb_sep=0;
while(c!='\n' && c!=EOF)
{
if (c==separateur)
nb_sep++;
c=fgetc(f);
nb_char++;
}
/* on écrit la ligne contenue dans le buffer dans le tableau de sortie.
Si la ligne est vide on n'imcrémente pas ligne donc on réécrira dessus au prochain tour de boucle */
if ( nb_sep==(nbc-1) )
{
fseek(f,-nb_char,SEEK_CUR);
if ( !(buf1=(char *)malloc(sizeof(char)*(nb_char))) )
erreur_memoire();
fscanf(f,"%s\n",buf1); /* on récupére dans le buffer la ligne courante */
buf1=bouche_tours(buf1,separateur,'0');printf("%s\n",buf1);
if ( !(sortie[ligne]=(double *)malloc(sizeof(double)*nbc)) )
erreur_memoire();
enregistre_ligne(sortie,buf1,ligne,separateur);
free(buf1);
ligne++;
}
}
fclose(f);
}
else
{
fichier_non_trouve();
if (ligne_tableau)
*ligne_tableau=0;
return NULL;
}
if ( !(sortie=(double **)realloc(sortie,sizeof(double *)*ligne)) )
erreur_memoire();
if (ligne_tableau)
*ligne_tableau=ligne;
return sortie;
}
void libere_tableau(int nb_lignes, double **tableau)
{
int i;
if (tableau)
{
for(i=0;i<nb_lignes;i++)
{
if (tableau[i])
free(tableau[i]);
}
free(tableau);
}
}
/* renvoi une ligne pour min et une pour max contenant les min et max de la colonne correspondante.
Pour le moement, le tableau ne doit comporter que des chiffres.
Il faut penser à faire un free sur min et max après utilisation. */
void extremum(double **tableau, int nb_lignes, int nb_colonnes, double **mini, double **maxi)
{
int i,j;
double *min, *max;
if (!(min=(double *)malloc(sizeof(double)*nb_colonnes)))
erreur_memoire();
if (!(max=(double *)malloc(sizeof(double)*nb_colonnes)))
erreur_memoire();
for(i=0;i<nb_colonnes;i++)
{
min[i]=tableau[0][i];
max[i]=tableau[0][i];
}
for(i=0;i<nb_lignes;i++)
{
for(j=0;j<nb_colonnes;j++)
{
if (tableau[i][j]<min[j])
min[j]=tableau[i][j];
if (tableau[i][j]>max[j])
max[j]=tableau[i][j];
}
}
*mini=min;
*maxi=max;
}
void libere_tout(double **tableau, double *min, double *max, int nbl)
{
if (min)
free(min);
if (max)
free(max);
if (tableau)
libere_tableau(nbl,tableau);
}
void normalisation(double **tableau,double *min, double *max, int nbc, int nbl)
{
int i,j;
double EM;
for(i=0;i<nbc;i++)
{
EM=max[i]-min[i];
for(j=0;j<nbl;j++)
{
if (EM)
tableau[j][i]=(tableau[j][i]-min[i])/EM;
}
}
}
int main(int argc, char **argv)
{
double **tableau, *min, *max;
char sep=',', texte='\"';
int nbl,nbc,nblv,nblt,i;
if (argc<2)
{
printf("Ce programme s'utilise comme suit :\n./main fichier.csv\n");
return -1;
}
printf("\nInformations sur le fichier :\n");
printf("-----------------------------\n");
info_fichier(argv[1],&nbl,&nbc,&nblv,sep);
printf("\nNombre de lignes : %d\nNombre de colonnes :%d\nNombre de lignes vides :%d\n\n",nbl,nbc,nblv);
tableau=lecture_cvs(argv[1],nbl,nbc,sep,&nbl);
if (tableau)
{
printf("\nLégende :\n");
printf("---------\n");
printf("-1 : erreur de lecture\n");
printf("-2 : texte\n");
printf("Autre : valeur numérique\n");
printf("N.B. : cette version de la librairie ne prend pas en compte les chaines de caractères\n");
printf("\n");
printf_tableau(nbl,nbc,tableau);
extremum(tableau,nbl,nbc,&min,&max);
printf("mins : ");
for(i=0;i<nbc;i++)
printf("%g ",min[i]);
printf("\n");
printf("maxs : ");
for(i=0;i<nbc;i++)
printf("%g ",max[i]);
printf("\n\n");
normalisation(tableau,min,max,nbc,nbl);
printf("Tableau Normalisé :\n");
printf_tableau(nbl,nbc,tableau);
}
libere_tout(tableau,min,max,nbl);
printf("mémoire libérée avec succès\n\n");
return EXIT_SUCCESS;
} |