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

    +62

    1. 1
    2. 2
    3. 3
    4. 4
    if( state != !val ) 
    {
       state = !val;
    }

    Переключение. Обе переменные булевские.

    absolut, 20 Февраля 2015

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

    +51

    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
    void main() {
    	system("color 04");
    
    	setlocale(LC_ALL, "rus");
    
    
    
    	if (start == false)  {
    		logos();
    		loading();
    		start = true;
    		system("cls");
    	}
    
    
    
    	if (get_hero_s == false)  {
    		get_hero();
    		get_hero_s = true;
    	}
    
    
    
    
    	menu();
    	map();
    	if (go_went_gone == 1) {
    		system("cls");
    		cout << "\nВы напали на оборотня в тёмном лесу\n";
    		loading();
    		Sleep(1400);
    		fight("werewolf");
    
    
    		go_went_gone = 0;
    
    		main();
    	}
    	else if (go_went_gone == 3) {
    		system("cls");
    		cout << "\nВы напали на лучника в мрачном поле\n";
    		loading();
    		Sleep(1400);
    		fight("archer");
    
    
    		go_went_gone = 0;
    
    		main();
    	}
    
    
    	if (go_went_gone == 2) {
    		system("cls");
    		cout << "\nВы напали на гоблина в тёмном лесу\n";
    		loading();
    		Sleep(1400);
    		fight("werewolf");
    
    
    		go_went_gone = 0;
    
    		main();
    	}
    	else if (go_went_gone == 4) {
    		system("cls");
    		cout << "\nВы напали на лучника на костяного лучника в подземелье \n";
    		loading();
    		Sleep(1400);
    		fight("archer");
    
    
    		go_went_gone = 0;
    
    		main();
    	}

    Рекурсия мейна, и это курсовая работа!!

    BoMo, 20 Февраля 2015

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

    +55

    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
    bool validateIp(std::string& ip) {
        if( ip.length() == 0 ) {
            return false;
        }
        
        if( ip[0] == '.' ) {
            return false;
        }
        
        // Проверка на наличие 3 точек
        int cp = 0;
        for( int i = 0; i < ip.length(); i++ ) {
            if( ip[i] == '.' ) {
                cp++;
            }
        }
        if( cp != 3 ) {
            std::cout << "проверка на 3 точки" << std::endl;
            return false;
        }
        //=====================
        
        
        // Проверка на 2 точки подряд
        for( int i = 0; i < ip.length()-1; i++ ) {
            if( ip[i] == '.' && ip[i+1] == '.' ) {
                std::cout << "проверка на 2 точки подряд" << std::endl;
                return false;
            }
        }
        //===========================
        
        
        //Проверка на больше 3 цифр подряд
        int i = 0;
        int j = 0;
        for( i = 0; i < ip.length(); i++ ) {
            for( j = i; j < i+4 && j < ip.length(); j++ ) {
                if( j == i+3 && ip[j] != '.' ) {
                    std::cout << "проверка на 4 цифры подряд" << std::endl;
                    return false;
                }
                if( ip[j] == '.' ) {
                    i = j;
                    break;
                }
            }
        }
        
        return true;
        //============================
    }

    Валидация IP-адреса

    lukaville, 17 Февраля 2015

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

    +51

    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
    while (w.pollEvent(event)) {
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Period)) {
            ip += ".";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num0)) {
            ip += "0";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num1)) {
            ip += "1";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num2)) {
            ip += "2";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num3)) {
            ip += "3";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num4)) {
            ip += "4";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num5)) {
            ip += "5";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num6)) {
            ip += "6";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num7)) {
            ip += "7";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num8)) {
            ip += "8";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Num9)) {
            ip += "9";
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::BackSpace) && ip.length() > 0) {
            ip.erase(ip.end() - 1, ip.end());
        }
        if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Return)) {
            if (validateIp(ip)) {
                Text c("Connection...", f);
                c.setColor(Color::Black);
                c.setPosition(100, 20);
                w.draw(c);
                w.display();
                return ip;
            } else {
                ip.erase(ip.begin(), ip.end());
                wrongAnswer = true;
            }
        }
        if (event.type == Event::Closed) {
            w.close();
            return 0;
        }
    }

    Ввод IP-адреса в интерфейсе игры

    lukaville, 17 Февраля 2015

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

    +49

    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 <iostream>
     
    int get_number() {
    	return 5;
    }
     
    int magic_number(int foo()) {
    	return foo();
    }
     
    int main(void)
    {
    	std::cout << magic_number(get_number) << std::endl;
    }

    http://ideone.com/TbV0jD

    LispGovno, 17 Февраля 2015

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

    +51

    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
    //Sets the color(background and foreground)
    void Console::SetColor(){
        #ifdef _WIN32
            SetConsoleTextAttribute(hConsole, FGColor | BGColor);
        #else
            string clr = "\033[";
            clr += BGColor;
            clr += ";";
            clr += FGColor;
            clr += "m";
            cout << clr;
        #endif
    }

    Изменение цвета текста и фона консоли

    govnokod3r, 17 Февраля 2015

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

    +52

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    // round up the blockSize to fit an integer number of pointers...
    m_blockSize = static_cast<QMPoolSize>(sizeof(QFreeBlock));//start with one
    uint_fast16_t nblocks = uf16_1; //# free blocks in a memory block
    while (m_blockSize < static_cast<QMPoolSize>(blockSize)) {
        m_blockSize += static_cast<QMPoolSize>(sizeof(QFreeBlock));
        ++nblocks;
    }

    в догонку к #17616. делим на 4 с округлением, с помощью цикла.

    P.S. касты и цикл само собой разумеется в ж не нужны:
    m_blockSize = (blockSize + sizeof(QFreeBlock)-1) & ~(sizeof(QFreeBlock)-1);
    nblocks = m_blockSize / sizeof(QFreeBlock);

    Dummy00001, 12 Февраля 2015

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

    +53

    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
    {
       ...
       _tswfstring contentID = fileName;
       _tswfstring::size_type index = fileName.find_last_of ( _T("\\")  );
       if ( index != -1 )
          contentID.erase(0, index + 1);
    
       TCHAR name[10] = {0};
       memcpy(name, contentID.c_str() + contentID.length() - 9, 9 * sizeof(TCHAR));
       if(name[6] == _T('B') || name[6] == _T('b')) //to upper case if .bmp
       {
          name[6] = _T('B');
          name[7] = _T('M');
          name[8] = _T('P');
       }
    }

    blackhearted, 11 Февраля 2015

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

    +55

    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
    uint8_t const Q_ROM QF_div8Lkup[65] = {
        static_cast<uint8_t>(0), // unused location
        static_cast<uint8_t>(0), static_cast<uint8_t>(0), static_cast<uint8_t>(0),
        static_cast<uint8_t>(0), static_cast<uint8_t>(0), static_cast<uint8_t>(0),
        static_cast<uint8_t>(0), static_cast<uint8_t>(0),
        static_cast<uint8_t>(1), static_cast<uint8_t>(1), static_cast<uint8_t>(1),
        static_cast<uint8_t>(1), static_cast<uint8_t>(1), static_cast<uint8_t>(1),
        static_cast<uint8_t>(1), static_cast<uint8_t>(1),
        static_cast<uint8_t>(2), static_cast<uint8_t>(2), static_cast<uint8_t>(2),
        static_cast<uint8_t>(2), static_cast<uint8_t>(2), static_cast<uint8_t>(2),
        static_cast<uint8_t>(2), static_cast<uint8_t>(2),
        static_cast<uint8_t>(3), static_cast<uint8_t>(3), static_cast<uint8_t>(3),
        static_cast<uint8_t>(3), static_cast<uint8_t>(3), static_cast<uint8_t>(3),
        static_cast<uint8_t>(3), static_cast<uint8_t>(3),
        static_cast<uint8_t>(4), static_cast<uint8_t>(4), static_cast<uint8_t>(4),
        static_cast<uint8_t>(4), static_cast<uint8_t>(4), static_cast<uint8_t>(4),
        static_cast<uint8_t>(4), static_cast<uint8_t>(4),
        static_cast<uint8_t>(5), static_cast<uint8_t>(5), static_cast<uint8_t>(5),
        static_cast<uint8_t>(5), static_cast<uint8_t>(5), static_cast<uint8_t>(5),
        static_cast<uint8_t>(5), static_cast<uint8_t>(5),
        static_cast<uint8_t>(6), static_cast<uint8_t>(6), static_cast<uint8_t>(6),
        static_cast<uint8_t>(6), static_cast<uint8_t>(6), static_cast<uint8_t>(6),
        static_cast<uint8_t>(6), static_cast<uint8_t>(6),
        static_cast<uint8_t>(7), static_cast<uint8_t>(7), static_cast<uint8_t>(7),
        static_cast<uint8_t>(7), static_cast<uint8_t>(7), static_cast<uint8_t>(7),
        static_cast<uint8_t>(7), static_cast<uint8_t>(7)
    };
    
    // ....
    
        //! the function evaluates to TRUE if the priority set has the element n.
        bool hasElement(uint_fast8_t const n) const {
            uint_fast8_t const m =
                static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_div8Lkup[n]));
            return ((m_bits[m]
                      & static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_pwr2Lkup[n])))
                   != static_cast<uint_fast8_t>(0));
        }
    
        //! insert element \a n into the set, n = 1..64
        void insert(uint_fast8_t const n) {
            uint_fast8_t m =
                static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_div8Lkup[n]));
            m_bits[m] |= static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_pwr2Lkup[n]));
            m_bytes   |=
                static_cast<uint_fast8_t>(Q_ROM_BYTE(QF_pwr2Lkup[m
                                              + static_cast<uint_fast8_t>(1)]));
        }

    делим на 8 в индустриальном С++. это такой специальный вариант крестов где пользователям сначала лоботомию делают.

    из реализации bitset'а. insert() приведен в качестве примера.

    Dummy00001, 11 Февраля 2015

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

    +53

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    static uint8_t const FREE = static_cast<uint8_t>(0);
    static uint8_t const USED = static_cast<uint8_t>(1);
    
    static char_t const * const THINKING = &"thinking"[0];
    static char_t const * const HUNGRY   = &"hungry  "[0];
    static char_t const * const EATING   = &"eating  "[0];

    Из демы QP/C++ библиотеки. Вот в таком духе очень много кода.

    Индустриальщики, после перехода на С++, похоже очень сильно страдают по отсутствию pre-ANSI C какашек, и изобретают новые.

    Dummy00001, 10 Февраля 2015

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