#include #include #include #define _FILE "espressione.txt" #define MAX_L 120 typedef struct ident_t{ char ident; int n_valori; float *valori; struct ident_t *next; }ident_t; typedef struct nodo_t{ int type; float num; char operatore; ident_t *identificatore; struct nodo_t *left; struct nodo_t *right; }nodo_t; /* prototipi */ /* * Suddivide in sottoespressioni * */ char *sottoesp(char *esp, int num_p) { char *tmp; int i=num_p,len=0; while (esp[i]!=')'){ len++; i++; } tmp =(char*) malloc(len); esp += num_p+1; strncpy(tmp,esp,len-1); //for(i=0;esp[i]&&inext=*idnt; *idnt=x; } return 0; } int list_print(ident_t *idnt) { ident_t *tmp=NULL; if (idnt==NULL) printf("SS"); tmp=idnt; while(tmp!=NULL) { printf(":%c :%d\n", tmp->ident, tmp->n_valori); tmp=tmp->next; } return 0; } /* * Calcola valori * */ ident_t *calc_valori(ident_t *tmp, float *tmp_val) { int i=0; tmp->valori=(float*)malloc(sizeof(float)*tmp->n_valori); for(i=0; in_valori; i++) { tmp->valori[i]=tmp_val[0]+(tmp_val[1]*i); } return tmp; } /* * Main * */ int main() { FILE *fp; char esp_tmp[MAX_L], buf[MAX_L]; float tmp_val[3]; int i=0; ident_t *idnt=NULL, *tmp=NULL; nodo_t *albero=NULL; //idnt = (ident_t *)malloc (sizeof(ident_t)); //idnt->next=NULL; if (( fp=fopen(_FILE, "r") )==NULL ) { printf("Impossibile accedere al file\n"); exit(-1); } fgets(esp_tmp, MAX_L, fp); while(fgets(buf, MAX_L, fp)!=NULL) { tmp=(ident_t*)malloc(sizeof(ident_t)); sscanf(buf, "%c %f %f %f", &tmp->ident, &tmp_val[0], &tmp_val[1], &tmp_val[2]); #ifdef TRACE printf("%s", buf); printf("%c %f %f %f\n", tmp->ident, tmp_val[0], tmp_val[1], tmp_val[2]); #endif tmp->n_valori=tmp_val[2]; /* in tmp_val[2] è memorizzato il numero di valori possibili che l'identificatore può assumere */ tmp->next=NULL; tmp=calc_valori(tmp, tmp_val); #ifdef TRACE for(i=0; in_valori; i++) printf("%f",tmp->valori[i]); #endif //if (idnt==NULL) printf("ERR"); list_insert(&idnt, tmp); } if (idnt==NULL) printf("Nessun identificatore.\n"); fclose(fp); genera_struct(esp_tmp, albero, idnt); //list_print(idnt); return 0; }