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

    +141

    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
    #include <stdio.h>
    #include <dstring.h>
    #include <iostream>
    #include <fstream>
    #include <sysmac.h>
    #include <System.hpp>
    
    class ImpFromTxt
    {
    	AnsiString S1; //переменная для выбранного текста из файла
    	AnsiString buf; //буферная переменная для записи необходимых слов
    
    
    	public:
    
    	AnsiString Import(const char* Aftr, int Chislo=0, ...)
    	{                                               //^число слов после которых необходимо вычленить слова
    //	using namespace std;
    
    			size_t count=0; //Число символов в файле
    			char S; //для подсчета символов
    			const char* word_before = "Точка: "; //слово, после которого необходимо выделить слово
    			/*Просто считаем число символов*/
    			ifstream f(Aftr);
    			while (!f.eof())
    			{
    				f.get(S);
    				count++;
    			}
    			f.close();
    
    			/*Добавляем текст в переменную*/
    
    			char *S2=new char [count]; //Выделяем память по определенному числу символов
    			for (int i=0;i<count;i++) S2[i]=NULL; //Очищаем строку от хлама
    				ifstream f2(Aftr);  //создаем поток для работы с файлом
    				 int i=0; //куроср на текущий символ (как номер буквы в слове)
    
    				 while (!f2.eof()) //Проходим ао файлу
    				 {
    					 f2.get(S2[i]); //Записываем симыол за символом
    					 i++; //передвинули курсор для записи следующего символа
    				 }
             
    				f2.close(); //закончили работу с файлом
    				S1 = S2;
    //				cout<<S2<<endl; //Теперь переменная char[] содержит текст из файла
    
    		  	delete []S2; //освобождаем память
    			/**************************************/
    			switch (Chislo)  //цикл для выбора количества видов слов
    			{                //слова будут собираться в буферной переменной
    							 //для возвращения в основную программу
    
                    case 2:
    						break;
    				case 1:	{   // TX;
    							 while (S1.Pos(word_before))
    							  {
    									char buf1[100]= " ";
    									int j=0;
    									i=(S1.Pos(word_before)) ? (S1.Pos(word_before)+strlen(word_before)) : 0;
    									int M = S1.Pos(word_before); //индекс первой буквы Т в первом нахождении слова Точка
    									int L = S1.Length(); //длинна всего текста
    
    									if (i)
    									while (S1[i] != '\n')
    									{
    										buf1[j]=S1[i];
    										j++;
    										i++;
    									}
    									buf1[strlen(buf1)] = '\r';
    									buf1[strlen(buf1)] = '\n';
    									buf += buf1;
    								   //buf = buf.Insert((S1.SubString(S1.Pos("Точка:")+8,5)),(S1.Pos("Точка:")+8));
    									S1 = S1.SubString(S1.Pos(word_before)+strlen(buf1)-1+strlen(word_before),S1.Length()-(S1.Pos(word_before)+strlen(word_before)+strlen(buf1)-2));
    								   //-1 и -2 в этом присваивании поставлены для компенсации добавленных лишних символов \r \n
    									L = S1.Length();
    							  }
    						}
    						break;
    
    				case 3:
    						break;
    
    				default:
                    ;
    			}
    
    			return buf;
    	}
    };

    Класс парсит текстовый файл вида
    Точка: Инфа 1
    Точка: Инфа 2
    Точка: Инфа ...
    Точка: Инфа n
    доставая инфу, находящуюся после лексемы "Точка: ".

    Нет, код писался не специально, чтобы потешить кого-то на этом сайте. Это из реального рабочего проекта.

    vr8h, 14 Мая 2015

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

    +48

    1. 1
    auto filename = std::string{};

    laMer007, 12 Мая 2015

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

    +142

    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
    typedef enum {
            INIT1=0, INIT2,  INIT3,  INIT4,  INIT5,  INIT6,  INITN,
            BIND1,   BIND2,  BIND3,  BIND4,  BIND5,  BIND6,  BINDN,
            YIELD1,  YIELD2, YIELD3, YIELD4, YIELD5, YIELD6, YIELDN, 
            COMPARE, CHECK, FILTER, CFILTER, PFILTER, CHOOSE, NOOP, CONTINUE,
            GET_ENODE, 
            GET_CGR1, GET_CGR2, GET_CGR3, GET_CGR4, GET_CGR5, GET_CGR6, GET_CGRN,
            IS_CGR
        } opcode;
    ...
    ...
    ...
    
    
    
            switch (m_pc->m_opcode) {
            case INIT1:
                m_app          = m_registers[0];
                if (m_app->get_num_args() != 1)
                    goto backtrack;
                m_registers[1] = m_app->get_arg(0);
                m_pc = m_pc->m_next;
                goto main_loop;
                
            case INIT2:
                m_app          = m_registers[0];
                if (m_app->get_num_args() != 2)
                    goto backtrack;
                m_registers[1] = m_app->get_arg(0);
                m_registers[2] = m_app->get_arg(1);
                m_pc = m_pc->m_next;
                goto main_loop;
                
            case INIT3:
                m_app          = m_registers[0];
                if (m_app->get_num_args() != 3)
                    goto backtrack;
                m_registers[1] = m_app->get_arg(0);
                m_registers[2] = m_app->get_arg(1);
                m_registers[3] = m_app->get_arg(2);
                m_pc = m_pc->m_next;
                goto main_loop;
                
            case INIT4:
                m_app          = m_registers[0];
                if (m_app->get_num_args() != 4)
                    goto backtrack;
                m_registers[1] = m_app->get_arg(0);
                m_registers[2] = m_app->get_arg(1);
                m_registers[3] = m_app->get_arg(2);
                m_registers[4] = m_app->get_arg(3);
                m_pc = m_pc->m_next;
                goto main_loop;
                
            case INIT5:
                m_app          = m_registers[0]; 
                if (m_app->get_num_args() != 5)
                    goto backtrack;
                m_registers[1] = m_app->get_arg(0);
                m_registers[2] = m_app->get_arg(1);
                m_registers[3] = m_app->get_arg(2);
                m_registers[4] = m_app->get_arg(3);
                m_registers[5] = m_app->get_arg(4);
                m_pc = m_pc->m_next;
                goto main_loop;
                
            case INIT6: 
                m_app          = m_registers[0];
                if (m_app->get_num_args() != 6)
                    goto backtrack;
                m_registers[1] = m_app->get_arg(0);
                m_registers[2] = m_app->get_arg(1);
                m_registers[3] = m_app->get_arg(2);
                m_registers[4] = m_app->get_arg(3);
                m_registers[5] = m_app->get_arg(4);
                m_registers[6] = m_app->get_arg(5);
                m_pc = m_pc->m_next;
                goto main_loop;
                
            case INITN:
                m_app      = m_registers[0];
                m_num_args = m_app->get_num_args();
                if (m_num_args != static_cast<const initn *>(m_pc)->m_num_args)
                    goto backtrack;
                for (unsigned i = 0; i < m_num_args; i++)
                    m_registers[i+1] = m_app->get_arg(i);
                m_pc = m_pc->m_next;
                goto main_loop;

    Из изходников STM-солвера Z3
    https://github.com/Z3Prover/z3/blob/master/src/smt/mam.cpp#L2298
    Почему нельзя было оставить только вариант INITN? Цикл отбирает так много ресурсов?

    j123123, 10 Мая 2015

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

    +139

    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
    //Башня хаханойская
    #include <iostream>
    #include<ctime>
    #include<math.h>
    
    using namespace std;
    int count=0;
    void hanoi_towers(int quantity, int from, int to, int buf_peg)   
    {
    int mass[10];                                                       
    if (quantity != 0)
    {
    hanoi_towers(quantity-1, from, buf_peg, to);
     for (int i = 0; i < quantity; i++) {
    mass[i]=1+rand()%quantity;
    cout <<" ["<<i<<"]="<<mass[i]<<endl;}
    cout <<"S kol'ca # "<< from << " na kol'co # " << to << endl;
    hanoi_towers(quantity-1, buf_peg, to, from);
     count++;
    }
    }
    
    int main()
    {
    int mas[10];
    		int start_peg = 1, destination_peg=3, buffer_peg=2, plate_quantity,p;
    
    		cout << "Koli4estvo discov:" << endl;
    		cin  >> plate_quantity;
    	   for (int i = 0; i < plate_quantity; i++) {
    	   mas[i]=i;
    		cout <<"1 massiv=["<<i<<"]="<<mas[i]<<endl;}
    		hanoi_towers(plate_quantity, start_peg, destination_peg, buffer_peg);
    			 cout<<"Kol. iteracui  = "<<count<<endl;
    			  p=pow(2.0,plate_quantity)-1 ;
    			 cout<<"Po formule ="<<p<<endl;
    for (int i = 0; i < plate_quantity; i++) {
    cout <<"3 massiv=["<<i<<"]="<<mas[i]<<endl;
    }
    getchar();
    getchar();
    }

    //Башня хаханойская

    ebywku, 07 Мая 2015

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

    +143

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    void MyWindow::OkButtonClicked()
    {
        if((!cb1->isChecked()) && (!cb2->isChecked()))
            emit Simple(line->text());
        if((cb1->isChecked()) && (!cb2->isChecked()))
            emit Register(line->text());
        if((!cb1->isChecked()) && (cb2->isChecked()))
            emit Invers(line->text());
        if((cb1->isChecked()) && (cb2->isChecked()))
            emit RegVers(line->text());
    }

    dia, 05 Мая 2015

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

    +148

    1. 1
    2. 2
    //G++ now allows typename in a template template parameter.
        template<template<typename> typename X> struct D; // xzibit.jpeg

    Пятый gcc вышел. Больше крестоблядства за те же деньги.
    https://gcc.gnu.org/gcc-5/changes.html

    3.14159265, 02 Мая 2015

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

    +146

    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
    #ifndef FILTER_H
    #define FILTER_H
    
    #include <cassert>
    #include <vector>
    #include <algorithm>
    
    template<typename T>
    class filter_container;
    namespace detail {
    	template<typename Iter, typename Pred>
    	class filter {
    	public:
    		filter(Iter begin, Iter end, Pred pred) :_begin(begin), _end(end), _pred(pred) { }
    		filter(const filter & f) : _begin(f._begin), _end(f._end), _pred(f._pred) { }
    		filter & operator = (const filter & f) {
    			if(this != &f) {
    				_begin = f._begin;
    				_end = f._end;
    				_pred = f._pred;
    			}
    			return *this;
    		}
    		~filter() { }
    
    		class const_iterator {
    		public:
    			typedef typename Iter::reference reference;
    			typedef typename Iter::pointer pointer;
    			const_iterator(Iter iter, const filter & cont) :_iter(iter), _cont(&cont) { advance_until_pred(); }
    			const_iterator(const const_iterator & i) :_iter(i._iter), _cont(i._cont) { }
    			const_iterator & operator = (const const_iterator & i) {
    				if(this != &i) {
    					_iter = i._iter;
    					_cont = i._cont
    				}
    				return *this;
    			}
    			~const_iterator() { }
    			const_iterator & operator++() { advance_until_pred(); return *this; }
    			const_iterator operator++(int) {
    				const_iterator ret = *this;
    				advance_until_pred();
    				return ret;
    			}
    			const reference operator*() const { return *_iter; }
    			const pointer operator->() const { return &*_iter; }
    			bool operator == (const const_iterator & i) const {
    				assert(_cont == i._cont);
    				return _iter == i._iter;
    			}
    			bool operator != (const const_iterator & i) const { return !(*this == i); }
    		protected:
    			Iter _iter;
    			const filter * _cont;
    		private:
    			void advance_until_pred() {
    				if(_iter != _cont->_end) {
    					std::advance(_iter, 1);
    				}
    				_iter = std::find_if(_iter, _cont->_end, _cont->_pred);
    			}
    		private:
    			const_iterator();
    		};
    
    		class iterator : public const_iterator {
    		public:
    			iterator(Iter iter, const filter & cont) :const_iterator(iter, cont) { }
    			iterator(const iterator & i) :const_iterator(i) { }
    			iterator & operator = (const iterator & i) { const_iterator::operator=(i); return *this; }
    			~iterator() { }
    			reference operator*() { return *_iter; }
    			pointer operator->() { return &*_iter;  }
    		private:
    			iterator();
    		};
    
    		iterator begin() { return iterator(_begin, *this); }
    		iterator end() { return iterator(_end, *this); }
    		const_iterator cbegin() const { return const_iterator(_begin, *this); }
    		const_iterator cend() const { return const_iterator(_end, *this); }
    	private:
    		Iter _begin, _end;
    		Pred _pred;
    	private:
    		filter();
    	}; 
    }
    
    template<typename Iter, typename Pred> 
    detail::filter<Iter, Pred> filter(Iter begin, Iter end, Pred pred) {
    	return detail::filter<Iter, Pred>(begin, end, pred);
    }
    
    #endif // FILTER_H

    Тривиальная крестовщина, ничего выдающегося. Внезапно подумалось, что подобный контейнер был бы довольно удобен.
    Мне просто любопытно, насколько быстро этот пост будет слит автоминусаторами :)

    Xom94ok, 29 Апреля 2015

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

    +141

    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
    class ammo
    {
        public:
            bool existance;
            COORD alloc;
        void print()
            {
                pole[alloc.Y+1][alloc.X]=' ';
                pole[alloc.Y][alloc.X]='*';
            }
    };
    
    ammo bullet[10];
    
    void initpole();
    void replaceunit();
    void printpole();
    void redirect();
    void fire();
    void ammomove();
    
    int main(int argc, char* argv[])               //òåëî
    {
        initpole();
        do{
            if(kbhit())
                redirect();
            printpole();
            ammomove();
            Sleep(0);
            }
        while(chk!=27);
        return 0;
    }
    
    void redirect()                              //обработка нажатой клавиши
    {
        chk=getch();
        if(chk==32)
            {
                bullet[bulletcounter].existance=true;
                if(bulletcounter<10)
                    bulletcounter++;
                else
                    bulletcounter=0;
                fire();
            }
        else if(chk==75 || chk==77)
            replaceunit();
        else if(chk==112)
            {
            system("pause");
            system("cls");
            }
    }
    
    void fire()                                         //инициализация пули
    {
        bullet[bulletcounter].alloc.X=x;
        bullet[bulletcounter].alloc.Y=y-3;
        if(bullet[bulletcounter].existance==true)
            bullet[bulletcounter].print();
    }
    
    void ammomove()                                     //сдвиг пули
    {
        if(bullet[bulletcounter].existance==true)
            if(bullet[bulletcounter].alloc.Y>1)
                {
                    bullet[bulletcounter].alloc.Y--;
                    bullet[bulletcounter].print();
                }
            else
                {
                    bullet[bulletcounter].existance=false;
                    pole[bullet[bulletcounter].alloc.Y][bullet[bulletcounter].alloc.X]=' ';
                }
    }

    по нажатию пробела должна вылетать пулька, но чегот не хочет она вылетать, есть идеи? :3

    Morozz, 29 Апреля 2015

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

    +145

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    int main () {
      constexpr int a = f ();
      constexpr int b = f ();
    
      static_assert (a != b, "fail");
    }

    Можно ли это успешно скомпилировать?
    http://b.atch.se/posts/non-constant-constant-expressions/
    Можно, лал.

    LispGovno, 29 Апреля 2015

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

    +147

    1. 1
    https://ideone.com/xM1uqd

    Bobik, 29 Апреля 2015

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