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
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
/*Fonction de remplacement de chaine*/
char *str_remplace (const char *s, unsigned int start, unsigned int lenght, const char *ct){
char *new_s = NULL;
if (s && ct && start >= 0 && lenght >= 0){
size_t size = strlen (s);
new_s = malloc (sizeof (*new_s) * (size - lenght + strlen (ct)));
if (new_s){
memmove (new_s, s, start);
memmove (&new_s[start], ct, strlen (ct));
memmove (&new_s[start + strlen (ct)], &s[start + lenght],size - lenght - start + 1);
}
}
else{
fprintf (stderr, "Memoire insuffisante\n");
exit (EXIT_FAILURE);
}
return new_s;
}
/*Position de la chaine ct dans cs dans la chaine*/
int str_istr (const char *cs, const char *ct){
int index = -1;
if (cs && ct){
char *ptr_pos = NULL;
ptr_pos = strstr (cs, ct);
if (ptr_pos){
index = ptr_pos - cs;
}
}
return index;
}
/*Mise en forme de l'expression pour etre interpretee par bc*/
char * convert_bc (char * chaine,float x){
char old_chaine[255];
char new_chaine[255];
int pos;
int tai;
int sig=5;/* significant digits */
int match;
while(strstr(chaine,"sin")||strstr(chaine,"cos")||strstr(chaine,"arg")||strstr(chaine,"exp")||strstr(chaine,"x")){
match=0;
if((strstr(chaine,"sin"))&&(match==0)){
strcpy(old_chaine,"sin");
strcpy(new_chaine,"s");
match=1;
}
if((strstr(chaine,"cos"))&&(match==0)){
strcpy(old_chaine,"cos");
strcpy(new_chaine,"c");
match=1;
}
if((strstr(chaine,"arct"))&&(match==0)){
strcpy(old_chaine,"arct");
strcpy(new_chaine,"a");
match=1;
}
if((strstr(chaine,"exp"))&&(match==0)){
strcpy(old_chaine,"exp");
strcpy(new_chaine,"e");
match=1;
}
if((strstr(chaine,"x"))&&(match==0)){
strcpy(old_chaine,"x");
/* a regular number */
sprintf(new_chaine, "%f", x);
match=1;
//gcvt(x, sig,new_chaine);
}
if(match==1){
pos = str_istr(chaine, old_chaine);
tai = strlen(old_chaine);
chaine=str_remplace(chaine,pos,tai,new_chaine);
printf("A nc %s - oc %s - pos %d - tai %d -ch %s\n",new_chaine, old_chaine,pos,tai,chaine);
// printf("resultat %s\n",chaine);
}
printf("B %s\n",chaine);
}
return chaine;
}
/*Calcul de l'ordonne en focntion de l'expression*/
float eval(char * chaine,float x){
char * cmd = malloc (sizeof (*cmd) * 256);
char * c_resultat = malloc (sizeof (*c_resultat) * 256);
float f_resultat;
char * chaine_clean = malloc (sizeof (*chaine_clean) * 256);
//printf("exprB:%s\n",chaine);
chaine_clean=convert_bc(chaine,x);
printf("exprA:%s\n",chaine_clean);
strcat(cmd,"echo \"");
strcat(cmd,chaine_clean);
strcat(cmd,"\" | bc -l");
printf("%s\n",cmd);
FILE * f;
// Ouverture du shell et lancement de gnuplot
f = popen(cmd, "r");
fgets(c_resultat,255,f);
fflush(f);
pclose(f);
f_resultat=atof(c_resultat);
return f_resultat;
}
int main(int argc, char** argv){
float x=1;
//for(x=0.0;x<10.0;x+=0.001){
printf("resul pour :%f = %f\n",x,eval("x+exp(x)-1+sin(x)",x));
//}
return 0;
} |
Partager