Beyrin

lab1_5

Jun 1st, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6.  
  7. int main()
  8. {
  9.     int round = 0, curly = 0, square = 0, errorflag = 0;
  10.     int i, j, z;
  11.     char c, *str = "", *tmp = NULL;
  12.  
  13.     for (i=0; (c = getchar())!= '\n'; i++)
  14.     {
  15.         tmp = str;
  16.         str = (char*)malloc(i+2);
  17.         strcpy(str, tmp);
  18.         free(tmp);
  19.         str[i] = c;
  20.         str[i+1]= '\0';
  21.     }
  22.  
  23.     for (j = 0; j!= strlen(str); j++)
  24.     {
  25.         if (str[j] == '(')
  26.         {
  27.             round++;
  28.             for (z = j;z !=strlen(str);z++ )
  29.             {
  30.                 if (str[z+1] == '}' || ']')
  31.                 {
  32.                     errorflag = 1;
  33.                     break;
  34.                 }
  35.                 if (str[z+1] == ')')
  36.                 {
  37.                     round--;
  38.                     break;
  39.                 }
  40.             }
  41.         }
  42.         if (str[j] == '{')
  43.         {
  44.             curly++;
  45.             for (z = j;z !=strlen(str);z++ )
  46.             {
  47.                 if (str[z+1] == ')' || ']')
  48.                 {
  49.                     errorflag = 1;
  50.                     break;
  51.                 }
  52.                 if (str[z+1] == '}')
  53.                 {
  54.                     curly--;
  55.                     break;
  56.                 }
  57.             }
  58.         }
  59.         if (str[j] == '[')
  60.         {
  61.             square++;
  62.             for (z = j;z !=strlen(str);z++ )
  63.             {
  64.                 if (str[z+1] == '}' || ')')
  65.                 {
  66.                     errorflag = 1;
  67.                     break;
  68.                 }
  69.                 if (str[z+1] == ']')
  70.                 {
  71.                     square--;
  72.                     break;
  73.                 }
  74.             }
  75.         }
  76.  
  77.     }
  78.     if (round && curly && square == 0)
  79.         printf("Here are unclosed round = %d\n curly = %d\n square = %d\n", round, curly, square);
  80.     else
  81.     {
  82.         if(errorflag == 1)
  83.             printf("Wrong brackets\n");
  84.         else
  85.         printf("Brackets aren't closed!\n");
  86.     }
  87.    // printf("%s\n i=%d", str, i);
  88.     free(str);
  89.     return 0;
  90. }
Add Comment
Please, Sign In to add comment