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

    +17

    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
    #include <boost/spirit/include/phoenix_fusion.hpp>
    #include <boost/fusion/include/adapt_struct.hpp>
    
    #include <iostream>
    
    
    #define GEN_MACRO_0(X, Y)                             \
        ((X, Y)) GEN_MACRO_1
    #define GEN_MACRO_1(X, Y)                             \
        ((X, Y)) GEN_MACRO_0
    #define GEN_MACRO_0_END
    #define GEN_MACRO_1_END
    
    #define SHOW(name,val)                               \
            std::cout << name << " : " << val ;
    
    #define SHOW_TUPLE(r,_,tupple)                       \
           SHOW( "1st",BOOST_PP_TUPLE_ELEM(2,0,tupple))  \
           SHOW("\t2nd",BOOST_PP_TUPLE_ELEM(2,1,tupple)) \
           std::cout << std::endl;
    #define OUTPUT (ADD_PAREN_1 INPUT,_END) 
    
    
    #define SHOW_NODES(seq)                              \
            BOOST_PP_SEQ_FOR_EACH(SHOW_TUPLE,_,          \
                     BOOST_PP_CAT(GEN_MACRO_0 seq,_END) )
    
    int main() {
        SHOW_NODES(
            ("cock","unskill")
            ("rock","hard")
            ("price","over 100500")
            )
    }

    Годнокодец из boost::fusion.
    http://coliru.stacked-crooked.com/a/c516a67930a9c1a8

    crastinus, 29 Декабря 2013

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

    +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
    17. 17
    #include <iostream>
    
    #define PRINT(val) std::cout << #val \
                                 << " value: " << val \
                                 << " address: " << &val << std::endl; 
    int& constRef(){
        int var = 10;
        PRINT(var)
        return var;
    }
    
    int main() {
       const int a = 'a', b = 'bcde';
       std::cout << a << " " << b << std::endl;
       const int& ref = constRef();  
       PRINT(ref)  
    }

    Малоизвестный синтаксис плюсов.
    1. Инициализация целочисленного значения из строки (используется в строках-классах времени компиляции).
    2. Константная ссылка на стековый объект (используется для создания DSL, в т.ч. boost::spirit).
    P.S.: Более полный вариант со строками лежит на форуме.

    crastinus, 28 Декабря 2013

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

    +23

    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
    //VS 2010 C++
    #include <Windows.h>
    #include <time.h>
    #include <queue>
    #pragma comment(lib, "winmm.lib")
    char shapeForms[7][4][5][6] = {{{{0,0,0,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0}},{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,1},{0,0,0,0,0}},{{0,0,0,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0}},{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,1,1,1,1},{0,0,0,0,0}}},{{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,1,1,0},{0,0,1,1,0}},{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,1,1,0},{0,0,1,1,0}},{{0,0,0,0,0},{0,0,0,0,0},{0,0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0,0,0,0},{0,0,0,0,0},{0,0,1,1,1},{0,0,0,0,1}},{{0,0,0,0,0},{0,0,0,0,0},{0,0,1,1,0},{0,0,1,0,0},{0,0,1,0,0}},{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,1,0,0,0},{0,1,1,1,0}},{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,1,0},{0,0,0,1,0},{0,0,1,1,0}}}}, field[32][28] = {0}, shape[5], shape_r[5], modifiers[4][3] = {{0,-1,0},{0,1,0},{1,0,0},{0,0,64}}, keys[4] = {'A', 'D', 'W', 'S'}, keyPressed = -1, lk = 0; //shape - 1: shape state; 2: shape type; 3: y; 4: x;
    std::queue<std::pair<COORD, std::pair<char, char>>> paste;
    COORD t;
    unsigned int Score(0);
    void draw(COORD cd, char c) {if((cd.Y = cd.Y - 4)||true) if(cd.Y >= 0) {SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cd);
    printf("%c", c);}}
    bool next_frame(bool mode, bool isShapeFell) {for(char y(0); y != 5; ++y) {for(char x(0); x != 6; ++x) {t.X = shape[4]+x; t.Y = shape[3]+y; 
    			if(shapeForms[shape[2]][shape[1]][y][x] == 1) {if(field[t.Y][t.X] == 0) paste.push(std::make_pair(t, std::make_pair(-37, 1)));
    				if(field[t.Y][t.X] == 2 || field[t.Y][t.X] == 3) isShapeFell = true;}else if(field[t.Y][t.X] == 1) paste.push(std::make_pair(t, std::make_pair(32, 0)));}}
    	if(isShapeFell) {while(paste.size()) paste.pop();
    		if(mode) for(char i(0); i != 31; ++i) for(char j(0); j != 25; ++j) if(field[i][j] == 1) field[i][j] = 3;
    		return false;}else {for(paste; paste.size() != 0; paste.pop()) {draw(paste.front().first, paste.front().second.first);
    			field[paste.front().first.Y][paste.front().first.X] = paste.front().second.second;}}
    	return true;}
    bool clearFullLines() {for(char isLineExist(1); isLineExist == 1;) {isLineExist = 0;
    		for(char j(0), up(0), down(0), cnt(0); j != 31; (++j) && (cnt = 0)) {for(char i(3); i != 25; ++i) if(field[j][i] == 3) if(j < 5) return false; else cnt++;
    			if(cnt == 22) {if(isLineExist == 0 && (isLineExist = 1) && (down = j)) up = j;
    			else down = j;}else if(isLineExist == 1 && (Score = Score + down-up+1)) {for(char q = down, tmp(0); q >= char(4); (--q)) for(char p(3); p != 25; (++p) && (tmp = 0)) {for(t.X = p, t.Y = q; tmp == 0; tmp = 1);
    					field[q][p] = 0; draw(t, 32);
    					if(field[up+q-down-1][p] == 3) draw(t, -37);
    					t.Y = up+q-down-1;
    					field[q][p] = field[up+q-down-1][p];
    					field[up+q-down-1][p] = 0;}
    				goto next_search;}}
    	next_search:;}
    	return true;}
    int main() {timeBeginPeriod(2);
    	system("mode con cols=42 lines=28");
    	srand(clock()+time(NULL));
    	memset(field, 2, sizeof(field));
    	for(char i(0); i != 28; ++i) for(char j(0), q(0); j != 32; ++j && (q = 0)) for(t.X = i, t.Y = j; q == 0; q = 1) draw(t, -80);
    	for(char i(3); i != 25; ++i) for(char j(0), q(0); j != 30; ++j && (q = 0)) for(t.X = i, t.Y = j; q == 0; (q = 1) && (field[j][i] = 0)) draw(t, 32);
    	for(int tm = 100;true;) {next_turn: if(!clearFullLines() && printf("You fail!")) {system("pause");
    			return 0;}
    		for(t.X = 30, t.Y = 5; false;);  
    		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), t);
    		printf("Score: %i ", Score);
    		shape[1] = rand()*4/(RAND_MAX+1); shape[2] = rand()*7/(RAND_MAX+1); shape[4] = rand()*20/(RAND_MAX+1)+3;
    		for(shape[3] = 0; true; shape[3]++) {for(char h(0); h != 5; ++h) {Sleep(tm); tm = 70;
    				for(char i(0); i != 4 && keyPressed == -1; ++i) if(GetKeyState(keys[i]) < 0) keyPressed = i;
    				if(keyPressed != -1 && !(lk == keyPressed && lk == 2)) {for(char j(0); j != 5; ++j) shape_r[j] = shape[j];
    					shape[4] += modifiers[keyPressed][1]; shape[1] = (shape[1] + modifiers[keyPressed][0])%4;
    					tm -= modifiers[keyPressed][2];
    					lk = keyPressed;
    					keyPressed = -1; 
    					if(!next_frame(false, false)) {for(char j(0); j != 5; ++j) shape[j] = shape_r[j];
    						if(!next_frame(true, false)) goto next_turn;}}else {if(!next_frame(true, false)) goto next_turn;
    					if(keyPressed == -1) lk = -1;
    					if(lk != 2) lk = 0;else keyPressed = -1;}}}}}

    Была задача за минимальное кол-во строк реализовать простой тетрис. Особого внимания заслуживает 6 строчка кода. Всё очень плохо?
    Управление WSAD.

    o0oflashero0o, 26 Декабря 2013

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

    +19

    1. 1
    2. 2
    3. 3
    boost::unordered::unordered_set<const WindowName> _windowNameSet;
    //...
    return (std::find(_windowNameSet.begin(),_windowNameSet.end(), Name) != _windowNameSet.end());

    laMer007, 25 Декабря 2013

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

    +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
    /** фрагмент 0 */ 
    buff[symbols]=0;
    printf ("%s\n",buff);	 
    
    if (strcmp (buff,"Yes!") != 0)
        exit (1);
    
    strcpy (buff,"");	  
    sprintf (buff,"%s","File_size?");
    printf ("%s\n",buff);
    
    /** фрагмент 1 */ 
    Par2 = 0;
    		  if (number_packet < 10)
    			  Par2 = 1;
    		  else
    			  if (number_packet < 100)
    			    Par2 = 2;
    			  else
    				  if (number_packet < 1000)
    			        Par2 = 3;
    				  else
    					  if (number_packet < 10000)
    			             Par2 = 4;
    					  else
    						  if (number_packet < 100000)
    			                 Par2 = 5;
    						  else
    							  if (number_packet < 1000000)
    			                     Par2 = 6;
    							  else
    								  if (number_packet < 10000000)
    			                        Par2 = 7;
    		  Par2++;

    2 фрагмента лабораторной работы

    dannikonov, 24 Декабря 2013

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

    +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
    static std::vector<std::string> listSubstring(std::string stringData, std::string separator)
    {
    	std::vector<std::string> result;
    	std::size_t found = stringData.find(separator);
    	if (found==std::string::npos)
    	{
    		result.push_back(stringData);
    		return result;
    	}
    	result.push_back(stringData.substr(0,found));
    	std::vector<std::string> ko=listSubstring(found+separator.length(),separator);
    	for (std::vector<std::string>::iterator i = ko.begin(); i != ko.end(); ++i)
    		{result.push_back(*i);}
    	return result;
    
    }

    laMer007, 24 Декабря 2013

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

    +11

    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
    /// Represents the model of a Writer document.
    class SW_DLLPUBLIC SwDoc :
        public IInterface,
        public IDocumentSettingAccess,
        public IDocumentDeviceAccess,
        public IDocumentRedlineAccess,
        public IDocumentLinksAdministration,
        public IDocumentFieldsAccess,
        public IDocumentContentOperations,
        public IDocumentStylePoolAccess,
        public IDocumentLineNumberAccess,
        public IDocumentStatistics,
        public IDocumentState,
        public IDocumentDrawModelAccess,
        public IDocumentLayoutAccess,
        public IDocumentTimerAccess,
        public IDocumentChartDataProviderAccess,
        public IDocumentListItems,
        public IDocumentOutlineNodes,
        public IDocumentListsAccess,
        public IDocumentExternalData
    {
    ...

    Вот это монстрище!

    http://opengrok.libreoffice.org/xref/core/sw/inc/doc.hxx

    Sauron, 23 Декабря 2013

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

    +6

    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
    #include <iostream>
    #include <conio.h>
    #include <clocale>
    using namespace std;
    int files; int disc_D(); int disc_E(); int disc_C();
    int resursefunction()
    {char s;
      cout << "Выберите диск" << endl;
      cout <<"1-D:\\" << endl;
      cout <<"2-C:\\" << endl;
      cout <<"3-E:\\" << endl;
      cout <<"0 - рабочий стол" << endl;
      cin >> s;
      if(s == '1') return disc_D();
      if(s == '2') return disc_C();
      if(s == '3') return disc_E();
      if(s == '0') return main();
      _getch();}
    int main()
    { char vibor;
      setlocale( LC_ALL, "Russian" );
      cout << "\t\t\t\tРабочий стол" << endl;
      cout << "\n\n";
      cout << "1-Управление ресурсами" << endl;
      cout << "2-Программы" << endl;
      cout << "3-Игры" << endl;
      cout << "4-Выход из системы" << endl;
      cin >> vibor;
      if(vibor == '1') resursefunction();
      _getch();
      return main();}
    int disc_D()
    { int res, a;
      char vv;
      cout << "D:\\" << endl;
      cout << "На диске" << endl;
      cout << "5 - папок" << endl;
      cout << files << " - файлов" << endl;
      cout << "1 - создать файл" << endl;
      cout << "2 - удалить файл" << endl;
      cout << "0 - назад" << endl;
      cin >> vv;
      if(vv == '1')
      {cout << "Введите сколько создать файлов"; cin >> a; res = files + a;
       res = files + a;}
      if(vv == '2')
      {cout << "Введите сколько удалить файлов"; cin >> a; res = files - a;
       res = files - a;}
      if(vv == '0') return resursefunction();
      files = res;
      return disc_D();}
    int disc_E()
    {
      int res, a;
      char vv;
      cout << "E:\\" << endl;
      cout << "На диске" << endl;
      cout << "7 - папок" << endl;
      cout << files << " - файлов" << endl;
      cout << "1 - создать файл" << endl;
      cout << "2 - удалить файл" << endl;
      cout << "0 - назад" << endl;
      cin >> vv;
      if(vv = '1')
      {cout << "Введите сколько создать файлов"; cin >> a; res = files + a;
       res = files + a;}
      if(vv == '2')
      {cout << "Введите сколько удалить файлов"; cin >> a; res = files - a;
       res = files - a;}
      if(vv == '0') return resursefunction();
      files = res;
      return disc_E();}
    int disc_C()
    {int res, a;
      char vv;
      cout << "C:\\" << endl;
      cout << "На диске" << endl;
      cout << "5 - папок" << endl;
      cout << files << " - файлов" << endl;
      cout << "1 - создать файл" << endl;
      cout << "2 - удалить файл" << endl;
      cout << "0 - назад" << endl;
      cin >> vv;
      if(vv = '1')
      {cout << "Введите сколько создать файлов"; cin >> a; res = files + a;
       res = files + a;}
      if(vv == '2')
      { cout << "Введите сколько удалить файлов"; cin >> a; res = files - a;
       res = files - a; }
      if(vv == '0') return resursefunction();
      files = res;
      return disc_C();}

    Еще нашел на форумах. К сожалению форматирование пришлось убить, иначе оно не помещалось целиком

    pelmenka, 23 Декабря 2013

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

    +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
    if (axis[0] == vector[0] && axis[1] == vector[1] && axis[2] == vector[2])
       {
          this->anglex = 0.0f;
       }
       else if (vector[1] >= 0.0f)
       {
          this->anglex = 360.0f*(GLfloat)acos(VectorMathf::dotProductf(axis, vector))/(2.0f*PIf);
       }
       else
       {
          this->anglex = -360.0f*(GLfloat)acos(VectorMathf::dotProductf(axis, vector))/(2.0f*PIf);
       }

    Часть либы ES3D (http://sourceforge.net/projects/es3d/).
    Кто не понял в чем прикол - все переменные имеют тип float.

    Pythoner, 23 Декабря 2013

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

    +64

    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
    int message2 = -1;
          for (int i = 0; i < 5; i++)
          {
              QFile f;
              f.setFileName(QString("/var/log/messages.") + QString::number(i));
              if (f.exists())
              {
                  message2 = i;
                  break;
              }
          }
    
          if (message2 != -1 )
            QFile::copy(QString("/var/log/messages.")+QString::number(message2), QString(NMP_DATA_DIR) + QString("messages.") +  QString::number(message2) +"-"+ _section);

    Пытается скопировать все файлы /var/log/messages, messages.0, ...

    leotrubach, 19 Декабря 2013

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