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

    +7

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    std::string s1;
    std::string s2;
    
    int ret = strcmp(s1.c_str(), s2.c_str());  //А здесь мы сравниваем строки
    if (ret == 0)
    {
        .... 
    }

    pathfinder, 18 Августа 2009

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

    +7

    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
    if(isMoving)
    {
    	std::map<int, unsigned char> dirToFrame = boost::assign::map_list_of(1, 10)(2, 5)(3, 6)(4, 7)(5, 8)(6, 9)(7, 12)(8, 11);
    	if(dir >= 1 && dir <= 10)
    		return dirToFrame[dir];
    
    	throw std::string("Something very wrong1.");
    }
    else //if(isMoving)
    {
    	std::map<int, unsigned char> dirToFrame = boost::assign::map_list_of(1, 13)(2, 0)(3, 1)(4, 2)(5, 3)(6, 4)(7, 15)(8, 14);
    	if(dir >= 1 && dir <= 10)
    		return dirToFrame[dir];
    
    	throw std::string("Something very wrong1.");
    }

    Кусок кода из open source игры. Выбирает кадр спрайта, в зависимости от направления героя. В ревизии, предшествующей этой, использовался switch.

    just_geek, 18 Августа 2009

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

    +14.1

    1. 1
    2. 2
    3. 3
    4. 4
    bool IsTranslucent() const
    {
    	return !!Translucent;
    }

    Зачем двойное отрицание?
    Наверное потому, что Translucent имеет тип BOOL из windows.h и приведение ее к bool даст нежелательный warning.

    schizoid2, 17 Августа 2009

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

    +6.9

    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
    #ifndef SOCHET_H
    #define SOCHET_H
    
    // Сдвигает самую младшую единицу в сторону младшего разряда
    #define shiftLast1Right(x) ( (x-1)^((x^(x-1)) >> 2) )
    
    // Дописывает 1 после самой младшей единицы
    #define add1AfterLast1(x) ( x | ((x^(x-1))+1) >> 2 )
    
    template<class T>
    class Sochet
    {
    public:
    	T value;
    
    	//////////////////////////////////////////////////////////////////////////
    
    	Sochet():value(0) { }
    	Sochet(int n, int k) {
    		firstTurn(n, k);
    	}
    	~Sochet() {
    		value = 0;
    	}
    
    	//////////////////////////////////////////////////////////////////////////
    
    	// Первая комбинация
    	// В первоначальной ситуации все К единиц располагаются в старших битах
    	void firstTurn(int n, int k) {
    		value = ( ( T(1) << k ) - 1 ) << (n - k);
    	}
    
    	// Нахождение следующей комбинации
    	// Возвращает false в случае последней комбинации
    	bool nextTurn()
    	{
    		// Отлов последней комбинации
    		if (value & (value+1) == 0)
    			return false;
    		
    		// Условие по младшему биту: 1 или 0
    		if (value & 1)
    		{
    			value >>= 1;
    			nextTurn();
    			value = add1AfterLast1(value << 1);
    		} else
    			value = shiftLast1Right(value);
    		
    		return true;
    	}
    }
    
    #endif // SOCHET_H

    Шаблон перебора всех сочетаний/выборок в много разрядных числах.
    Пример перебираемых чисел для N=5, K=3:
    11100
    11010
    11001
    10110
    10101
    10011
    01110
    01101
    01011
    00111

    // Код выглядит сочно(особенно дефайны), зато всё работает максимально быстро.
    // Статья про этот алгоритм: http://k06a.blogspot.com/2009/04/blog-post_08.html

    k06a, 15 Августа 2009

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

    +12.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
    DateTime get_date(const char * _sz_date, bool _b_trunc_year)
    {
    	DateTime _obj_dt;
    
    	// "01/02/2003" - format of the data
    
    	string _str_date = _sz_date;
    
     	//--------------
    
    	string _str_month;
    	string _str_year;
    
    	int _n_day;
    	int _n_month;
    	int _n_year;
    
     	///--------------
    
    	bool _b_1 = false;
    	bool _b_2 = false;
    
    	size_t _n_pos_1 = 0;
    	size_t _n_pos_2 = 0;
    
    	///--------------
    
    	for (size_t _un_num = 0; _un_num < _str_date.size(); _un_num++)
    	{
    		if(_str_date[_un_num] == '/')
    		{
    			///-----------------
    			if(!_b_1)
    			{
    				_b_1 		= true;
    				_n_pos_1 	= _un_num;
    
            			continue;
    			}
    			///-----------------
    			if(_b_1 && !_b_2)
    			{
    				_b_2 		= true;
    				_n_pos_2 	= _un_num;
    			}
    			///-----------------
    		}
    	}
    
     	///--------------
    
    	if(!_b_1 || !_b_2)
    	{
    		return _obj_dt;
    	}
    
     	///--------------
    
    	_str_month = _str_date.substr( _n_pos_1 + 1, ( _n_pos_2 - _n_pos_1 ) - 1 );
    
     	_str_year  = _str_date.substr( _n_pos_2 + 1, ( _str_date.size() - 1 ) -  _n_pos_2 );
    
     	if(_b_trunc_year)
    	{
        		if(_str_year.size() >=4)
       			{
          			_str_year = _str_year.substr(2,2);
        		}
     	}
    
    	sscanf(_str_month.c_str(), "%d", &_n_month);
    	sscanf(_str_year .c_str(), "%d", &_n_year);
    
    	_n_day = 1;
    
    	_obj_dt = DateTime(_n_year,_n_month,_n_day);
    
    	return _obj_dt;
    }

    Парсим дату формата mm/dd/yyyy. Не все так просто в этой жизни.

    lamer, 14 Августа 2009

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

    +5

    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
    //...
      string m_str_datum, m_str_zeit;
      m_str_datum.assign(m_str_data , 22 ,  6 ); 
      m_str_zeit.assign(m_str_data , 28 ,  6 );
      m_str_datum = remove_spaces(m_str_datum) ;
      m_str_zeit  = remove_spaces(m_str_zeit);  
    
      string _str_year;
      string _str_month;
      string _str_day;
    
      int _n_year;
      int _n_month;
      int _n_day;
    
      _n_year  = 0;
      _n_month = 0;
      _n_day   = 0;
    
      _str_day.assign(m_str_datum,0,2);
      _str_month.assign(m_str_datum,2,2);
      _str_year.assign(m_str_datum,4,2);
    
      sscanf( _str_day.c_str()  , "%d", &_n_day   );
      sscanf( _str_month.c_str(), "%d", &_n_month );
      sscanf( _str_year.c_str() , "%d", &_n_year  );
      if (_n_year > 0 && _n_year < 50)
    	_n_year += 2000;
      else if (_n_year >= 50 && _n_year < 100)
    	_n_year += 1900;
    
      //...
      string _str_hours;
      string _str_minutes;
      string _str_seconds;
     
      int _n_hours;
      int _n_minutes;
      int _n_seconds;
    
      _n_hours   = 0;
      _n_minutes = 0;
      _n_seconds = 0;
    
      _str_hours.assign(m_str_zeit,0,2);
      _str_minutes.assign(m_str_zeit,2,2);
      _str_seconds.assign(m_str_zeit,4,2);
    
      sscanf( _str_hours.c_str()   , "%d", &_n_hours    );
      sscanf( _str_minutes.c_str() , "%d", &_n_minutes  );
      sscanf( _str_seconds.c_str() , "%d", &_n_seconds  );
    
      DateTime _obj_start_time;
    
      try {
    	_obj_start_time = DateTime(_n_year,  _n_month,   _n_day, _n_hours, _n_minutes, _n_seconds);
      }
    //...

    Парсим дату ddmmyyhhnnss не по-детски. Код функции которая обрабатывает несколько сотен тысяч строк.

    lamer, 14 Августа 2009

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

    +20.7

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    .....
    int i;
    .....
    int b=i/2;
    if(i==b*2)//проверка на четность
    {
    .....

    schaos, 14 Августа 2009

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

    +3.3

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    class boolshit{
    public:
    boolshit(bool shit):_shit(shit){}
    operator bool() const{return _shit;}
    private:
    bool _shit;
    };

    :)

    Ror77, 13 Августа 2009

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

    +18.2

    1. 1
    2. 2
    ProfileManager* man = Application::GetProfileManager();
    		_DESIRE(man);	//гм. "Желать мужика". Ужос какой. Это не я написал, если чо. Это оно само

    Вот в коде наткнулся, улыбнуло

    ich, 13 Августа 2009

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

    +17

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    unsigned long f( unsigned long a ) {
     while( (1 << 24) < a )
      a -= (1 << 24);
     return a;
    }

    Найден в дебрях старого проекта. Цель непонятна. Предположение -- обнулить старших 8-мь бит в 32-х битовом числе.

    eao197, 13 Августа 2009

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