xbubbax
07-04-2008, 09:51
Ho fatto questo esercizio per capire se le parentesi tonde e quadre all'interno di una stringa sono annidate correttamente ma mi da sempre 0 come risultato, il che è sbagliato. Come mai?
#include <stdio.h>
#include <stdlib.h>
#include "funzioni_pile.h"
int match(char p1, char p2)
{
return(p1=='('&& p2==')')||(p1=='['&&p2==']');
}
int bilanciamento(char *s)
{
//PRE LA FUNZIONE PRENDE IN INGRESSO UNA STRINGA s CHE CONTIENE EVENTUALI PARENTESI TONDE E QUADRE
//POST LA FUNZIONE RESTITUISCE 1 SE LE PARENTESI CONTENUTE IN s SONO CORRETTAMENTE BILANICATE, 0 ALTRIMENTI
pilaPtr P=NULL;
char c;
char par;
while(c=*s++)
{
if(c=='('||c=='['){ P=Push(P,c);}
if(c==')'||c==']')
{
if(Empty(P)==1||!match(Top(P),c))
{
return 0;
}else
{
P=Pop(P);
}
}
}
}
int main(void)
{
char *s="[34(25)]";
printf("%d\n", bilanciamento(s));
}
#include <stdio.h>
#include <stdlib.h>
#include "funzioni_pile.h"
int match(char p1, char p2)
{
return(p1=='('&& p2==')')||(p1=='['&&p2==']');
}
int bilanciamento(char *s)
{
//PRE LA FUNZIONE PRENDE IN INGRESSO UNA STRINGA s CHE CONTIENE EVENTUALI PARENTESI TONDE E QUADRE
//POST LA FUNZIONE RESTITUISCE 1 SE LE PARENTESI CONTENUTE IN s SONO CORRETTAMENTE BILANICATE, 0 ALTRIMENTI
pilaPtr P=NULL;
char c;
char par;
while(c=*s++)
{
if(c=='('||c=='['){ P=Push(P,c);}
if(c==')'||c==']')
{
if(Empty(P)==1||!match(Top(P),c))
{
return 0;
}else
{
P=Pop(P);
}
}
}
}
int main(void)
{
char *s="[34(25)]";
printf("%d\n", bilanciamento(s));
}