/*PARENTHESIZED EXPRESSION USING STACK */
#include<stdio.h>
#include<conio.h>
#define MAX 10
int top=-1;
int stack[MAX];
void push(char);
char pop();
void main()
{
char exp[MAX],ch;
int i,flag=1;
clrscr();
printf("\n Enter Infix Expression:-\n");
gets(exp);
i=0;
while(exp[i]!='\0')
{
if(exp[i]=='('||exp[i]=='{'||exp[i]=='[')
{
push(exp[i]);
}
if(exp[i]==')'||exp[i]=='}'||exp[i]==']')
{
ch=pop();
if(exp[i]==')'&&(ch=='{'||ch=='['))
{
flag=0;
}
if(exp[i]=='}'&&(ch=='('||ch=='['))
{
flag=0;
}
if(exp[i]==']'&&(ch=='('||ch=='{'))
{
flag=0;
}
}
i++;
}
if(top>=0)
{
flag=0;
}
if(flag==1)
{
printf("\n Valid Expression");
}
else
{
printf("\n Invalid Expression");
}
getch();
}
void push(char c)
{
if(top==MAX-1)
{
printf("\n stack is full");
}
else
{
top++;
stack[top]=c;
}
}
char pop()
{
char c;
if(top==-1)
{
printf("\n Stack is empty");
}
else
{
c=stack[top];
top--;
return c;
}
}
#include<stdio.h>
#include<conio.h>
#define MAX 10
int top=-1;
int stack[MAX];
void push(char);
char pop();
void main()
{
char exp[MAX],ch;
int i,flag=1;
clrscr();
printf("\n Enter Infix Expression:-\n");
gets(exp);
i=0;
while(exp[i]!='\0')
{
if(exp[i]=='('||exp[i]=='{'||exp[i]=='[')
{
push(exp[i]);
}
if(exp[i]==')'||exp[i]=='}'||exp[i]==']')
{
ch=pop();
if(exp[i]==')'&&(ch=='{'||ch=='['))
{
flag=0;
}
if(exp[i]=='}'&&(ch=='('||ch=='['))
{
flag=0;
}
if(exp[i]==']'&&(ch=='('||ch=='{'))
{
flag=0;
}
}
i++;
}
if(top>=0)
{
flag=0;
}
if(flag==1)
{
printf("\n Valid Expression");
}
else
{
printf("\n Invalid Expression");
}
getch();
}
void push(char c)
{
if(top==MAX-1)
{
printf("\n stack is full");
}
else
{
top++;
stack[top]=c;
}
}
char pop()
{
char c;
if(top==-1)
{
printf("\n Stack is empty");
}
else
{
c=stack[top];
top--;
return c;
}
}