Advertisement
TAImatem

brackets

Mar 12th, 2017
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdio.h"
  3.  
  4. #define INF 99999999
  5. #define E 0.00000001
  6.  
  7. int abs(int a)
  8. {
  9.     return a < 0 ? -a : a;
  10. }
  11.  
  12. int main()
  13. {
  14.     int i, j, n, maxd = 1, depth[100], fl = 0, col;
  15.     char c, b[250][250];
  16.     scanf("%i\n%c", &n, &c);
  17.     depth[0] = 1;
  18.     for (i = 1; i < n; ++i)
  19.     {
  20.         scanf("%c", &c);
  21.         if (c == '[')
  22.         {
  23.             depth[i] = abs(depth[i - 1] + 1);
  24.         }
  25.         else
  26.         {
  27.             depth[i] = -abs(depth[i - 1]);
  28.         }
  29.         if (depth[i - 1] < 0)
  30.             ++depth[i];
  31.         if (depth[i] > maxd)
  32.             maxd = depth[i];
  33.     }
  34.     b[0][0] = '+';
  35.     for (i = 1; i < maxd * 2; ++i)
  36.         b[i][0] = '|';
  37.     b[maxd * 2][0] = '+';
  38.     col = 1;
  39.     for (j = 1; j < (n-1); ++j, ++col)
  40.     {
  41.         for (i = 0; i < abs(depth[j]) - 2; ++i)
  42.             b[i][col] = ' ';
  43.         if (depth[j] > 0)
  44.         {
  45.             if (depth[j - 1] > 0)
  46.             {
  47.                 b[abs(depth[j]) - 2][col] = '-';
  48.                 fl = 1;
  49.             }
  50.             else
  51.                 b[abs(depth[j]) - 2][col] = ' ';
  52.         }
  53.         else
  54.         {
  55.             if (depth[j - 1] > 0)
  56.             {
  57.                 b[abs(depth[j]) - 2][col] = ' ';
  58.                 b[abs(depth[j]) - 1][col] = '-';
  59.                 for (i = abs(depth[j]); i < (maxd * 2 - abs(depth[j]) + 1); ++i)
  60.                     b[i][col] = ' ';
  61.                 b[i][col] = '-';
  62.                 ++i;
  63.                 for (; i <= maxd * 2; ++i)
  64.                     b[i][col] = ' ';
  65.                 ++col;
  66.                 for (i = 0; i <= maxd * 2; ++i)
  67.                     b[i][col] = ' ';
  68.                 ++col;
  69.                 for (i = 0; i < abs(depth[j]) - 1; ++i)
  70.                     b[i][col] = ' ';
  71.                 b[abs(depth[j]) - 1][col] = '-';
  72.                 for (i = abs(depth[j]); i < (maxd * 2 - abs(depth[j]) + 1); ++i)
  73.                     b[i][col] = ' ';
  74.                 b[i][col] = '-';
  75.                 ++i;
  76.                 for (; i <= maxd * 2; ++i)
  77.                     b[i][col] = ' ';
  78.                 ++col;
  79.                 for (i = 0; i < abs(depth[j]) - 2; ++i)
  80.                     b[i][col] = ' ';
  81.             }
  82.             if (depth[j + 1] < 0)
  83.             {
  84.                 b[abs(depth[j]) - 2][col] = '-';
  85.                 fl = 1;
  86.             }
  87.             else
  88.                 b[abs(depth[j]) - 2][col] = ' ';
  89.         }
  90.         b[abs(depth[j]) - 1][col] = '+';
  91.         for (i = abs(depth[j]); i < (maxd * 2 - abs(depth[j]) + 1); ++i)
  92.             b[i][col] = '|';
  93.         b[i][col] = '+';
  94.         if (fl)
  95.         {
  96.             ++i;
  97.             b[i][col] = '-';
  98.             fl = 0;
  99.         }
  100.         ++i;
  101.         for (; i <= maxd * 2; ++i)
  102.             b[i][col] = ' ';
  103.     }
  104.     if (depth[n-2] > 0)
  105.     {
  106.         b[abs(depth[n-1]) - 2][col] = ' ';
  107.         b[abs(depth[n-1]) - 1][col] = '-';
  108.         for (i = abs(depth[n-1]); i < (maxd * 2 - abs(depth[j]) + 1); ++i)
  109.             b[i][col] = ' ';
  110.         b[i][col] = '-';
  111.         ++i;
  112.         for (; i <= maxd * 2; ++i)
  113.             b[i][col] = ' ';
  114.         ++col;
  115.         for (i = 0; i <= maxd * 2; ++i)
  116.             b[i][col] = ' ';
  117.         ++col;
  118.         for (i = 0; i < abs(depth[n-1]) - 1; ++i)
  119.             b[i][col] = ' ';
  120.         b[abs(depth[n-1]) - 1][col] = '-';
  121.         for (i = abs(depth[n-1]); i < (maxd * 2 - abs(depth[n-1]) + 1); ++i)
  122.             b[i][col] = ' ';
  123.         b[i][col] = '-';
  124.         ++i;
  125.         for (; i <= maxd * 2; ++i)
  126.             b[i][col] = ' ';
  127.         ++col;
  128.         for (i = 0; i < abs(depth[n-1]) - 2; ++i)
  129.             b[i][col] = ' ';
  130.     }
  131.     b[0][col] = '+';
  132.     for (i = 1; i < maxd * 2; ++i)
  133.         b[i][col] = '|';
  134.     b[maxd * 2][col] = '+';
  135.     ++col;
  136.     for (i = 0; i <= maxd * 2; ++i)
  137.         b[i][col] = 0;
  138.     for (i = 0; i <= maxd * 2; ++i)
  139.         printf("%s\n", b[i]);
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement