All pastes #92470 Raw Edit

x

public c v1 · immutable
#92470 ·published 2006-07-19 16:00 UTC
rendered paste body
#include <stdlib.h>#include <stdio.h>#include <string.h>#define _FILE "espressione.txt"#define MAX_L 120typedef 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]&&i<len;i++) tmp[i] = esp[i];		return tmp;}/* * Conta le parentesi * */int count_parentesi(char *esp){	int num=0,i=0;		while (i<strlen(esp) || (esp[i]!=')'))	{		if (esp[i]=='(') num=i;		i++;	}	return num;	}/* * Genera l'albero di strutture * */int genera_struct(char *esp_tmp, nodo_t *albero, ident_t *idnt){	int num_p=0,i=0;	char *tmp_s=NULL;			printf("%s", esp_tmp);	num_p=count_parentesi(esp_tmp);	printf("%d\n", num_p);		tmp_s=sottoesp(esp_tmp, num_p);		printf("%s - %d", tmp_s, strlen(tmp_s));	/*	for (i=0; i<num_p/2; i++) 	{				elabora_tmp_s(tmp_s, albero, idnt);			}	*/return 0;}/* * Inserisce nella lista * */int list_insert(ident_t **idnt, ident_t *x){	ident_t *tmp;		if (x==NULL) exit(-1);			if (*idnt==NULL)		*idnt=x;	else	{		x->next=*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; i<tmp->n_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; i<tmp->n_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;}