codice:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAX_STRINGA 30
#define NON_PRESENTE -1
int matr_regole[6][7]={{4,1,1,1,1,1,5},
{2,2,2,1,1,1,2},
{2,2,2,1,1,1,2},
{2,2,2,2,2,1,2},
{2,2,2,2,2,1,2},
{5,1,1,1,1,1,3}};
char vett_car[7+1]="%+-*/()";
typedef struct dato_s
{
char carat;
struct dato_s *prossimo;
}dato;
typedef enum {FALSE,TRUE} boolean;
dato *stack_pointer,*punt_insert,*punt_extract;
char str[MAX_STRINGA];
int indice,colonna,riga,regola;
boolean finito,corretto;
char carat_stack;
int leggi_str(char stringa[]);
void push(dato **p_stack_pointer, char carattere);
void pop(dato **p_stack_pointer, char *carattere);
void queue(dato **p_insert, dato **p_extract, char carattere);
int cerca_posizione(char carattere);
void guarda_cima_stack(dato *p_stack_pointer, char *carattere);
void stampa(dato *punt);
void inizializza (dato **p_stack_pointer, dato **p_punt_insert, dato **p_punt_extract);
main()
{
clrscr();
while(leggi_str(str)!=EOF)
{
inizializza (&stack_pointer,&punt_insert,&punt_extract);
push(&stack_pointer,str[0]);
indice=1;
finito=FALSE;
while(!finito)
{
if((colonna=cerca_posizione(str[indice]))==NON_PRESENTE)
{
queue(&punt_insert,&punt_extract,str[indice]);
indice++;
}
else
{
guarda_cima_stack(stack_pointer,&carat_stack);
riga=cerca_posizione(carat_stack);
regola=matr_regole[riga][colonna];
switch(regola)
{
case 1:
{
push(&stack_pointer,str[indice]);
indice++;
}
break;
case 2:
{
pop(&stack_pointer,&carat_stack);
queue(&punt_insert,&punt_extract,carat_stack);
}
break;
case 3:
{
pop(&stack_pointer,&carat_stack);
indice++;
}
break;
case 4:
{
corretto=TRUE;
finito=TRUE;
}
break;
case 5:
{
corretto=FALSE;
finito=TRUE;
}
}
}
}
if(corretto)
stampa(punt_extract);
else
printf("Espressione non correttamente bilanciata\n");
}
}
void inizializza (dato **p_stack_pointer, dato **p_punt_insert, dato **p_punt_extract)
{
dato *p_aux, *p_prec;
p_aux = *p_stack_pointer;
while (p_aux!=NULL)
{
p_prec = p_aux;
p_aux = (*p_prec).prossimo;
free (p_prec);
}
*p_stack_pointer=NULL;
*p_punt_insert=*p_punt_extract=NULL;
}
int leggi_str(char stringa[])
{
int indice;
char carat;
stringa[0]='%';
indice=1;
printf("Introduci l'espressione infissa (CTRL+Z per terminare): ");
while(((carat=getchar())!='\n')&&(carat!=EOF))
{
stringa[indice]=carat;
indice++;
}
stringa[indice]='%';
stringa[indice+1]='\0';
return(carat);
}
void push(dato **p_stack_pointer, char carattere)
{
dato *p_corr;
p_corr=(dato *)malloc(sizeof(dato));
(*p_corr).carat=carattere;
(*p_corr).prossimo=*p_stack_pointer;
*p_stack_pointer=p_corr;
}
void pop(dato **p_stack_pointer, char *carattere)
{
*carattere=(**p_stack_pointer).carat;
*p_stack_pointer=(**p_stack_pointer).prossimo;
}
void queue(dato **p_insert, dato **p_extract, char carattere)
{
dato *p_corr;
p_corr=(dato *)malloc(sizeof(dato));
(*p_corr).carat=carattere;
(*p_corr).prossimo=NULL;
if(*p_insert==NULL)
{
*p_insert=p_corr;
*p_extract=p_corr;
}
else
{
(**p_insert).prossimo=p_corr;
*p_insert=p_corr;
}
}
int cerca_posizione(char carattere)
{
int indice=0;
while((vett_car[indice]!=carattere)&&(indice<strlen(vett_car)))
indice++;
if (indice==strlen(vett_car))
indice = NON_PRESENTE;
return(indice);
}
void guarda_cima_stack(dato *p_stack_pointer, char *carattere)
{
*carattere=(*p_stack_pointer).carat;
}
void stampa(dato *punt)
{
dato *p_temp;
p_temp=punt;
while(p_temp!=NULL)
{
putchar((*p_temp).carat);
p_temp=(*p_temp).prossimo;
}
putchar('\n');
}
Ciao!