home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
CataniaB
/
teach-act
/
laboratorio
/
lab-26-4-99
/
ese7-versione-con-stack.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-25
|
1KB
|
47 lines
/*
* Stringhe con parentesi bilanciate: stack
*/
#include <stdio.h>
#define MAX 100
#define true 1
#define false 0
int stack[MAX];
int ptop;
void init(void) {ptop=stack[ptop]=0;} /* stack vuoto se ptop=0 */
void pop(void) {if (ptop>0) ptop--;}
void push(int el) {if (ptop<MAX) stack[++ptop]=el;} /* prima muovi ptop */
int top(void) {return(stack[ptop]);}
main()
{
char buf[MAX];
int i,continua;
gets(buf);
printf("\n echo %s\n ",buf);
init();
for(i=0,continua=true;(continua && buf[i]);i++)
switch (buf[i])
{
case '[': case '(': case '{':
push(buf[i]);
break;
case '}': if (top()=='{') pop();
else continua=false;
break;
case ']': if (top()=='[') pop();
else continua=false;
break;
case ')': if (top()=='(') pop();
else continua=false;
break;
default:;
}
if (continua && !ptop) printf("\nBilanciate\n");
else printf("\nNon Bilanciate\n");
}