Advertisement
tsnaik

Checking if brackets are matching in an expression

Oct 22nd, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<stdlib.h>
  4. struct node
  5. {
  6.         char data;
  7.         struct node *next;
  8. } *start=NULL;
  9.  
  10. void push(char);
  11. char pop(void);
  12. void print();
  13.  
  14. void main()
  15. {
  16.     char in='\0';
  17.     while(in!='\n')
  18.     {
  19.         in=getchar();
  20.        
  21.         if(in =='(' || in=='[' || in=='{')
  22.         {
  23.             push(in);
  24. //          print();
  25.             continue;
  26.         }
  27.         else if (in ==')')
  28.         {
  29.             if((pop())+1==in)
  30.             {
  31. //              print();
  32.                 continue;
  33.             }
  34.             else
  35.             {
  36.                 push(in-1);
  37.                 continue;
  38.             }  
  39.         }
  40.  
  41.         else if (in ==']')
  42.                 {
  43.                         if((pop())+2==in)
  44.                         {
  45. //              print();
  46.                                 continue;
  47.                         }
  48.             else
  49.             {
  50.                 push(in-2);
  51.                 continue;
  52.             }
  53.                 }
  54.  
  55.         else if (in =='}')
  56.                 {
  57.                         if((pop())+2==in)
  58.                         {
  59. //              print();
  60.                                 continue;
  61.                         }
  62.             else
  63.             {
  64.                 push(in-2);
  65.                 continue;
  66.             }
  67.                 }
  68.        
  69.         else if(isalnum(in) || in=='+' || in=='-' || in=='*' || in=='/')
  70.         {
  71. //          print();
  72.             continue;
  73.         }
  74.         else if(in!='\n')
  75.         {
  76.             printf("\nInvalid Expression\n");
  77.             exit(1);
  78.         }
  79.  
  80.  
  81.        
  82.     }
  83.    
  84.     if(start!=NULL)
  85.     {
  86.         printf("\nExpression is invalid\n");
  87.         exit(1);
  88.     }
  89.  
  90.     printf("\nExpression is valid\n");
  91.  
  92. }
  93.  
  94. void push(char element)
  95. {
  96.  
  97.         struct node *new;
  98.         new=(struct node *) (malloc(sizeof(struct node)));
  99.  
  100.     new->data=element;
  101.  
  102.         new->next=NULL;
  103.         if(start==NULL)
  104.         {
  105.                 start=new;
  106.  
  107.                 return;
  108.         }
  109.         new->next=start;
  110.  
  111.         start=new;
  112. }
  113.  
  114. char pop()
  115.  {
  116.     struct node *p;
  117.  
  118.     if(start==NULL) //if empty
  119.      {
  120.         return('!');
  121.      }
  122.      else
  123.      {
  124.         p=start;
  125.         start=start->next;
  126.        
  127.         return(p->data);
  128.      }
  129.  }
  130.  
  131. void print()
  132. {
  133.         if(start==NULL)
  134.         {
  135.                 printf("\nNo data in Linked List\n");
  136.                 return;
  137.         }
  138.  
  139.         struct node *p;
  140.         p=start;
  141.         printf("\nLinked list: \n");
  142.  
  143.         while(p!=NULL)
  144.         {
  145.         printf("\n%c",p->data);
  146.         p=p->next;
  147.         }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement