Parentheses in mathematical expression

#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
struct stack
{
	int top;
   char paren[20];
};
int main ()
{
	struct stack s;
   char instr[20];
   int valid=TRUE,i=0;
   s.top=-1;
   clrscr();
   printf("Enter an arithmetic expression: ");
   while((instr[i]=getchar())!='\n')
   {
   	if(instr[i]=='(')
      	push(&s,instr[i]);
      if(instr[i]==')')
      {
      	if(empty(&s))
         {
         	valid =FALSE;
            break;
         }
         else
         {
         	pop(&s);
         }
         i++;
      }
   }
   if(!empty(&s))
   	valid=FALSE;
   if(valid)
   	printf("The given expression is valid!");
   else
   	printf("The given Expression is NOT valid");
   getch();
   return 0;

}

push(ps,n)
struct stack *ps;
int n;
{
	ps->top++;
   ps->paren[ps->top]=n;
}

pop(ps)
struct stack *ps;
{
	ps->top--;
}
empty(ps)
struct stack *ps;
{
	if(ps->top==-1)
   	return TRUE;
   else
   	return FALSE;
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *