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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<string.h>
typedef struct t_tab
{
int dim; // dimension du tableau
double * data;
} Tableau;
Tableau * alloc_Tableau(unsigned int,double);
void free_Tableau(Tableau *);
void affiche_Tableau(Tableau *);
Tableau * Tableau_vide(void);
void copie_Tableau(Tableau *,Tableau *);
Tableau * alloc_Tableau(unsigned int dim_,double val)
{
// alloue un espace memoire pour creer le tableau t et l'initialise a val
assert(dim_>=0);
Tableau * t=malloc(sizeof(*t));
assert(t!=NULL);
if(dim_==0) // si dim_==0 on considere que le tableau est vide
{
t->dim=0;
t->data=NULL;
}
else
{
t->dim=dim_;
t->data=malloc(dim_*sizeof(*t->data));
assert(t->data!=NULL);
unsigned int i;
for(i=0;i<dim_;i++)
t->data[i]=val;
}
return t;
}
void free_Tableau(Tableau * t)
{
t->dim=0;
// libere la memoire
if(t->data!=NULL)
{
free(t->data);
t->data=NULL;
}
free(t);
t=NULL;
return ;
}
Tableau * Tableau_vide(void)
{
Tableau * t=malloc(sizeof(*t));
t->dim=0;
t->data=NULL;
return t;
}
void affiche_Tableau(Tableau * t)
{
if(t->data==NULL)
printf("\nTableau vide\n");
else
{
int i;
for(i=0;i<t->dim;i++)
printf("%15.15g\t",t->data[i]);
printf("\n");
}
printf("\n");
return;
}
void copie_Tableau(Tableau * t,Tableau * out)
{
// copie le tableau *t dans le tableau *out
int i;
if(t->dim==0) // tableau vide
out=Tableau_vide();
else
{
for(i=0;i<out->dim;++i)
out->data[i]=t->data[i];
}
return;
}
void LoadInitialInputs(char * input_file,unsigned short * N,Tableau** temp,Tableau ** pres,Tableau ** richesse,Tableau ** EGR,double * rpm);
void ReadLineWithDouble(char * s,Tableau ** tab)
{
/*
s est une string qui contient des doubles.
Cette fonction remplit le tableau tab avec ces doubles
*/
char * p = strstr(s," ");
if(p!=NULL)
{
unsigned int ind=0;
while (*p)
{
(*tab)->data[ind]=strtod(p,&p);
++ind;
}
}
}
short IsCommentLine(char * s)
{
/*
The output is 1 if s is a line with only comments, else 0
A comment line is a line which begins by the char '!'
*/
char * p=strchr(s,'!');
if(p==NULL)
return 0;
else
{
int d=p-s; /* index of string s which contains the char '!' Remark : the first index is 0 */
int i;
for(i=0;(i<=d) && (s[i]==' ');++i);
return (i==d) ? 1 : 0;
}
}
void DeleteInitialSpaces(char * s)
{
/* this function deletes all the chars in the string s between the
first char which is not a space
*/
unsigned int n,i; /* n is the number of spaces which must be removed */
for(n=0;s[n]==' ';++n);
if(n!=0)
{
for(i=0;i<strlen(s)-n;++i)
s[i]=s[i+n];
s[strlen(s)-n]='\0';
}
}
void DeleteConsecutiveSpaces(char * s)
{
/* if there are many spaces between two chars of the string s, this
function deletes all these spaces in order to it remains only one
space between these two chars
*/
unsigned int i,j;
for(i=0;i<strlen(s);++i)
{
if(s[i]==' ')
{
for(j=i+1;(s[j]==' ') && (j<strlen(s));++j);
short n=j-i-1; /* number of spaces which should be erased */
if(n!=0)
{
for(j=i+1;j<strlen(s)-n;++j)
s[j]=s[j+n];
s[strlen(s)-n]='\0';
}
}
}
}
short NumberSpaces(char * s)
{
/* count number spaces of the string s */
DeleteInitialSpaces(s);
DeleteConsecutiveSpaces(s);
unsigned short count=0, i;
for(i=0;i<strlen(s);++i)
if(s[i]==' ')
++count;
return count;
}
void DeleteComment(char *);
void DeleteCharEndOfLine(char * ,unsigned short,unsigned short);
int main()
{
Tableau * temp=Tableau_vide();
Tableau * pres=Tableau_vide();
Tableau * richesse=Tableau_vide();
Tableau * EGR=Tableau_vide();
unsigned short N;
double rpm;
LoadInitialInputs("input.txt",&N,&temp,&pres,&richesse,&EGR,&rpm);
printf("affichage de temp\n");
affiche_Tableau(temp);
printf("affichage de pres\n");
affiche_Tableau(pres);
printf("affichage de EGR\n");
affiche_Tableau(EGR);
printf("affichage de richesse\n");
affiche_Tableau(richesse);
printf("rpm = %g\n",rpm);
free_Tableau(temp);
free_Tableau(pres);
free_Tableau(richesse);
free_Tableau(EGR);
return 0;
}
void DeleteComment(char * line_)
{
/* This function deletes the char '!' and all the char after it of
the string line_.
This function deletes also all the spaces befor the char '!' and
the last char of line_ which isn't a space
*/
char * p=strchr(line_,'!');
if(p!=NULL)
{
/* d is the index of string line_ which contains the char '!'
Remark : the first index is 0
*/
int d=p-line_,i;
for(i=d-1;line_[i]==' ';--d,--i);
char dest[d];
char * s3=strncpy(dest,line_,d);
/* we add char '\0' at the end of s3 so at the end of line_ */
s3[d]='\0';
char * aux=strcpy(line_,s3);
assert(aux!=NULL);
}
}
void DeleteCharEndOfLine(char * line_,unsigned short nl,unsigned short nl_)
{
/*
Delete char '\n' if this one was founded in the string line_,
send an error message otherwise
nl is the number of line of the input file
nl_ is the number of the line which contains the string line_
*/
char * p = strchr(line_,'\n'); /* char '\n' is sought */
if (p != NULL) /* if it was founded it is cleared */
*p =0;
else if(nl_!=nl) /* the last line of input file can not to have char '\n' */
{
/* tabular is too short. Exit program */
fprintf(stderr,"Error in file %s line %d in function %s : tabular line_ is too short\nIncrease its size\nExit program\n",__FILE__,__LINE__,__FUNCTION__);
exit(1);
}
}
void LoadInitialInputs(char * input_file,unsigned short * N,Tableau ** temp,Tableau ** pres,Tableau ** richesse,Tableau ** EGR,double * rpm)
{
/*
input_file est le nom du fichier contenant les input
N est le nombre de particules
temp est un tableau contenant les temperatures initiales de chaque particule
pres est un tableau contenant les pressions initiales de chaque particule
richesse est un tableau contenant la richesse initiale de chaque particule
EGR est un tableau contenant le taux d'EGR (en %) de chaque particule
*/
/* opening of the file in reading mode */
FILE * file=fopen(input_file,"r");
if (file==NULL)
{
fprintf(stderr,"Error in file %s line %d in function %s : file %s wasn't found\nExit program\n",__FILE__,__LINE__,__FUNCTION__,input_file);
exit(1);
}
int i;
/* definition of a table of char intended to receive the line */
char line[10000];
/* on compte le nombre de lignes du fichier d'entrees */
unsigned int countline=0;
for(;fgets (line, sizeof line, file) != NULL;++countline);
if(ferror(file))
{
fprintf(stderr,"Error in file %s line %d in function %s : a misreading occurred in file %s\nExit program\n",__FILE__,__LINE__,__FUNCTION__,input_file);
exit(1);
}
fclose(file);
file=fopen(input_file,"r");
unsigned int countline2=0;
do
{
fgets (line, sizeof line, file);
++countline2;
if(feof(file))
{
fprintf(stderr,"Error in file %s line %d in function %s : key word TEMP wasn't found in file %s\nExit program\n",__FILE__,__LINE__,__FUNCTION__,input_file);
exit(1);
}
}
while(strstr(line,"TEMP ")==NULL);
fclose(file);
DeleteCharEndOfLine(line,countline,countline2);
DeleteComment(line);
*N=NumberSpaces(line);
/* creation des tableaux */
free_Tableau(*temp);
free_Tableau(*pres);
free_Tableau(*richesse);
free_Tableau(*EGR);
*temp=alloc_Tableau(*N,0.0);
*pres=alloc_Tableau(*N,0.0);
*richesse=alloc_Tableau(*N,0.0);
*EGR=alloc_Tableau(*N,0.0);
/* initialisation des tableaux */
file=fopen(input_file,"r");
char keyword[50]; /* tableau stockant le mot cle */
countline2=0; /* line counteur */
while(fgets (line, sizeof line, file) != NULL)
{
++countline2;
/* si strlen(line_)==2 alors cela veut dire que la ligne est une ligne "blanche" et que line_='\n'
il vaut mieux utiliser le test strlen(line_)!=2 plutot que le test strcmp(line_,"\n") car suivant
le cas ou le fichier input_file a ete ecrit sous windows ou unix, le format des retours chariot n'est
pas le meme, ce qui peut etre cause d'erreur du prgm
cf <a href="http://emmanuel-delahaye.developpez.com/notes.htm" target="_blank">http://emmanuel-delahaye.developpez.com/notes.htm</a>
*/
if( (!IsCommentLine(line)) && (strlen(line)!=2) )
{
DeleteCharEndOfLine(line,countline,countline2);
DeleteComment(line);
if(strstr(line,"TEMP ")!=NULL)
{
ReadLineWithDouble(line,temp);
for(i=0;i<*N;++i)
assert((*temp)->data[i]>0.0);
}
else if(strstr(line,"PRES ")!=NULL)
{
assert(NumberSpaces(line)==*N);
ReadLineWithDouble(line,pres);
for(i=0;i<*N;++i)
assert((*pres)->data[i]>0.0);
}
else if(strstr(line,"RICHESSE ")!=NULL)
{
assert(NumberSpaces(line)==*N);
ReadLineWithDouble(line,richesse);
for(i=0;i<*N;++i)
assert((*richesse)->data[i]>0.0);
}
else if(strstr(line,"EGR ")!=NULL)
{
assert(NumberSpaces(line)==*N);
ReadLineWithDouble(line,EGR);
for(i=0;i<*N;++i)
assert( ((*EGR)->data[i]/=100.0) >=0.0);
}
else if(strstr(line,"RPM ")!=NULL)
{
assert(NumberSpaces(line)==1);
sscanf(line,"%s%lf",keyword,rpm);
assert(*rpm>0.0);
}
else
{
fprintf(stderr,"Error in file %s line %d in function %s : none keyword was found in file %s at line %s\nExit program\n",__FILE__,__LINE__,__FUNCTION__,input_file,line);
exit(1);
}
} /* end of if( (!IsCommentLine(line_)) && strcmp(line_,"\n") ) */
} /* end of while */
if(!feof(file))
{
fprintf(stderr,"Error in file %s line %d in function %s : a misreading occurred in file %s\nExit program\n",__FILE__,__LINE__,__FUNCTION__,input_file);
exit(1);
}
if(ferror(file))
{
fprintf(stderr,"Error in file %s line %d in function %s : a misreading occurred in file %s\nExit program\n",__FILE__,__LINE__,__FUNCTION__,input_file);
exit(1);
}
fclose(file);
} |
Partager