#include #include #include #include "stack.h" #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 */ /* * * */ int add_nodo(nodo_t **x, char *sub) { } int parent_2(char *stringa) { int startpos; int endpos,i=0; stack_t *s; char buf[200]; for (i=0; i < strlen(stringa); i++) { switch(stringa[i]) { case '(': s=stack_push(s,(void*)i); // crea_livello() //-------------> imposta un sottoalbero radice,figlio sx, figlio dx break; case ')': s=stack_pop(s, (void*)&startpos); endpos=i; //printf("%d ~ %d\n", startpos, endpos); memset(buf, 0, sizeof(buf)); strncpy(buf, stringa+startpos+1, i-startpos); // copia i caratteri da "(" fino a "+,/,-,*" printf("src2: %s\n", buf); // set_figlio_dx(buf) break; case '+': case '-': case '*': case '/': s=stack_pop(s, (void*)&startpos); memset(buf, 0, sizeof(buf)); strncpy(buf, stringa+startpos+1, i-startpos); // copia i caratteri da "(" fino a "+,/,-,*" printf("src1: %s\n", buf); printf("op : %c\n", stringa[i]); s=stack_push(s,(void*)i); // imposta il nuovo punto di inizio per la II parte // set_figlio_sx(buf) // set_radix_val(stringa[i]) // setta l'operatore voluto come radice del sottoalbero break; default: break; } } return 0; } /* * PARENTESIZZAZIONE * */ int parent(char *stringa) { int startpos; int endpos; int i=0; stack_t *s; for (i=0; i < strlen(stringa); i++) { switch(stringa[i]) { case '(': s=stack_push(s,(void*)i); break; case ')': s=stack_pop(s, (void*)&startpos); endpos=i; printf("%d ~ %d\n", startpos, endpos); break; default: break; } } return 0; } /* * STR REMOVE * */ char *str_remove(char *x, int num, int len) { char *tmp; //printf("x:%s\nn:%d\nl:%d\n",x,num,len); tmp = malloc(strlen(x)-len); memset(tmp,'\0',strlen(x)-len); strncpy(tmp,x,num); x+= (num+len); strncat(tmp,x,strlen(x)); return tmp; } /* * Suddivide in sottoespressioni * */ char *sottoesp(char **esp, int num_p) { char *tmp,*x, *x1; int i=num_p,len=0; x = malloc(strlen(*esp)); memset(x,'\0',strlen(*esp)); strncpy(x,*esp,strlen(*esp)-1); while (x[i]!=')'){ len++; i++; } tmp = malloc(len); memset(tmp,'\0',len); x += num_p+1; strncpy(tmp,x,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; }