Friday, April 9, 2010

ds - parsing operation for paranthesis ( )

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
#define n 20
struct stack
{
char c[n];
int top;
};
typedef struct stack stack;
void push(stack *,char);
char pop(stack *);
void parse(char *,stack *);
void main()
{
stack *st=NULL;
char s[15];
clrscr();
st->top=-1;
printf("enter equation :");
scanf("%s",s);
parse(s,st);
getch();

}
void push(stack *st,char data)
{
if(st->top==n-1)
{
printf("\nList is Already FULL");
return;
}

st->c[++st->top]=data;

}
char pop(stack *st)
{
char res;
if(st->top==-1)
{
printf("\nList is Empty");
return NULL;
}
res=st->c[st->top];

st->top--;
return res;
}
void parse(char *s,stack *st)
{
int i=0,f=0;
char c;
while(s[i]!=0)
{
c=s[i];
if(c=='(')
push(st,c);
else
{ if(c==')')
{
if(st->top==-1) {f=1;
printf("error:closing paranthesis not matched."); }
else
pop(st); }
} i++;
}
if(st->top!=-1)
printf("error:opening paranthesis not matched.");
else if(f!=1)
printf("no error:paranthesis are matched.");
}

No comments:

Post a Comment