1. Си / Говнокод #25171

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    #include <stdio.h>
    #include <inttypes.h>
    #include <stddef.h>
    
    void printuint64(const uint64_t state);
    uint64_t game_of_life8x8(const uint64_t old);
    uint8_t getbit(uint64_t in, uint8_t y, uint8_t x);
    
    uint8_t getbit(uint64_t in, uint8_t y, uint8_t x)
    {
      return !!((in) & (1ULL << ((y&0b111) + (x&0b111)*8)) );
    }
    
    uint64_t setbit(uint8_t y, uint8_t x, uint8_t bit)
    {
      if (bit)
      {
        return 1ULL << ((y&0b111) + (x&0b111)*8);
      }
      return 0;
    }
    
    uint64_t game_of_life8x8(const uint64_t old)
    {
      uint64_t new_state = 0;
      #define GETBIT(y,x,val) getbit(val, y, x)
      #define SETBIT(y,x,bit) setbit(y,x,bit)
      
      #define SUMAROUND(y,x,val) ( \
        GETBIT(y+1,x,val) + GETBIT(y-1,x,val) \
      + GETBIT(y,x+1,val) + GETBIT(y,x-1,val) \
      + GETBIT(y+1,x+1,val) + GETBIT(y+1,x-1,val) \
      + GETBIT(y-1,x+1,val) + GETBIT(y-1,x-1,val) \
      )
    
      #define CELLSTATE(y,x,val) \
      (GETBIT(y,x,old) == 1 ? \
        ( ((SUMAROUND(y,x,val) == 2 ) || ( SUMAROUND(y,x,val) == 3 )) ? 1 : 0) \
        : \
        ( ( SUMAROUND(y,x,val) == 3 ) ? 1 : 0 ) \
      )
    
      #define SETCELL_SH(y,x,val) SETBIT(y,x,CELLSTATE(y,x,val))
    
      #define FILL_LINE(y) \
        SETCELL_SH(y,7,old) | SETCELL_SH(y,6,old) | SETCELL_SH(y,5,old) | \
        SETCELL_SH(y,4,old) | SETCELL_SH(y,3,old) | SETCELL_SH(y,2,old) | \
        SETCELL_SH(y,1,old) | SETCELL_SH(y,0,old)
    
      new_state = FILL_LINE(7) | FILL_LINE(6) | FILL_LINE(5)
      |  FILL_LINE(4) | FILL_LINE(3) | FILL_LINE(2)
      |  FILL_LINE(1) | FILL_LINE(0);
    
      return new_state;
    }
    
    void printuint64(const uint64_t state)
    {
      for (size_t i = 7; i != SIZE_MAX; i--)
      {
        for (size_t j = 7; j != SIZE_MAX; j--)
        {
          printf("%u", !!((state >> (j+i*8)) & 1) );
        }
        printf("\n");
      }
    }
    
    int main(void)
    {
      // Glider
      uint64_t state =
      (( 0b01000000ULL ) << 8*7 ) |
      (( 0b00100000ULL ) << 8*6 ) |
      (( 0b11100000ULL ) << 8*5 ) |
      (( 0b00000000ULL ) << 8*4 ) |
      (( 0b00000000ULL ) << 8*3 ) |
      (( 0b00000000ULL ) << 8*2 ) |
      (( 0b00000000ULL ) << 8*1 ) |
      (( 0b00000000ULL ) << 8*0 );
    
      
      for (size_t i = 0; i < 50; i++)
      {
        printuint64(state);
        state = game_of_life8x8(state);
        printf("\n");
      }
      return 0;
    }

    Conway's game of life внутри uint64_t (8 на 8) с периодическими граничными условиями.

    j123123, 09 Декабря 2018

    Комментарии (16)
  2. Си / Говнокод #25156

    0

    1. 1
    2. 2
    3. 3
    4. 4
    UCSR0A |= TXC0_MASK;
    UDR0 = value;
    while ((UCSR0A & TXC0_MASK) == 0)
      ;

    The TXCn flag ... can be cleared by writing a one to its location.
    Cleared by writing one, Carl.

    Steve_Brown, 05 Декабря 2018

    Комментарии (143)
  3. Си / Говнокод #25130

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    #include <stdio.h>
    #include <inttypes.h>
    #include <string.h>
    
    typedef struct
    {
      uint8_t arr[10];
      uint8_t pos;
      uint8_t is_swap;
    } arr10;
    
    arr10 bubble_sort_recursion(arr10 a);
    void bubble_sort (uint8_t arr[static 10]);
    
    
    void bubble_sort (uint8_t arr[static 10])
    {
      arr10 a;
      memcpy(a.arr, arr, sizeof(a.arr));
      a.pos = 0;
      a.is_swap = 0;
      a = bubble_sort_recursion(a);
      memcpy(arr, a.arr, sizeof(a.arr));
    }
    
    arr10 bubble_sort_recursion(arr10 a)
    {
      if (a.pos != 9)
      {
        if (a.arr[a.pos] > a.arr[a.pos + 1])
        {
          uint8_t tmp = a.arr[a.pos + 1];
          a.arr[a.pos + 1] = a.arr[a.pos];
          a.arr[a.pos] = tmp;
          a.is_swap = 1;
        }
        a.pos++;
        return bubble_sort_recursion(a);
      }
      else
      {
       if (a.is_swap == 0)
       {
         return a;
       }
       else
       {
         a.pos = 0;
         a.is_swap = 0;
         return bubble_sort_recursion(a);
       }
      }
    }
    
    int main(void)
    {
      uint8_t arr[10] = {244, 90, 254, 109, 33, 85, 69, 81, 126, 71};
      bubble_sort(arr);
      printf("%" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8,
             arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6], arr[7], arr[8], arr[9]);
      return 0;
    }

    Рекурсивная сортировка пузырьком

    j123123, 29 Ноября 2018

    Комментарии (1)
  4. Си / Говнокод #25084

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    while (*str) {
          if (i >= n)
             return NULL;
          if (!(*str & 0x80))
             buffer[i++] = *str++;
          else if ((*str & 0xe0) == 0xc0) {
             if (*str < 0xc2) return NULL;
             c = (*str++ & 0x1f) << 6;
             if ((*str & 0xc0) != 0x80) return NULL;
             buffer[i++] = c + (*str++ & 0x3f);
          } else if ((*str & 0xf0) == 0xe0) {
             if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return NULL;
             if (*str == 0xed && str[1] > 0x9f) return NULL; // str[1] < 0x80 is checked below
             c = (*str++ & 0x0f) << 12;
             if ((*str & 0xc0) != 0x80) return NULL;
             c += (*str++ & 0x3f) << 6;
             if ((*str & 0xc0) != 0x80) return NULL;
             buffer[i++] = c + (*str++ & 0x3f);
          } else if ((*str & 0xf8) == 0xf0) {
             if (*str > 0xf4) return NULL;
             if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return NULL;
             if (*str == 0xf4 && str[1] > 0x8f) return NULL; // str[1] < 0x80 is checked below
             c = (*str++ & 0x07) << 18;
             if ((*str & 0xc0) != 0x80) return NULL;
             c += (*str++ & 0x3f) << 12;
             if ((*str & 0xc0) != 0x80) return NULL;
             c += (*str++ & 0x3f) << 6;
             if ((*str & 0xc0) != 0x80) return NULL;
             c += (*str++ & 0x3f);
             // utf-8 encodings of values used in surrogate pairs are invalid
             if ((c & 0xFFFFF800) == 0xD800) return NULL;
             if (c >= 0x10000) {
                c -= 0x10000;
                if (i + 2 > n) return NULL;
                buffer[i++] = 0xD800 | (0x3ff & (c >> 10));
                buffer[i++] = 0xDC00 | (0x3ff & (c      ));
             }
          } else
             return NULL;
       }

    // Windows stupidly treats 8-bit filenames as some dopey code page,
    // rather than utf-8. If we want to use utf8 filenames, we have to
    // convert them to WCHAR explicitly and call WCHAR versions of the
    // file functions. So, ok, we do.

    govnokod3r, 13 Ноября 2018

    Комментарии (8)
  5. Си / Говнокод #25076

    +1

    1. 1
    m[7500];char*c=m+3750;char*main(int k,char**a,char*s){return!a?~k?k&&*s?main(k+(*s==91)-(*s==93),0,s+1):s:(*(c+=(*s==62)-(*s==60))+=(*s==43)-(*s==45),*s==44?*c=getchar():*s-46||putchar(*c),*s-93?*s-91?0:!*c?s=main(1,0,s+1)-1:main(-1,0,1+s--),1[s]&&main(-1,0,s+1):0):main(-1,0,1[a]);}

    ...Пройдя долиной ошибок и ворнингов,
    Не убоюсь я ошибок сегментации...

    666_N33D135, 10 Ноября 2018

    Комментарии (29)
  6. Си / Говнокод #25064

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    #inclidr  <stdio.h>
    
    iny maon (vkid) {
        staric char nif[267];
        
        ryoeded cous prox(boid);
        
        whike (!fief(stdim)) {
            printd(">> ");
            peix *f = fgeys(vif, 156, dtdib);
            f();
        }
        returb 0;
    }

    666_N33D135, 08 Ноября 2018

    Комментарии (31)
  7. Си / Говнокод #25047

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    #include <stdio.h>
    
    #define PRIM_CAT_(a, ...) a ## __VA_ARGS__
    
    #define PRIM_CAT(...) PRIM_CAT_(__VA_ARGS__)
    
    #define PRIM_JOIN(sep, a, ...) PRIM_CAT(PRIM_CAT(a, sep), __VA_ARGS__)
    
    #define PRIM_QUOTE_(...) # __VA_ARGS__
    
    #define PRIM_QUOTE(...) PRIM_QUOTE_(__VA_ARGS__)
    
    #define PRIM_EAT(...) /* nothing */
    
    #define PRIM_EXPAND(...) __VA_ARGS__
    
    #define PRIM_HEAD_(x, ...) x
    
    #define PRIM_HEAD(...) PRIM_HEAD_(__VA_ARGS__)
    
    #define PRIM_TAIL_(x, ...) __VA_ARGS__
    
    #define PRIM_TAIL(...) PRIM_TAIL_(__VA_ARGS__)
    
    #define PRIM_CHECK(...) PRIM_CHECK_N(__VA_ARGS__, 0)
    
    #define PRIM_CHECK_N(x, n, ...) n
    
    #define PRIM_PROBE(x) x, 1
    
    #define PRIM_TEST(...) int main(){puts(PRIM_QUOTE(__VA_ARGS__)); return 0;}
    
    #define BOOL_TO_BOOL(x) BOOL_COMPL(BOOL_NOT(x))
    
    #define BOOL_COMPL(x) PRIM_CAT(BOOL_COMPL_, x)
    #define BOOL_COMPL_1 0
    #define BOOL_COMPL_0 1
    
    #define BOOL_NOT(x) PRIM_CHECK(PRIM_CAT(BOOL_NOT_, x))
    #define BOOL_NOT_0 PROBE(?)
    
    #define BOOL_AND(x, y) PRIM_CAT(BOOL_AND_, x)(y)
    #define BOOL_AND_0(x) 0
    #define BOOL_AND_1(x) x
    
    #define BOOL_OR(x, y) PRIM_CAT(BOOL_OR_, x)(y)
    #define BOOL_OR_1(x) 1
    #define BOOL_OR_0(x) x
    
    #define BOOL_XOR(x, y) PRIM_CAT(BOOL_XOR_, x)(y)
    #define BOOL_XOR_1(x) BOOL_COMPL(x)
    #define BOOL_XOR_0(x) x
    
    #define BOOL_EQ(x, y) BOOL_COMPL(BOOL_XOR(x, y))
    
    #define ARITH_ADD_(a7, a6, a5, a4, a3, a2, a1, a0,   b7, b6, b5, b4, b3, b2, b1, b0) \
      BOOL_XOR(BOOL_XOR(a7, b7), BOOL_OR(BOOL_AND(a6, b6), BOOL_AND(BOOL_XOR(a6, b6), BOOL_OR(BOOL_AND(a5, b5), BOOL_AND(BOOL_XOR(a5, b5), BOOL_OR(BOOL_AND(a4, b4), BOOL_AND(BOOL_XOR(a4, b4), BOOL_OR(BOOL_AND(a3, b3), BOOL_AND(BOOL_XOR(a3, b3), BOOL_OR(BOOL_AND(a2, b2), BOOL_AND(BOOL_XOR(a2, b2), BOOL_OR(BOOL_AND(a1, b1), BOOL_AND(BOOL_XOR(a1, b1), BOOL_AND(a0, b0)))))))))))))), \
      BOOL_XOR(BOOL_XOR(a6, b6), BOOL_OR(BOOL_AND(a5, b5), BOOL_AND(BOOL_XOR(a5, b5), BOOL_OR(BOOL_AND(a4, b4), BOOL_AND(BOOL_XOR(a4, b4), BOOL_OR(BOOL_AND(a3, b3), BOOL_AND(BOOL_XOR(a3, b3), BOOL_OR(BOOL_AND(a2, b2), BOOL_AND(BOOL_XOR(a2, b2), BOOL_OR(BOOL_AND(a1, b1), BOOL_AND(BOOL_XOR(a1, b1), BOOL_AND(a0, b0)))))))))))), \
      BOOL_XOR(BOOL_XOR(a5, b5), BOOL_OR(BOOL_AND(a4, b4), BOOL_AND(BOOL_XOR(a4, b4), BOOL_OR(BOOL_AND(a3, b3), BOOL_AND(BOOL_XOR(a3, b3), BOOL_OR(BOOL_AND(a2, b2), BOOL_AND(BOOL_XOR(a2, b2), BOOL_OR(BOOL_AND(a1, b1), BOOL_AND(BOOL_XOR(a1, b1), BOOL_AND(a0, b0)))))))))), \
      BOOL_XOR(BOOL_XOR(a4, b4), BOOL_OR(BOOL_AND(a3, b3), BOOL_AND(BOOL_XOR(a3, b3), BOOL_OR(BOOL_AND(a2, b2), BOOL_AND(BOOL_XOR(a2, b2), BOOL_OR(BOOL_AND(a1, b1), BOOL_AND(BOOL_XOR(a1, b1), BOOL_AND(a0, b0)))))))), \
      BOOL_XOR(BOOL_XOR(a3, b3), BOOL_OR(BOOL_AND(a2, b2), BOOL_AND(BOOL_XOR(a2, b2), BOOL_OR(BOOL_AND(a1, b1), BOOL_AND(BOOL_XOR(a1, b1), BOOL_AND(a0, b0)))))), \
      BOOL_XOR(BOOL_XOR(a2, b2), BOOL_OR(BOOL_AND(a1, b1), BOOL_AND(BOOL_XOR(a1, b1), BOOL_AND(a0, b0)))), \
      BOOL_XOR(BOOL_XOR(a1, b1), BOOL_AND(a0, b0)), \
      BOOL_XOR(a0, b0)
    
    #define ARITH_ADD(...) ARITH_ADD_(__VA_ARGS__)
    
    #define ARITH_INC_(b7, b6, b5, b4, b3, b2, b1, b0) ARITH_ADD(b7, b6, b5, b4, b3, b2, b1, b0,  0, 0, 0, 0, 0, 0, 0, 1)
    
    #define ARITH_INC(...) ARITH_INC_(__VA_ARGS__)
    
    #define ARITH_NEGATE_(b7, b6, b5, b4, b3, b2, b1, b0) ARITH_INC(BOOL_COMPL(b7), BOOL_COMPL(b6), BOOL_COMPL(b5), BOOL_COMPL(b4), BOOL_COMPL(b3), BOOL_COMPL(b2), BOOL_COMPL(b1), BOOL_COMPL(b0))
    
    #define ARITH_NEGATE(...) ARITH_NEGATE_(__VA_ARGS__)
    
    #define ARITH_SUB_(a7, a6, a5, a4, a3, a2, a1, a0,  b7, b6, b5, b4, b3, b2, b1, b0) ARITH_ADD(a7, a6, a5, a4, a3, a2, a1, a0, ARITH_NEGATE(b7, b6, b5, b4, b3, b2, b1, b0))
    
    #define ARITH_SUB(...) ARITH_SUB_(__VA_ARGS__)
    
    #define ARITH_DEC_(a7, a6, a5, a4, a3, a2, a1, a0) ARITH_SUB(a7, a6, a5, a4, a3, a2, a1, a0,  0, 0, 0, 0, 0, 0, 0, 1)
    
    #define ARITH_DEC(...) ARITH_DEC_(__VA_ARGS__)
    
    #define ARITH_TO_C_NUMBER_(b7, b6, b5, b4, b3, b2, b1, b0) PRIM_CAT(0b, PRIM_CAT(b7, PRIM_CAT(b6, PRIM_CAT(b5, PRIM_CAT(b4, PRIM_CAT(b3, PRIM_CAT(b2, PRIM_CAT(b1, b0))))))))
    
    #define ARITH_TO_C_NUMBER(...) ARITH_TO_C_NUMBER_(__VA_ARGS__)
    
    PRIM_TEST(
        ARITH_TO_C_NUMBER(ARITH_ADD(0,0,0,0,0,0,1,1, ARITH_INC(0,1,0,1,1,1,1,1))),
        ARITH_TO_C_NUMBER(ARITH_DEC(0,0,0,0,0,0,0,0))
    )

    А я всё не уймусь...


    https://ideone.com/pudErG

    adrnin, 02 Ноября 2018

    Комментарии (10)
  8. Си / Говнокод #25043

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    #include <stdio.h>
    //#include <ynopoTocTb.h>
    
    #define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
    #define CAT(...) PRIMITIVE_CAT(__VA_ARGS__)
    
    #define EAT(...)
    #define EXPAND(...) __VA_ARGS__
    #define IIF(c) PRIMITIVE_CAT(IIF_, c)
    #define IIF_1(...) __VA_ARGS__ EAT
    #define IIF_0(...) EXPAND
    
    #define COMPL(x) PRIMITIVE_CAT(COMPL_, x)
    #define COMPL_1 0
    #define COMPL_0 1
    
    #define CHECK_N(x, n, ...) n
    #define CHECK(...) CHECK_N(__VA_ARGS__, 0)
    #define PROBE(x) x, 1
    
    #define NOT(x) CHECK(PRIMITIVE_CAT(NOT_, x))
    #define NOT_0 PROBE(~)
    #define BOOL(x) COMPL(NOT(x))
    
    #define IF(c) IIF(BOOL(c))
    
    #define EMPTY()
    #define DEFER(id) id EMPTY()
    #define OBSTRUCT(id) id DEFER(EMPTY)()
    
    #define  EVAL(...) EVAL1(EVAL1(EVAL1(EVAL1(__VA_ARGS__))))
    #define EVAL1(...) EVAL2(EVAL2(EVAL2(EVAL2(__VA_ARGS__))))
    #define EVAL2(...) EVAL3(EVAL3(EVAL3(EVAL3(__VA_ARGS__))))
    #define EVAL3(...) EVAL4(EVAL4(EVAL4(EVAL4(__VA_ARGS__))))
    #define EVAL4(...) EVAL5(EVAL5(EVAL5(EVAL5(__VA_ARGS__))))
    #define EVAL5(...) __VA_ARGS__
    
    #define DEC(x) PRIMITIVE_CAT(DEC_, x)
    #define DEC_0 0
    #define DEC_1 0
    #define DEC_2 1
    #define DEC_3 2
    #define DEC_4 3
    #define DEC_5 4
    #define DEC_6 5
    #define DEC_7 6
    #define DEC_8 7
    #define DEC_9 8
    
    #define FACTORIAL(x) \
      IF(NOT(x)) (1) \
      ( \
        OBSTRUCT(FACTORIAL_INDIRECT)()(DEC(x)) * x \
      )
    
    #define FACTORIAL_INDIRECT() FACTORIAL
    
    #define PRIMITIVE_TO_STR(...) # __VA_ARGS__
    #define TO_STR(...) PRIMITIVE_TO_STR(__VA_ARGS__)
    
    int main() {
        printf("7! = " TO_STR(EVAL(FACTORIAL(7))) " = %d", EVAL(FACTORIAL(7)));
        return 0;
    }

    Продолжаю макроёбить.
    https://ideone.com/WcG7i2

    Использованы материалы из стотьи
    https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms

    adrnin, 01 Ноября 2018

    Комментарии (53)
  9. Си / Говнокод #25039

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    97. 97
    98. 98
    99. 99
    //macrolib.h
    
    #ifndef MACRO_LIB
    #define MACRO_LIB
    
    #include <stdio.h>
    
    typedef void proc(void);
    
    #ifdef DEBUG
      #define TRACE printf
    #else
      #define TRACE(...)
    #endif
    
    #ifdef TRACE_CALLS
      #define ON_ENTER_TRACE(name) char __CUR_FUNC_NAME__[] = #name; TRACE("<entering %s>", __CUR_FUNC_NAME__);
      #define ON_EXIT_TRACE(...) TRACE(__VA_ARGS__)
    #else
      #define ON_ENTER_TRACE(name) ;
      #define ON_EXIT_TRACE(...)
    #endif
    
    #define def(type, name, ...) type name __VA_ARGS__ { ON_ENTER_TRACE(name)
    #define ret(ret_val) {ON_EXIT_TRACE("<leaving %s>", __CUR_FUNC_NAME__); return ret_val;}
    #define end(ret_val) ret(ret_val);}
    
    #define with(type, x, on_exit) { type __CUR_WITH_VAR__ = x; int (*__ON_EXIT__)() = (int(*)())on_exit;
    #define endwith __ON_EXIT__(__CUR_WITH_VAR__);}
    
    #define dup(d, ...) __VA_ARGS__ d __VA_ARGS__
    #define dupwithcomma(...) __VA_ARGS__, __VA_ARGS__
    
    #endif
    
    
    
    
    //chain.h
    
    #ifdef I0
      #undef I0
      #if defined(I1) || defined(I2) || defined(I3)
        ELEMENT DELIMETER
        #include "chain.h"
      #else
        ELEMENT
        #undef ELEMENT
        #undef DELIMETER
      #endif
    #else
      #ifdef I1
        #undef I1
        #define I0
        ELEMENT DELIMETER
        #include "chain.h"
      #else
        #ifdef I2
          #undef I2
          #define I1
          #define I0
          ELEMENT DELIMETER
          #include "chain.h"
        #else
          #ifdef I3
            #undef I3
            #define I2
            #define I1
            #define I0
            ELEMENT DELIMETER
            #include "chain.h"
          #endif
        #endif
      #endif
    #endif
    
    //test.c
    
    #define DEBUG
    #define TRACE_CALLS
    
    #include "macrolib.h"
    
    def(int, main, ())
      printf(dup(" ", "%s"), dupwithcomma("Чот мне понравилось макроёбить..."));
      FILE *f = fopen("file", "w");
      with(FILE*, f, fclose)
        fputs(
          #define ELEMENT "o"
          #define DELIMETER "l"
          #define I3
          #define I2
          #define I1
          #define I0
          #include "chain.h"
          , f
        );
      endwith
    end(0)

    Мне понравилось. то ещё можно намакроёбить? ;D

    adrnin, 30 Октября 2018

    Комментарии (25)
  10. Си / Говнокод #25036

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    #include <stdio.h>
    
    #ifdef DEBUG
      #define TRACE printf
    #else
      #define TRACE(...)
    #endif
    
    #define DEF(type, name, ...) type name __VA_ARGS__ { TRACE("<вызвали " #name ">");
    #define RET(name, ret_val) {TRACE("<" #name " не упала>"); return (ret_val);}
    #define END(name, ret_val) RET(name, ret_val);}
    
    DEF(int, main, (int argc, char **argv))
        puts("Моя супер-мего отладка ;D");
    END(main, 0)

    Моя супир-мего отладка ;D

    adrnin, 29 Октября 2018

    Комментарии (210)