1. Список говнокодов пользователя 3.14159265

    Всего: 150

  2. Си / Говнокод #28292

    +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 <stdlib.h>
    #include <math.h>
    
    #define NUMARGS(type,...)  (sizeof((type[]){__VA_ARGS__})/sizeof(type))
    #define xDEF(type) typedef struct {size_t size; type* arr;  }  xARR(type)
    #define xARR(type) jArr_ ## type
    
    #define A(type, ...) (xARR(type)) {NUMARGS(type,__VA_ARGS__), (type[]){__VA_ARGS__}}
    
    #define _FOR(type, item, Arr) type item=Arr.arr[0]; for(size_t I = 0; I < Arr.size; item=Arr.arr[++I] ) 
    // MSVC
    #define xFOR(type, item, array) do{  _FOR(type, item, array) {
    // GCC, Clang
    #define FOR(item, array)        do{ __auto_type Arr=array; _FOR(__auto_type, item, Arr) {
        
    #define NEXT }} while(0);
    
    #define OfArray(type,arr) (xARR(type)){sizeof(arr)/sizeof(type), arr }
    
    typedef struct {
        char *name;
        int     id;
    } Entry;
    
    
    typedef struct {const char *name;} Str;
    typedef struct {int x[2]; } Pair;
    
    xDEF(Entry);
    xDEF(Str);
    xDEF(Pair);
    xDEF(int);
    
    void printEntry(xARR(Entry) entries)
    {
        xFOR(Entry, e, entries)
            printf("%s %d \n", e.name, e.id);
        NEXT
    }
    
    void printSquares(xARR(int) ints)
    {
        FOR(v, ints)
            printf("%d²=%d\n", v,(int) pow(v,2.));
        NEXT
    }
    
    int main(void)
    {
        xARR(Entry) e = A(Entry, {"one",1}, {"two",2}, {"three",3});
        printEntry(e);
        
        puts("_______________________________________");
        
        // можно передавать в метод непосредственно аргуметом
        printSquares( A(int, 3, 7, 5, 4) );
        puts("_______________________________________");    
        
        int nums[]={4,3,2,1};
        // можно использовать ранее объявленный массив
        printSquares(OfArray(int,nums));
        
        // можно итерироватьcя по ранее объявленному массиву
        FOR(i, OfArray(int, nums))
            printf("%d-",i);
        NEXT
        
        puts("\n_______________________________________");
        
        //вложенные циклы:
        for (int k=1;k<3;k++)
            FOR(i, A(Str, "kakoi", "bagor"))    
                FOR(j, A(int, 1111,2222,3333))
                    printf("%d %s %d\n", k, i.name, j);
                NEXT
            NEXT
        
        puts("_______________________________________");
        
        FOR(v, A(Pair, {1,2}, {11,12}, {20,21}))
            printf("%d,%d\n", v.x[0], v.x[1]);
        NEXT
        puts("_______________________________________");    
        //проблема пустого варарга
        FOR(j, A(int))
            printf("%d\n", j);
        NEXT    
        return(0);
    }

    https://godbolt.org/z/o9Tv9EvGx

    Довёл for~each до ума.

    3.14159265, 23 Июля 2022

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

    −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
    #define BITS 8
    
    typedef union
    {
        int v;
        struct 
        {
            #define FIELD(x,_) int b##x:1;        
            EVAL(REPEAT(BITS, FIELD, ~))
            #undef FIELD
        };
    } Num;
    
    Num shl(Num n, int carry)
    {
        #define SHIFTL(x,_) CAT(n.b, CAT(x = n.b, CAT(DEC_,x)));    
        EVAL(RREPEAT(BITS, SHIFTL, ~))
        #undef SHIFTL
        n.b0 = carry;
    }
    
    Num shr(Num n, int carry)
    {
        #define SHIFTR(x,_) CAT(n.b, CAT(CAT(DEC_,x) = n.b, x));
        EVAL(REPEAT(BITS, SHIFTR, ~))
        #undef SHIFTR
        CAT(n.b, CAT(DEC_,BITS)) = carry;
    }
    
    
    int main()
    {
        for (int i=0; i<33; ++i){
            Num n   = {i};
            Num n1 = shl(n,0);
            Num n2 = shr(n,0);        
            printf("%d %d %d\n",n ,n1 , n2);
        }
    }

    https://godbolt.org/z/48h6EWacY

    Двунаправленный сдвиговый регистр на препроцессоре.
    Сделан без использования арифметических действий.

    3.14159265, 21 Июля 2022

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

    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
    int main(int argc, char **argv) {
      void *vp;
      const char *version_str;
    
      if (argc != 2) {
        fprintf(stderr, "usage: %s <version>\n", argv[0]);
        exit(1);
      }
    
      vp = malloc(strlen(argv[1]) + 1);
      strcpy(vp, argv[1]);
    
      version_str = check_header(vp);
      free(vp);
    
      printf("0.%s\n", version_str);
    
      return 0;
    }

    Там Беллард с научил вореции программировать на Сишке

    https://textsynth.com/playground.html

    3.14159265, 31 Января 2022

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

    +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
    #include <stdio.h>
    #include <string.h>
    
    int main() 
    {
        char* lalka = "bagor";
        if (0!=strcmp(lalka, "bagor‮ ⁦// comment⁩ ⁦")) {
            puts("Kakoi gabor )))");
        }
        if (0!=strcmp(lalka, "bagor")){
            puts("Pituz!");
        }    
        return 0;
    }

    https://ideone.com/RiM3LX

    #stdout
    Kakoi gabor )))

    3.14159265, 27 Января 2022

    Комментарии (3)
  6. Куча / Говнокод #27662

    +2

    1. 1
    (format t "~A~%" (*) )

    https://ideone.com/oO7f4I

    3.14159265, 13 Сентября 2021

    Комментарии (8)
  7. bash / Говнокод #27650

    +4

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    #!/bin/bash 
    # apt install jq w3m curl
    
    curl https://gcode.space/api/comments  \
    | jq -r 'map("-"*99+"<br>"+.user_name+" "+.posted+"<br>","<ul>"+.text+"</ul>") | reduce .[] as $item (""; . + $item) '  \
    |  w3m -T text/html -cols 99 -dump \
    | less -R

    Консольная читалка для стока ГК

    3.14159265, 08 Сентября 2021

    Комментарии (214)
  8. Куча / Говнокод #27646

    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
    pub fn take(end: u32) -> u32 
    {
        (0..).step_by(1000000000)
            .map(|i| i * i)
            .take_while(|&i| i < end)
            .sum()
    }
    pub fn filter(end: u32) -> u32 
    {
        (0..).step_by(1000000000)
            .map(|i| i * i)
            .filter(|&i| i < end)
            .sum()
    }

    Решил вернуться к изучению багра rust.
    Суть такова: код с take_while выводит 0, код с filter падает с runtime error или выбрасывается rustClang как бесконечный цикл (зависит от версии).

    https://ideone.com/IS05Q0

    То есть код filter ма-те-ма-ти-че-ски эквивалентен take_while. Поскольку i² монотонно возрастающая функция.

    Из примера ниже можно убедиться что цикл на самом деле конечен.
    https://ideone.com/xC2r35
    Счётчик кидает ошибку при переполнении и range не зацикливается.

    for x in (1..).step_by(1000000000) {
    println!("{}", x);
    }

    1
    1000000001
    2000000001
    Runtime error #stdin #stdout #stderr 0.01s 5552KB

    3.14159265, 05 Сентября 2021

    Комментарии (102)
  9. JavaScript / Говнокод #27639

    +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
    Complex numbers:
    >> Complex()({r: 2, i: 0} / {r: 1, i: 1} + {r: -3, i: 2}))
    <- {r: -2, i: 1}
    
    Automatic differentiation:
    Let f(x) = x^3 - 5x:
    >> var f = x => Dual()(x * x * x - {x:5, dx:0} * x);
    
    Now map it over some values:
    >> [-2,-1,0,1,2].map(a=>({x:a,dx:1})).map(f).map(a=>a.dx)
    <- [ 7, -2, -5, -2, 7 ]
    i.e. f'(x) = 3x^2 - 5.
    
    Polynoomials:
    >> Poly()([1,-2,3,-4]*[5,-6]).map((c,p)=>''+c+'x^'+p).join(' + ')
    <- "5x^0 + -16x^1 + 27x^2 + -38x^3 + 24x^4"

    В ЙажаСцрипт завезли перегрузку операторов.
    https://gist.github.com/pyrocto/5a068100abd5ff6dfbe69a73bbc510d7

    3.14159265, 02 Сентября 2021

    Комментарии (6)
  10. Куча / Говнокод #27632

    +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
    https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/pidigits.html
    
    1.0 	C gcc #6	0.59 	2,444 	1090 	2.37 	100% 100% 100% 98%
    1.1 	C++ g++ #6	0.66 	5,152 	986 	2.63 	100% 100% 100% 100%
    1.2 	Rust #4	 	0.71 	2,672 	799 	0.73 	3% 0% 0% 100% 
    1.2 	Free Pascal #3	0.73 	2,268 	530 	0.73 	0% 0% 100% 0%
    1.4 	F# .NET #6	0.82 	34,428 	905 	0.83 	1% 2% 96% 1%
    1.4 	Haskell GHC #5	0.83 	6,056 	928 	0.84 	0% 99% 1% 1%
    1.5 	Ada 2012 GNAT   0.88 	4,704 	1130 	0.89 	0% 0% 100% 1%
    1.5 	Rust #2	 	0.88 	2,800 	1306 	0.89 	0% 1% 100% 0%
    1.5 	C++ g++ #4	0.89 	4,280 	513 	0.92 	0% 2% 1% 100%
    1.5 	OCaml #7	0.89 	5,968 	593 	0.90 	0% 0% 1% 100%
    1.5 	Swift #2	0.89 	9,256 	600 	0.91 	3% 0% 0% 99%
    1.5 	PHP #5	  	0.91 	13,196 	399 	0.96 	2% 0% 3% 100%
    1.5 	C# .NET #5	0.92 	35,404 	977 	0.96 	98% 3% 2% 1%
    1.6 	Java  #3	0.93 	36,552 	764 	0.98 	2% 3% 1% 99%

    However, Java is one of the fastest and most energy-efficient object-oriented language. Interpreted languages like Perl, Python, and Ruby were among the least energy efficient

    Using the Computer Benchmarks Game, the team of researchers tested these languages by compiling/executing such programs using the state-of-the-art compilers, virtual machines, interpreters, and libraries.

    They then analyzed the performance of the different implementation considering three variables: execution time, memory consumption and energy consumption.

    https://jaxenter.com/energy-efficient-programming-languages-137264.html

    https://jaxenter.com/wp-content/uploads/2017/09/energy-efficient-languages-768x689.png

    https://jaxenter.com/wp-content/uploads/2017/09/energy-efficient-languages-2-768x368.png

    3.14159265, 31 Августа 2021

    Комментарии (43)
  11. C# / Говнокод #27628

    +3

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    var actions = new List<Action>();
    foreach (var i in Enumerable.Range(1, 3))
    {
        actions.Add(() => Console.WriteLine(i));
    }
    
    foreach (var action in actions)
    {
        action();
    }

    По мотивам https://govnokod.ru/11946

    Просто форкнул и запустил старый пример LispGovno (мир ему)

    Старый пример: https://ideone.com/RaiHr

    Новый пример: https://ideone.com/M1ducs

    Однако получил совершенно другой результат.

    3.14159265, 29 Августа 2021

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