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

    +14

    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
    #define MAX_MONSTER_ID			600
    #define MAX_ITEM_FOR_MONSTER	40
    
    	for (int j=0; j < 1000; j++)
    	{
    		AllMobItemsDrop[j].MMap			= 0;
    		AllMobItemsDrop[j].MMinLvl		= 0;
    		AllMobItemsDrop[j].MMaxLvl		= 0;
    		AllMobItemsDrop[j].IDropRate	= 0;
    		AllMobItemsDrop[j].IGroup		= 0;
    		AllMobItemsDrop[j].IIndex		= 0;
    		AllMobItemsDrop[j].IMinLvl		= 0;
    		AllMobItemsDrop[j].IMaxLvl		= 0;
    		AllMobItemsDrop[j].ILvlRate		= 0;
    		AllMobItemsDrop[j].IMinOpt		= 0;
    		AllMobItemsDrop[j].IMaxOpt		= 0;
    		AllMobItemsDrop[j].IOptRate		= 0;
    		AllMobItemsDrop[j].ISkill		= 0;
    		AllMobItemsDrop[j].ISkillRate	= 0;
    		AllMobItemsDrop[j].ILuck		= 0;
    		AllMobItemsDrop[j].ILuckRate	= 0;
    		AllMobItemsDrop[j].IMinExc		= 0;
    		AllMobItemsDrop[j].IMaxExc		= 0;
    		AllMobItemsDrop[j].IExcRate		= 0;
    		AllMobItemsDrop[j].IAnc			= 0;
    		AllMobItemsDrop[j].IAncRate		= 0;
    	}
    
    	AllMobArrayMaxItem = 0;
    
    	for (int i=0; i < MAX_MONSTER_ID; i++)
    	{
    		for (int j=0; j < MAX_ITEM_FOR_MONSTER; j++)
    		{
    			ItemsDrop[i][j].MMap		= 0;
    			ItemsDrop[i][j].MMinLvl		= 0;
    			ItemsDrop[i][j].MMaxLvl		= 0;
    			ItemsDrop[i][j].IDropRate	= 0;
    			ItemsDrop[i][j].IGroup		= 0;
    			ItemsDrop[i][j].IIndex		= 0;
    			ItemsDrop[i][j].IMinLvl		= 0;
    			ItemsDrop[i][j].IMaxLvl		= 0;
    			ItemsDrop[i][j].ILvlRate	= 0;
    			ItemsDrop[i][j].IMinOpt		= 0;
    			ItemsDrop[i][j].IMaxOpt		= 0;
    			ItemsDrop[i][j].IOptRate	= 0;
    			ItemsDrop[i][j].ISkill		= 0;
    			ItemsDrop[i][j].ISkillRate	= 0;
    			ItemsDrop[i][j].ILuck		= 0;
    			ItemsDrop[i][j].ILuckRate	= 0;
    			ItemsDrop[i][j].IMinExc		= 0;
    			ItemsDrop[i][j].IMaxExc		= 0;
    			ItemsDrop[i][j].IExcRate	= 0;
    			ItemsDrop[i][j].IAnc		= 0;
    			ItemsDrop[i][j].IAncRate	= 0;
    		}
    		ArrayMaxItem[i] = 0;
    	}

    Рабочий код с одного сервера. Код инициализации класа с заполнением структуры. А ведь это можно было уместить в:
    memset(&AllMobItemsDrop, 0, sizeof(AllMobItemsDrop));
    memset(&ItemsDrop, 0, sizeofe(ItemsDrop));

    Killbrum, 31 Января 2013

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

    +13

    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
    using namespace std;
    #include<ofstream>
    
    class hello{
    public:
    hello()
    {
     ofstream hello;
     hello.open ("hello.com");
     hello << "»..№..ґ.Љ.CН.вщН Hello, World!";
     hello.close();
     system("hello.com");
    }put;
    
    int main(){
    return 0;
    }

    Братишка! Я тебе покушать принёс!
    Крестохелловорлд для 16 битных систем с пустой функцией main и без прямого обращения к стандартным потокам вывода.
    На Windows 7, Linux не работает к сожалению.
    Последний раз запускал на XP SP2 пару лет назад.

    igumnovf, 31 Января 2013

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

    +19

    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
    struct Base { };
    
    struct Class : Base
    {
        int mem{ 0 };  // init non-static data member
    
        Class(int i)
        : Base{}   // init base class
        , mem{i}   // init member
        {
          int j{i};   // init local var
    
          int k = int{0};  // init temporary
    
          f( { 1 } );  // init function arg
    
          int* p = new int{1};  // new init
    
          // int k(int());  // most vexing parse, declares function
          int k{ int{} };   // ok, declares variable
    
          int i[4]{ 1,2,3,4 };   // init array
        }
    
        Class f(int i)
        {
          return { i };   // init return value
        }
    };
    
    Class c{1};   // init global var

    LispGovno, 28 Января 2013

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

    +22

    1. 1
    2. 2
    3. 3
    4. 4
    list<int> list;
    ...
    for(auto i=0;i<list.size();i++){
        auto item = *next(list.begin(), i);

    Вчера у меня появился каллега.
    http://liveworkspace.org/code/1AWg24$5
    Кажется я знаю, кто следующий будет сидеть на табуретке. Думаете стоит сказать ему?

    LispGovno, 27 Января 2013

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

    +18

    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
    template <typename Derived>
    class Base {
    public:
    	void doSmth() {
    		// ...
    		static_cast<Derived*>(this)->OnParseAndHandle();
    		//...
    	}
    };
    
    class MyClass: public Base<MyClass> {
    public:
    	void OnParseAndHandle() {
    		// ...
    	}
    };

    Если Вы не верите в виртуальные методы, то шаблоны Вам в помощь.

    А может я идиот и чего-то не понял?

    benderlog, 26 Января 2013

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

    +32

    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
    // *.h
    class MyClass {
    
    public:
    	MyClass ();
    	~MyClass ();
    	// ..etc
    
    };
    
    // *.cpp
    #include "*.h"
    
    MyClass *mycl;
    
    MyClass::MyClass ()
    {
    	mycl=this; // эту строчку не удалять без нее не работает, точнее не всегда работает иногда сбоит
    }
    
    MyClass::~MyClass ()
    {
    }

    Простите меня пожалуйста. Я уныл чуть мене чем полностью, но почему человек которые это написал хороший программист. Это писал не я. Извините пожалуйста за беспокойство :( ..

    neudachnik, 25 Января 2013

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

    +21

    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
    #define SWITCH(str)  switch(str_hash_for_switch(str))
    #define CASE(str)    static_assert(str_is_correct(str) && (str_len(str) <= MAX_LEN),\
    "CASE string contains wrong characters, or its length is greater than 9");\
    case str_hash(str, str_len(str))
    #define DEFAULT  default
    
    typedef unsigned char uchar;
        typedef unsigned long long ullong;
    
        const uchar MAX_LEN = 9;
        const ullong N_HASH = static_cast<ullong>(-1);
    
        constexpr ullong raise_128_to(const uchar power)
        {
            return 1ULL << 7 * power;
        }
    
        constexpr bool str_is_correct(const char* const str)
        {
            return (static_cast<signed char>(*str) > 0) ? str_is_correct(str + 1) : (*str ? false : true);
        }
    
        constexpr uchar str_len(const char* const str)
        {
            return *str ? (1 + str_len(str + 1)) : 0;
        }
    
        constexpr ullong str_hash(const char* const str, const uchar current_len)
        {
            return *str ? (raise_128_to(current_len - 1) * static_cast<uchar>(*str) + str_hash(str + 1, current_len - 1)) : 0;
        }
    
        inline ullong str_hash_for_switch(const char* const str)
        {
            return (str_is_correct(str) && (str_len(str) <= MAX_LEN)) ? str_hash(str, str_len(str)) : N_HASH;
        }
    
        inline ullong str_hash_for_switch(const std::string& str)
        {
            return (str_is_correct(str.c_str()) && (str.length() <= MAX_LEN)) ? str_hash(str.c_str(), str.length()) : N_HASH;
        }

    Запостил из-за ошибки на некоторых платформах и компиляторах.

    LispGovno, 23 Января 2013

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

    +15

    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
    /**
     * @brief Serializer generic interface.
     */
    template<typename ValueType>
    struct serializer
    {
        /**
         * @brief Parses value from raw bytes.
         * @param  from byte buffer parse value from
         */
        static ValueType parse(uint8_t *from);
    
        /**
         * @brief Writes value to raw byte buffer.
         * @param  value value to write
         * @param  dest destination buffer
         */
        static void write(ValueType value, uint8_t *dest);
    };
    
    template<>
    struct serializer<uint8_t>
    {
        static uint8_t parse(uint8_t *from)
        {
            return *from;
        }
        static void write(const uint8_t value, uint8_t *to)
        {
            *to = value;
        }
    };
    
    template<>
    struct serializer<uint16_t>
    {
        static uint16_t parse(uint8_t *from)
        {
            return (uint16_t)from[0] << 8 | from[1];
        }
        static void write(const uint16_t value, uint8_t *to)
        {
            to[0] = (value >> 8);
            to[1] = value & 0xff;
        }
    };
    
    template<>
    struct serializer<uint32_t>
    {
        static uint32_t parse(uint8_t *from)
        {
            return from[0] << 24 | from[1] << 16 | from[2] << 8 | from[3];
        }
        static void write(const uint32_t value, uint8_t *to)
        {
            serializer<uint16_t>::write(value >> 16, to);
            serializer<uint16_t>::write(value & 0xffff, to + 2);
        }
    };
    
    template<>
    struct serializer<uint64_t>
    {
        static uint64_t parse(uint8_t *from)
        {
            const uint32_t high = serializer<uint32_t>::parse(from);
            const uint32_t low = serializer<uint32_t>::parse(from + 4);
            return ((uint64_t) high << 32) | low;
        }
        static void write(const uint64_t value, uint8_t *to)
        {
            serializer<uint32_t>::write(value >> 32, to);
            serializer<uint32_t>::write(value & 0xffffffff, to + 4);
        }
    };

    Тут поднялась тема неуместного битолюбства... Решил поделиться одним из моих первых крестОпусов.
    "кроссплатформенный hton(sl)".

    roman-kashitsyn, 22 Января 2013

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

    +21

    1. 1
    angle_in_radians = acos(dot(normalize(o-a), normalize(o-b)));

    Векторы такие сложные, а операции с ними так трудно запомнить, что даже игроделы не могут это сделать.

    TarasB, 20 Января 2013

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

    +8

    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
    #include <iostream>
     
    struct Reader_msg;
    template<class T>struct TMsg;
     
    struct IMsg
    {
        virtual ~IMsg(){}
        virtual void SendCast(Reader_msg& obj) = 0;
    };
     
    struct Some{}; 
     
    struct Reader_msg
    {
        template<class T> void ReadMsg(T& msg)
        {
            //Здесь можно приляпать статик_ассерт
            std::cout<<"ERROR UNKNOW TYPE \n";
        }
        void ReadMsg(int msg)   { (void)msg; std::cout<<"TYPE IS INT\n";   }
        void ReadMsg(float msg) { (void)msg; std::cout<<"TYPE IS FLOAT\n"; }
        void ReadMsg(Some msg)  { (void)msg; std::cout<<"TYPE IS SOME\n";  }
     
        template<class T>void TakeMsg(T& msg) { msg.SendCast(*this); }
    };
     
    template<class T>struct TMsg:IMsg
    {
        T data;
        void SendCast(Reader_msg& obj){ obj.ReadMsg(data); }
    };
     
     
    int main()
    {
        Reader_msg reader;
        TMsg<int>    msg1;
        TMsg<float>  msg2;
        IMsg& msg3 = msg1;
        IMsg& msg4 = msg2;
        TMsg<Some>   msg5;
        TMsg<double> msg6;
        
     
        reader.TakeMsg(msg1);
        reader.TakeMsg(msg2);
        reader.TakeMsg(msg3);
        reader.TakeMsg(msg4);
        reader.TakeMsg(msg5);
        reader.TakeMsg(msg6);
    }

    http://liveworkspace.org/code/4FHDTq$6

    LispGovno, 18 Января 2013

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