1. C++ / Говнокод #26943

    +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
    #include <vector>
    #include <cwchar>
    #include <algorithm>
    #include <iostream>
     
    int main()
    {
        std::vector<const wchar_t*> leaders{L"Ленин", L"Сталин", L"Маленков",
            L"Хрущёв", L"Брежнев", L"Андропов", L"Черненко", L"Горбачёв"};
     
        std::sort(leaders.begin(), leaders.end(), [](auto strA, auto strB) {
            return std::wcscmp(strA, strB) < 0;
        });
     
        std::setlocale(LC_ALL, "en_US.utf8");
        std::wcout.imbue(std::locale("en_US.utf8"));
        for (auto leader : leaders)
            std::wcout << leader << '\n';
    }

    Отсюда:
    https://en.cppreference.com/w/cpp/string/wide/wcscmp

    Naf-Naf, 11 Сентября 2020

    Комментарии (14)
  2. C++ / Говнокод #26913

    +3

    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
    int main()
    {   
        using output1 = Eval<
            Input<'H', 'e', 'l', 'l', 'o'>,
            App<
                ',', '>', ',', '>', ',', '>', ',', '>', ',', '>',
                '<', '.', '<', '.', '<', '.', '<', '.', '<', '.'
            >
        >;
        std::cout << "Hello reverse (read/write): " << SpanToStringContinuous<output1>::value() << std::endl;
    
        using output2 = Eval<
            Input<>,
            App<'+', '+', '+', '[', '-', ']'>
        >;
        std::cout << "Simple loop (empty output): " << SpanToStringContinuous<output2>::value() << std::endl;
    
        // Source: Wikipedia
        using output3 = Eval<
            Input<>,
            App<
                '+', '+', '+', '+', '+', '+', '+', '+', '[', '>', '+', '+', '+',
                '+', '[', '>', '+', '+', '>', '+', '+', '+', '>', '+', '+', '+',
                '>', '+', '<', '<', '<', '<', '-', ']', '>', '+', '>', '+', '>',
                '-', '>', '>', '+', '[', '<', ']', '<', '-', ']', '>', '>', '.',
                '>', '-', '-', '-', '.', '+', '+', '+', '+', '+', '+', '+', '.',
                '.', '+', '+', '+', '.', '>', '>', '.', '<', '-', '.', '<', '.',
                '+', '+', '+', '.', '-', '-', '-', '-', '-', '-', '.', '-', '-',
                '-', '-', '-', '-', '-', '-', '.', '>', '>', '+', '.', '>', '+',
                '+', '.'
            >
        >;
        std::cout << "Hello World (wiki): " << SpanToStringContinuous<output3>::value() << std::endl;
    
    
        return EXIT_SUCCESS;
    }

    https://wandbox.org/permlink/AERueBhsiS4WxGZY, https://pastebin.com/Cywe05JY

    Напейсал полностью компайл-таймовый интерпретатор «Брейнфака» на крестовых шаблонах.

    gost, 03 Сентября 2020

    Комментарии (104)
  3. C++ / Говнокод #26912

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    void tick(void)
    {
    	for (auto&& x : registry.objects)
    		(x? std::function<void(void)>([&](void) {((IObject*)(x))->tick(); }) : [&]() {})();
    }

    Мозг сказал что "((IObject*)(x))->tick();" написать слишком сложно и повелел написать вот это.

    digitalEugene, 03 Сентября 2020

    Комментарии (30)
  4. C++ / Говнокод #26911

    +3

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    int main()
    {
        + ! - ! -- -- ! + ! - - - ++ ~ - ! -- -- -- ~ ~ ~ ++ ++ ++ - ~ ++ - * - - -
        * ++ * * ++ * -- -- -- -- - * -- + * ++ ++ ++ ++ ++ * ++ * -- -- * -- - *
        
        BRAINFUCK;
        
        return 0;
    }

    https://ideone.com/EtK1J4

    Ответ на https://govnokod.ru/26910. Теперь с реальной конпеляцией в нативный код.

    bormand, 03 Сентября 2020

    Комментарии (13)
  5. C++ / Говнокод #26910

    +4

    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
    // https://govnokod.ru/26890#comment571155
    // bormand 2 часа назад #
    // Можно брейнфак запилить на операторах. Как раз вроде унарных хватает.
    // & * - ~ ! -- + ++ --
    
    #include <array>
    #include <vector>
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <limits>
    
    struct Brainfuck {
    public:
        using IPType = uint16_t;
        constexpr static size_t MAX_MEMORY = std::numeric_limits<IPType>::max();
    
        std::array<uint8_t, MAX_MEMORY> memory{};
        std::vector<char> app{};
        IPType ip = 0;
        IPType cell = 0;
    
        IPType find_matching_tag(IPType cur_ip, char open, char close, int ip_direction)
        {
            size_t stack_size = 0;
            do  {
                if (app[cur_ip] == close) {
                    --stack_size;
                }
                if (app[cur_ip] == open) {
                    ++stack_size;
                }
                cur_ip += ip_direction;
            } while (stack_size > 0);
            return cur_ip - ip_direction;
        }
    
        IPType find_matching_close_tag(IPType cur_ip)
        {
            return find_matching_tag(cur_ip, '[', ']', 1);
        }
    
        IPType find_matching_open_tag(IPType cur_ip)
        {
            return find_matching_tag(cur_ip, ']', '[', -1);
        }
    
        void loop_open()
        {
            if (memory[cell] == 0) {
                ip = find_matching_close_tag(ip);
            } else {
                ++ip;
            }
        }
    
        void loop_close()
        {
            if (memory[cell] != 0) {
                ip = find_matching_open_tag(ip);
            } else {
                ++ip;
            }
        }
    
        void exec(char op)
        {
            switch (op) {
                case '>': ++cell; break;
                case '<': --cell; break;
                case '+': ++memory[cell]; break;
                case '-': --memory[cell]; break;
                case '.': std::putchar(memory[cell]); break;
                case ',': memory[cell] = static_cast<uint8_t>(std::getchar()); break;
                case '[': loop_open(); return;   // no ip advancing
                case ']': loop_close(); return;  // no ip advancing
            }
            ip++;
        }
    
        void run()
        {
            while (ip < app.size()) {
                exec(app[ip]);
            }
        }
    
    public:
        Brainfuck & operator++() { app.push_back('>'); return *this; }
        Brainfuck & operator--() { app.push_back('<'); return *this; }
        Brainfuck & operator+() { app.push_back('+'); return *this; }
        Brainfuck & operator-() { app.push_back('-'); return *this; }
        Brainfuck & operator*() { app.push_back('.'); return *this; }
        Brainfuck & operator&() { app.push_back(','); return *this; }
        Brainfuck & operator!() { app.push_back('['); return *this; }
        Brainfuck & operator~() { app.push_back(']'); return *this; }
        Brainfuck & operator>>(const Brainfuck &) { run(); return *this; }
    };

    https://wandbox.org/permlink/XJUKGyb4YbnBVwOI

    Бонус («99 bottles of beer»): https://pastebin.com/s4sBK9nX (взято с https://github.com/fabianishere/brainfuck/blob/master/examples/bottles-1.bf).

    gost, 03 Сентября 2020

    Комментарии (244)
  6. C++ / Говнокод #26908

    +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
    void Argument::parseAsInt()
    {
        auto res = std::from_chars(data.data(), data.data() + data.size(), dataInt);
        if (res.ec == std::errc()) {
            setTypeFlag(ArgType::Int);
        }
    }
    
    void Argument::parseAsFloat()
    {
        // Rww: gcc still does not support float from_chars(), lol
        const char *begin = data.data();
        const char *end = begin + data.size();
        char *endPtr = nullptr;
    
        dataFloat = std::strtof(begin, &endPtr);
        if (endPtr == end || dataFloat != 0.0f) {
            setTypeFlag(ArgType::Float);
        } else {
            for (const char *it = endPtr; it < end; it++) {
                if (!std::isspace(*it)) {
                    return;
                }
            }
            setTypeFlag(ArgType::Float);
        }
    }

    Говнокодил тут недавно, долго думал, что считать числом (пет, ТЗ нет). В конце-концов решил считать всё, что можно распарсить.

    gost, 02 Сентября 2020

    Комментарии (14)
  7. C++ / Говнокод #26905

    +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
    92. 92
    93. 93
    //--------------------------------------------------------------------------------------------------------------------\\
    
        float max_rotation = record->m_pEntity->GetMaxBodyRotation();
       
    
        auto animstate = ent->m_PlayerAnimState();
        float resolve_value = 50.f; //не трогайте это, так и должно быть
    
        if (!record->m_pState)
            return;
    
        const auto info = g_anims.GetAnimationInfo(record->m_pEntity);
        if (!info)
            return;
    
        float eye_yaw = record->m_pState->m_flEyeYaw;
    
        if (max_rotation < resolve_value)
            resolve_value = max_rotation;
    
        data->m_extending = record->m_pLayers[3].m_cycle == 0.f && record->m_pLayers[3].m_weight == 0.f;
        float Delta = AngleDiff(ent->m_angEyeAngles().y, animstate->m_flGoalFeetYaw);
    
        //--------------------------------------------------------------------------------------------------------------------\\
                                                           RESOLVERPART
        //--------------------------------------------------------------------------------------------------------------------\\
    
        if (data->m_extending)
            resolve_value = max_rotation;
    
        if (ent->m_AnimOverlay()[13].m_weight + 14.250f > 0.54)
        {
            if (ent->m_AnimOverlay()[3].m_cycle > 0.12)
            {
                if (ent->m_AnimOverlay()[13].m_cycle > 0.43)
                {
                    animstate->m_flGoalFeetYaw = ent->m_angEyeAngles().y;
                }
            }
        }
    
        if (data->m_extending)
        {
            if (Delta <= 0)
            {
                animstate->m_flGoalFeetYaw = Delta * resolve_value;
            }
            else if (Delta > 0)
            {
                animstate->m_flGoalFeetYaw = Delta * -resolve_value;
            }
        }
    
        else if (ent->m_AnimOverlay()[7].m_order == record->m_pLayers[7].m_order)
        {
            if (ent->m_AnimOverlay()[7].m_cycle > 0.5f)
            {
                animstate->m_flGoalFeetYaw = math::NormalizeYaw(record->m_pEntity->m_flLowerBodyYawTarget()) + get_max_desync_delta;
                return;
            }
            else
                animstate->m_flGoalFeetYaw = record->m_pEntity->m_angEyeAngles().y;
        }
    
        if (record->m_bDidShot)
            info->m_flBrute = Resolver::ResolveShot(data, record);
    
        //--------------------------------------------------------------------------------------------------------------------\\
                                                        BRUTEFORCE PART
        //--------------------------------------------------------------------------------------------------------------------\\
    
        else {
            float lbyt = record->m_pEntity->m_flLowerBodyYawTarget();
            data->m_delta = std::abs(math::NormalizedAngle(eye_yaw - lbyt));
    
            float resolve_yaw = ((data->m_delta < 0.f) ? resolve_value : resolve_value);
    
            switch (data->m_missed_shots % 3) {
            case 0:
                info->m_flBrute = data->m_last_resolve = resolve_yaw;
                break;
            case 1:
                info->m_flBrute = -data->m_last_resolve;
                break;
            case 2:
                info->m_flBrute = 0;
                break;
            }
        }
    
        record->m_pState->m_flGoalFeetYaw = eye_yaw + info->m_flBrute;
    
        //--------------------------------------------------------------------------------------------------------------------\\

    best resolver

    danielka33, 02 Сентября 2020

    Комментарии (0)
  8. C++ / Говнокод #26884

    +1

    1. 1
    2. 2
    3. 3
    bool isComputerOn() noexcept {
      return true;
    }

    Лицензия MIT. Пользуйтесь на здоровье

    Elvenfighter, 20 Августа 2020

    Комментарии (49)
  9. C++ / Говнокод #26882

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    #include <string>
    
    std::string greet(){
      return "hello world!";
    }

    пользуйтесь пока я добрый

    achtonepon, 19 Августа 2020

    Комментарии (0)
  10. C++ / Говнокод #26875

    0

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "is_same_v: " << std::is_same_v<int, const int> << endl;
    }

    https://en.cppreference.com/w/cpp/types/is_same

    > If T and U name the same type (taking into account const/volatile qualifications), provides the member constant value equal to true. Otherwise value is false.

    OlegUP, 17 Августа 2020

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