Pages

Related Posts with Thumbnails
Parenthesis Checking – C Programming and Algorithom >> Codes-N-Tricks
Content feed Comments Feed

Parenthesis Checking – C Programming and Algorithom

Posted by Soham Sunday, September 12, 2010

This Program check in an expression that if the brackets in the expression are in the proper form or not and also it checks for the same no. of opening and closing parenthesis.
This Program takes input from a file named "bracket.txt". Expressions are given there in each new line.Program takes an expression,checks it and shows the result on the display screen.

Code Snipped
  1. #include<stdio.h>
  2.  
  3. #define STACKSIZE 20
  4.  
  5. struct stack
  6. {
  7.     int top;
  8.     char items[STACKSIZE];
  9. };
  10.  
  11. struct stack stk;
  12.  
  13.  
  14. char pop(struct stack *ps)
  15. {
  16.     char var;
  17.     if(ps->top!=-1)
  18.     {
  19.         var=ps->items[ps->top];
  20.         ps->top--;
  21.         return var;
  22.     }
  23. }
  24.  
  25. void push(struct stack *ps,char a)
  26. {
  27.     if(ps->top==STACKSIZE-1)
  28.     {
  29.         printf("\nOverflow");
  30.         exit(1);
  31.     }
  32.     else
  33.     {
  34.         ps->top++;
  35.         ps->items[ps->top]=a;
  36.     }
  37. }
  38.  
  39.  
  40. int main()
  41. {
  42.     FILE *fp;
  43.     char inp[50],x;
  44.     int i,j,k,dec;
  45.     struct stack *ps;
  46.     ps=&stk;
  47.  
  48.     fp=fopen("bracket.txt","r");
  49.     if(fp==NULL)
  50.     {
  51.         printf("\nFile Reading Error.\n");
  52.         exit(1);
  53.     }
  54.  
  55.     while(!feof(fp))
  56.     {
  57.         i=0;j=0;k=0;dec=1;ps->top=-1;
  58.         printf("Expression=");
  59.         fscanf(fp,"%s\n",inp);
  60.         for(k=0;inp[k]!='\0';k++)      //printing the expression
  61.             printf("%c",inp[k]);
  62.         while(inp[j]!='\0' && inp[i]!='\0')
  63.         {
  64.             if(inp[j]=='(' || inp[j]=='{' || inp[j]=='[')
  65.             {
  66.                 push(ps,inp[j]);
  67.             }
  68.             j++;
  69.             if(inp[i]==')' || inp[i]=='}' || inp[i]==']')
  70.             {
  71.                 if(ps->top>-1)
  72.                 {
  73.                     x=pop(ps);
  74.                     if((inp[i]==')' && x=='(') || (inp[i]=='}' && x=='{') || (inp[i]==']' && x=='[')) //Checking the corresponding bracket
  75.                         dec=1;
  76.                     else
  77.                         dec=0;
  78.                 }
  79.                 else
  80.                     dec=0;
  81.             }
  82.             i++;
  83.             if(dec==0)
  84.                 break;
  85.         }
  86.         if(dec==0||ps->top>-1)
  87.             printf("\n\tBrackets are not matched.");
  88.         else
  89.             printf("\n\tBrackets are matched.");
  90.         printf("\n\n");
  91.     }
  92.     return 0;
  93. }
Further discussion will be carried out later.Any suggestion is solicited.
Thanks.

0 Responses to Parenthesis Checking – C Programming and Algorithom

Post a Comment

SyntaxHighlighter

E-Mail Subscription

Enter your email address:

Subscribe & get Regular E-Mail Updates From Codes-N-Tricks.
Delivered by FeedBurner

Share with Orkut

Loading

Recent Posts

About Us