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

    +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
    int main(int argc, char* argv[])
    {
    
         SetConsoleCP(1251);// установка кодовой страницы win-cp 1251 в поток ввода
       SetConsoleOutputCP(1251); // установка кодовой страницы win-cp 1251 в поток вывода
        setlocale(LC_ALL,"Ukrainian");
    
        if(argc<2)
        {
            argv[1] = (char*)malloc(500*sizeof(char));
            //printf("Vvedit' imya vxidnogo failu: \n");
            //scanf("%s",argv[1]);
            argv[1]="1.txt";
    
        }

    Abbath, 14 Февраля 2013

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

    +24

    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
    template <typename IdType, class Tag>
    class id {
    public:
        typedef IdType value_type;
    
        explicit id(value_type val)
            : value(val)
        {}
    
        bool operator==(id rhs)
        {
            return rhs.value == value;
        }
    
        bool operator!=(id rhs)
        {
            return rhs.value != value;
        }
    
        value_type value;
    };
    
    #define HASKELL_NEWTYPE(type_name, value_type) struct __##type_name##_tag__ {}; \
        typedef ::id<value_type, __##type_name##_tag__> type_name

    Когда newtype нет, но очень хочется.
    http://ideone.com/VRu56j
    Простите за синтетику

    roman-kashitsyn, 14 Февраля 2013

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

    +9

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    y=fopen(lex,"w+");
    fclose(y);
    FILE *r=fopen(result,"w+");
    fclose(r);
    FILE *k=fopen("pryklad.obj","w+");
    fclose(k);

    Abbath, 13 Февраля 2013

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

    +12

    1. 001
    2. 002
    3. 003
    4. 004
    5. 005
    6. 006
    7. 007
    8. 008
    9. 009
    10. 010
    11. 011
    12. 012
    13. 013
    14. 014
    15. 015
    16. 016
    17. 017
    18. 018
    19. 019
    20. 020
    21. 021
    22. 022
    23. 023
    24. 024
    25. 025
    26. 026
    27. 027
    28. 028
    29. 029
    30. 030
    31. 031
    32. 032
    33. 033
    34. 034
    35. 035
    36. 036
    37. 037
    38. 038
    39. 039
    40. 040
    41. 041
    42. 042
    43. 043
    44. 044
    45. 045
    46. 046
    47. 047
    48. 048
    49. 049
    50. 050
    51. 051
    52. 052
    53. 053
    54. 054
    55. 055
    56. 056
    57. 057
    58. 058
    59. 059
    60. 060
    61. 061
    62. 062
    63. 063
    64. 064
    65. 065
    66. 066
    67. 067
    68. 068
    69. 069
    70. 070
    71. 071
    72. 072
    73. 073
    74. 074
    75. 075
    76. 076
    77. 077
    78. 078
    79. 079
    80. 080
    81. 081
    82. 082
    83. 083
    84. 084
    85. 085
    86. 086
    87. 087
    88. 088
    89. 089
    90. 090
    91. 091
    92. 092
    93. 093
    94. 094
    95. 095
    96. 096
    97. 097
    98. 098
    99. 099
    100. 100
    class Thread
    {
    public:
    	Thread(const Thread&);
    	Thread() : handle(NULL), running(false), finished(false)
    	{
    		handle = (HANDLE)(_beginthreadex(NULL, 0, &(Thread::threadRun), this, CREATE_SUSPENDED, NULL));
    	}
    	~Thread()
    	{
    		if(isRunning()) {
    			TerminateThread(handle, 0);
    		} 
    
    		CloseHandle(handle);
    	}
    	void start(void* arg)
    	{
    		if(isRunning() || isFinished()) {
    			throw Exception("Thread is running or finished!");
    		} else {
    			this->arg = arg;
    			ResumeThread(handle);
    		}
    		
    	}
    	void pause()
    	{
    		if(isRunning()) {
    			SuspendThread(handle);
    		} else {
    			throw Exception("Thread is not running or finished!");
    		}
    	}
    	void resume()
    	{
    		if(!(isRunning())) {
    			throw Exception("Thread is finished!");
    		} else {
    			ResumeThread(handle);
    		}
    	}
    	void stop()
    	{
    		if(isRunning()) {
    			TerminateThread(handle, 0);
    			running = false;
    			finished = true;
    			throw Exception("Thread stopped!");
    		} else {
    			throw Exception("Thread is not running or finished!");
    		}
    	}
    	void setPriority(ThreadPriority priority)
    	{
    		if(isFinished()) {
    			throw Exception("Thread is finished!");
    		} else {
    			switch(priority) {
    			case ThreadPriorityLow:
    				SetThreadPriority(handle, THREAD_PRIORITY_LOWEST);
    				break;
    			case ThreadPriorityNormal:
    				SetThreadPriority(handle, THREAD_PRIORITY_NORMAL);
    				break;
    			case ThreadPriorityHigh:
    				SetThreadPriority(handle, THREAD_PRIORITY_HIGHEST);
    				break;
    			default:
    				throw Exception("Invalid priority!");
    				break;
    			}
    		}
    	}
    	bool isRunning()
    	{
    		return (running);
    	}
    	bool isFinished()
    	{
    		return (finished);
    	}
    protected:
    	virtual void run(void *arg) = 0;
    private:
    	static unsigned int __stdcall threadRun(void *arg)
    	{
    		Thread *thread = static_cast<Thread*>(arg);
    		thread->running = true;
    		thread->run(thread->arg);
    		thread->running = false;
    		thread->finished = true;
    		_endthreadex(0);
    		return (0);
    	}
    	void *arg;
    	HANDLE handle;
    	bool running;
    	bool finished;
    };

    Из предыдущей оперы.

    dreesto, 10 Февраля 2013

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

    +7

    1. 001
    2. 002
    3. 003
    4. 004
    5. 005
    6. 006
    7. 007
    8. 008
    9. 009
    10. 010
    11. 011
    12. 012
    13. 013
    14. 014
    15. 015
    16. 016
    17. 017
    18. 018
    19. 019
    20. 020
    21. 021
    22. 022
    23. 023
    24. 024
    25. 025
    26. 026
    27. 027
    28. 028
    29. 029
    30. 030
    31. 031
    32. 032
    33. 033
    34. 034
    35. 035
    36. 036
    37. 037
    38. 038
    39. 039
    40. 040
    41. 041
    42. 042
    43. 043
    44. 044
    45. 045
    46. 046
    47. 047
    48. 048
    49. 049
    50. 050
    51. 051
    52. 052
    53. 053
    54. 054
    55. 055
    56. 056
    57. 057
    58. 058
    59. 059
    60. 060
    61. 061
    62. 062
    63. 063
    64. 064
    65. 065
    66. 066
    67. 067
    68. 068
    69. 069
    70. 070
    71. 071
    72. 072
    73. 073
    74. 074
    75. 075
    76. 076
    77. 077
    78. 078
    79. 079
    80. 080
    81. 081
    82. 082
    83. 083
    84. 084
    85. 085
    86. 086
    87. 087
    88. 088
    89. 089
    90. 090
    91. 091
    92. 092
    93. 093
    94. 094
    95. 095
    96. 096
    97. 097
    98. 098
    99. 099
    100. 100
    template<typename T>
    class Enumerable
    {
    public:
    	Enumerable() : enumerableInProcess(false) { };
    	virtual void begin() = 0;
    	virtual void end() = 0;
    	virtual bool enumeration(T* item) = 0;
    protected:
    	bool enumerableInProcess;
    };
    template<typename T>
    class List : Enumerable<T*>
    {
    public:
    	class ListItem
    	{
    	public:
    		friend class List;
    		T item;
    		ListItem(T item) : item(item), next(nullptr), previous(nullptr)
    		{
    		}
    	private:
    		class ListItem* next;
    		class ListItem* previous;
    	};
    
    	/* ... */
    	
    	void begin()
    	{
    		if(enumerableInProcess) {
    			throw Exception("Error Enumerable!");
    		}
    
    		enumerableInProcess = true;
    		enumerationItem = first;
    	}
    	bool enumeration(T** item)
    	{
    		if(enumerableInProcess) {
    			if(enumerationItem != nullptr) {
    				(*item) = &(enumerationItem->item);
    				enumerationItem = enumerationItem->next;
    				return (true);
    			} else {
    				(*item) = nullptr;
    				return (false);
    			}
    		} else {
    			throw Exception("Error Enumerable!");
    		}
    	}
    	bool enumeration(ListItem **listItem)
    	{
    		if(enumerableInProcess) {
    			if(enumerationItem != nullptr) {
    				(*listItem) = enumerationItem;
    				enumerationItem = enumerationItem->next;
    				return (true);
    			} else {
    				(*listItem) = nullptr;
    				return (false);
    			}
    		} else {
    			throw Exception("Error Enumerable!");
    		}
    	}
    	void end()
    	{
    		if(!enumerableInProcess) {
    			throw Exception("Error Enumerable!");
    		}
    		enumerableInProcess = false;
    	}
    private:
    	const int size;
    	int count;
    	ListItem *first;
    	ListItem *last;
    	ListItem *enumerationItem;
    };
    void list_t_1()
    {
    	List<int> list(8);
    	List<int>::ListItem *item;
    	list.add(1);
    	list.add(2);
    	list.add(4);
    	list.add(8);
    	list.add(16);
    
    	list.begin();
    	while(list.enumeration(&item))
    	{
    		printf("%i\n", (item->item));
    	}
    	list.end();
    }

    dreesto, 09 Февраля 2013

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

    +25

    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
    if (delitel.number.at(0) == '0')
                         {
                             chastnoe.number.push_back('D');
                             chastnoe.number.push_back('e');
                             chastnoe.number.push_back('L');
                             chastnoe.number.push_back('e');
                             chastnoe.number.push_back('N');
                             chastnoe.number.push_back('i');
                             chastnoe.number.push_back('e');
                             chastnoe.number.push_back(' ');
                             chastnoe.number.push_back('n');
                             chastnoe.number.push_back('a');
                             chastnoe.number.push_back(' ');
                             chastnoe.number.push_back('0');
     
                             return chastnoe;
                         }

    Из чьей-то реализации длинной арифметики

    west_coast_coders, 08 Февраля 2013

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

    +27

    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
    inline double poly(double x, const double *c, int k) const {
            double y = c[k];
            switch (k) {
                case 15: y = y * x + c[14];
                case 14: y = y * x + c[13];
                case 13: y = y * x + c[12];
                case 12: y = y * x + c[11];
                case 11: y = y * x + c[10];
                case 10: y = y * x + c[ 9];
                case  9: y = y * x + c[ 8];
                case  8: y = y * x + c[ 7];
                case  7: y = y * x + c[ 6];
                case  6: y = y * x + c[ 5];
                case  5: y = y * x + c[ 4];
                case  4: y = y * x + c[ 3];
                case  3: y = y * x + c[ 2];
                case  2: y = y * x + c[ 1];
                case  1: y = y * x + c[ 0];
                case  0: break;
            }
            return y;
        }

    Схема Горнера для вычисления значения многочлена в точке

    uranix, 08 Февраля 2013

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

    +12

    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
    class DimensionAction : public PlmAction {
     public:
       virtual const std::type_info& type() const {
         return typeid( DimensionAction );
         }
    
      };
    
    class Object { // Где-то  в недрах иерархии...
      ...
      virtual const std::type_info& type() const = 0;
      ...
      };

    Зачем?! Почему?

    Try, 06 Февраля 2013

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

    +16

    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
    BOOL EnsureThreadIsSuspended (HANDLE hThread, Thread* pThread)
    {
        STATIC_CONTRACT_NOTHROW;
        STATIC_CONTRACT_GC_NOTRIGGER;
    
        WRAPPER_CONTRACT;
    
        CONTEXT ctx;
        ctx.ContextFlags = CONTEXT_INTEGER;
        BOOL ret;
        ret = ::GetThreadContext(hThread, &ctx);
    
        return ret;
    }

    А ведь и правда, никто не гарантирует, что поток будет остановлен к тому моменту, когда SuspendThread() вернет управление...

    Ccik, 06 Февраля 2013

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

    +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
    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
    #include <iostream>
    namespace detail
    {
      class CRWO;
      class CRO;
      class CWO;
      class CO;
    }
    typedef detail::CRWO&  CRWO;
    typedef detail::CRO&  CRO;
    typedef detail::CWO&  CWO;
    typedef detail::CO&  CO;
    class C
    {
        friend class detail::CRWO; 
        friend class detail::CRO; 
        friend class detail::CWO; 
        friend class detail::CO; 
      public:
        C( int a ) : value(a) {}
        ~C() {};
        operator CRWO() { return *static_cast<detail::CRWO*>(static_cast<void*>(this)); }
        operator CRO() { return *static_cast<detail::CRO*>(static_cast<void*>(this)); }
        operator CWO() { return *static_cast<detail::CWO*>(static_cast<void*>(this)); }
        operator CO() { return *static_cast<detail::CO*>(static_cast<void*>(this)); }
      private:
        void set( int newValue ) { value = newValue; }
        int get() { return value; }
      private:
        int value;
    };
    class detail::CRWO
    {
      public:
        void set( int newValue ) { static_cast<C*>(static_cast<void*>(this))->set( newValue ); }
        int get() { return static_cast<C*>(static_cast<void*>(this))->get( ); }
        operator ::CRO() { return *static_cast<detail::CRO*>(static_cast<void*>(this)); }
        operator ::CWO() { return *static_cast<detail::CWO*>(static_cast<void*>(this)); }
        operator ::CO() { return *static_cast<detail::CO*>(static_cast<void*>(this)); }    
      private:
        CRWO(); CRWO(const CRWO&);~CRWO();CRWO& operator=(const CRWO&);void operator&(); void operator*();
    };
    class detail::CWO
    {
      public:
        void set( int newValue ) { static_cast<C*>(static_cast<void*>(this))->set( newValue ); }
         operator ::CO() { return *static_cast<detail::CO*>(static_cast<void*>(this)); }    
      private:
        CWO(); CWO(const CWO&);~CWO();CWO& operator=(const CWO&);void operator&(); void operator*();
    };
    class detail::CRO
    {
      public:
        int get() { return static_cast<C*>(static_cast<void*>(this))->get( ); }
        operator ::CO() { return *static_cast<detail::CO*>(static_cast<void*>(this)); }    
      private:
        CRO(); CRO(const CRO&);~CRO();CRO& operator=(const CRO&);void operator&(); void operator*();
    };
    class detail::CO
    {
      public:
      private:
        CO(); CO(const CO&);~CO();CO& operator=(const CO&);void operator&(); void operator*();
    };int main(int argc, char *argv[])
    {
    C c(3);
    CRWO rwo = c;
    CRO ro = c;
    CWO wo = c;
    CO o = c;
      std::cout  << rwo.get() << std::endl;
      wo.set( 5);
      std::cout  << ro.get() << std::endl;
    return 0;
    }

    Оттуда.

    Автор требует указывать авторство при копировании.

    LispGovno, 02 Февраля 2013

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