Advertisement
ledrose

bracket_checker

Jan 26th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5. struct element {
  6.     char sym;
  7.     struct element *p;
  8. };
  9.  
  10. char set[]="{(())}[{(({(())}[][][]()()))}[][][]()({(({(())}[][][]()()))}[][][]()())][][]()()";
  11.  
  12. int main(int argc, char *argv[]) {
  13.     struct element *top=NULL,*memory;
  14.     int i=0,j;
  15.     while(set[i]) {
  16.         memory=(struct element*)malloc(sizeof(struct element));
  17.         memory->p=top;
  18.         memory->sym=set[i];
  19.         top=memory;
  20.         if (((top->sym==')')&&((top->p)->sym=='('))||((top->sym==']')&&((top->p)->sym=='['))||((top->sym=='}')&&((top->p)->sym=='{'))) {
  21.             for (j=0;j<2;j++) {
  22.                 memory=top->p;
  23.                 free(top);
  24.                 top=memory;
  25.             }
  26.         }
  27.         i++;
  28.     }
  29.     if (top) printf("Error\n");
  30.     else printf("OK");
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement