1. Си / Говнокод #13422

    +126

    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
    typedef struct {
      uint32_t id;
      char * title;
    } dbrow_t;
    
    typedef struct {
      char * context;
      dbrow_t dbrow;
    } parser_context_t;
    
    typedef enum {    
      bad_char = 0,
      identifer,
      number,
      state_end_context,  
      state_end_line
    } type_t;
    
    type_t ch_d[0x100] = { [0] = state_end_line, [1 ... 0xff] = bad_char, ['0' ... '9'] = number, ['a' ... 'z'] = identifer, ['\n'] = state_end_context};
    
    inline char * do_other_type(char * p, type_t type) { while(ch_d[*(++p)] == type); return p;}
    
    static inline uint64_t bad_char_headler(parser_context_t * context) { context->context = do_other_type(context->context, bad_char); return 1;}
    
    static inline uint64_t number_headler(parser_context_t * context) {
      uint32_t id = 0; char * p = context->context;
      while(({ id *= 10; id += (*p - '0'); ch_d[*(++p)];}) == number);
      context->dbrow.id = id; context->context = p;
      return 1;
    }
    
    static inline uint64_t identifer_headler(parser_context_t * context) {
      char * p = context->context, * title = context->dbrow.title;
      char * end_identifer = do_other_type(p, identifer);
      memcpy(title, p, (end_identifer - p)); *(title + (end_identifer - p)) = 0;
      context->context = end_identifer;
      return 1;
    }
    
    static inline uint64_t end_context_headler(parser_context_t * context) {
      context->context = do_other_type(context->context, state_end_context);
      if(context->dbrow.id && *context->dbrow.title)
        fprintf(stderr, "id = %u, title = %s\n", context->dbrow.id, context->dbrow.title);
      context->dbrow.id = 0, *context->dbrow.title = 0;
      return 1;
    }
    
    static inline uint64_t end_line_headler(parser_context_t * context) { return 0; }
    
    typedef uint64_t(*headler_ptr_t)(parser_context_t *);
    
    headler_ptr_t headlers[] = { [bad_char] = bad_char_headler, [identifer] = identifer_headler, [number] = number_headler, [state_end_context] = end_context_headler, [state_end_line] = end_line_headler };
    
    int main(void) {
        char * content = strcpy(malloc(50), "1|raz\n11|dva\n21|tri\n31|shestb\n5000|test\n|||\n||\n|\n");
        parser_context_t context = (parser_context_t){ .context = content, .dbrow = (dbrow_t){ .title = malloc(1000)}};
        while(headlers[ch_d[*(context.context)]](&context));
        return 0;
    }

    Решил пацанам показать как писать парсер - вышло говно. Хотя так-то должно тащить - забенчите кто-нибудь, настолько это гумно медленнее strtok()'а.

    superhackkiller1997, 15 Июля 2013

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

    +128

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    public void BuildInsertClause(OleDbCommand cmd, ObjectState objState)
            {
                  StringBuilder builder = new StringBuilder();
                  ..........
                  cmd.CommandText = builder.ToString() + "(" + columns.ToString() + ") VALUES (" +
                  values.ToString() + ")";
            }

    http://solidcoding.blogspot.ru/2008/01/linq-to-excel-provider-25.html
    Еще много смешного, для затравки:

    object val = reader[col.GetSelectColumn()];
    if (val is DBNull)
    {
    val = null;
    }

    neeedle, 15 Июля 2013

    Комментарии (17)
  3. PHP / Говнокод #13420

    +106

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    $items = array('palto','noski','shapki');
        $colors = array('red','black','white');
        $materials = array('kozha','meh','aluminij');
    
        foreach($items as $item) {
          foreach($colors as $color) {
            foreach($materials as $material) {
              echo $item.'-'.$color.'-'.$material;
              echo '</br>';
            }
          }
        }

    Задача:
    Группа 1.
    Пальто, Шапки, Носки
    Группа 2.
    Красный, Синий, Зеленый
    Группа 3.
    Мех, Кожа

    Необходимы комбинации следующего вида, пример:
    1) Пальто - красный - мех
    2) Пальто - красный - кожа
    3) Пальто - синий - мех
    ....
    N) Носки - зеленый - кожа

    При том условии, что группа занимает только свое место в порядке. Т.е. пальто не может быть 2-ым или 3-им словом в комбинации.

    Vasiliy, 15 Июля 2013

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

    +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
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    #include<iostream>
    #include<set>
    #include<vector>
    #include<hash_set>
    #include<math.h>
    #include<stdlib.h>
    using namespace std;
    inline int po(int a,int b){
    int ans =1;
    for(int i=0;i<b;++i)ans*=a;
    return ans;
    }
    inline bool isnotsq(int a){
    	return sqrt(0.0+a)!=int(sqrt(a+0.0));
    }
    
    int main(){
    	int t,n,k;
    	cin>>t;
    	int SQRT  = 100000,primes[100100]={0};
    	primes[2]=1;
    	vector<int> pr;
    	pr.push_back(2);
    	for(int i = 3;i<=SQRT;i+=2){
    		if(primes[i]==0) {
    			pr.push_back(i);
    			for(int j = 2*i;j<=SQRT;j+=i) primes[j]=1;
    		}
    	}
    	while(t--){
    		cin>>n>>k;
    		int nn=n;
    		int divisors[1001];
    		int divisecount[1001]={0};
    		int divc=0;
    		int count = 0;
    		for(int i =0;i<pr.size();++i){
    			if(n%pr[i]==0) divisors[divc++]=pr[i];
    			while(n%pr[i]==0) divisecount[divc-1]++,n/=pr[i],count++;
    		}
    		if(n!=1) divisors[divc++]=n,divisecount[divc-1]=1;
    		//for(int i =0;i<divc;++i) cout<<divisors[i]<<' '<<divisecount[i]<<'\n';
    		vector< int> cbused;
    		vector<int> rem;
    		rem.push_back(0);
    		cbused.push_back(1);
    		
    		for(int i =0;i<divc;++i){
    			int cs = cbused.size();
    			for(int j =0;j<cs;++j){
    				int cc = divisors[i];
    				int ops=1;
    				while(1){
    					if(nn%(cbused[j]*cc)==0) {
    						if(isnotsq(cbused[j]*cc)) 
    							cbused.push_back(cbused[j]*cc),rem.push_back(rem[j]+ops);
    					}
    					else break;
    					cc*=divisors[i];
    					ops+=1;
    				}
     			}
    		}
    		//for(int  i = 0;i<cbused.size();++i) cout<<cbused[i]<<' '<<rem[i]<<'\n';
    		int siz= cbused.size();
    		set< pair<int,int > > anse;
    		anse.insert(make_pair(1,0));
    		for(int j = 1;j<siz;++j){
    			for(int i = cbused[j],k=1;nn%i==0;i*=cbused[j],k++) if(i%2==1) anse.insert(make_pair(i,k));
    		}
    		for(int i = 1;i<siz;++i){
    				vector<pair<int , int> > toa;
    				for(set< pair<int,int> >::iterator it=anse.begin();it!=anse.end();it++){
    					if(nn%(cbused[i]*it->first)==0&&it->second<k) toa.push_back(make_pair(cbused[i]*it->first,it->second+1));
    					for(int q=0;q<toa.size();q++) anse.insert(toa[q]);
    				}
    			}
    		
    		bool f = false;
    		for(set< pair<int,int> >::iterator it=anse.begin();it!=anse.end();it++){
    			if(it->first==nn&&it->second==k) f=true;
    		}
    		cout<<(f?"YES":"NO")<<"\n";
    		if(t==0) return 0;
    	}
    }

    Задано целое положительное число n. Выясните, может ли оно быть представлено в виде произведения k целых положительных чисел, ни одно из которых не является квадратом целого числа.
    (с яндекс.алгоритма)

    AvadaKedavra, 14 Июля 2013

    Комментарии (14)
  5. Java / Говнокод #13418

    +70

    1. 1
    final int mazeBlockSize = mazeBlock.getIconWidth();       // it is square, OKAY?!

    Как я пакмена писал

    vortexx1, 13 Июля 2013

    Комментарии (19)
  6. Си / Говнокод #13416

    +129

    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 <stdio.h>
    #include <conio.h>
    #include <string.h>
    void swap (char *x, char *y)
    {
        char temp;
        temp = *x;
        *x = *y;
        *y = temp;
    }
     
    void permute(char *a, int i, int n)
    {
        int j;
        if (i == n)
        printf("%s\n", a);
        else
        {
            for (j = i; j <= n; j++)
            {
                swap((a+i), (a+j));
                permute(a, i+1, n);
                swap((a+i), (a+j));
            }
        }
    }
     
     
    int main()
    {
        char str[80] ;
        int len=0,i;
        puts("Enter a string:\n\n");
        gets(str);
        for (i=0; str[i] != '\0'; i++)
        {
            len++;
        }
        permute(str, 0, len);
        getchar();
        return 0;
    }

    Источник:
    http://www.c-program-example.com/2012/04/c-program-to-find-all-permutations-of.html

    про strlen видимо никто не слышал

    denis90, 13 Июля 2013

    Комментарии (125)
  7. JavaScript / Говнокод #13414

    +155

    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
    getCastIds:
            function(checkCast){
                var i
                  , castIds = [this.cancelBlesId]
                  ;
                if(checkCast)
                {
                    for(i = 0; i < castIds.length; i++)
                    {
                      if(Empire.asset(castIds[i]))
                      {
                        castIds.splice(i,1);
                        i--;
                      }
                    }
                }
                return castIds.length ? castIds : [0];
            }

    Выстрелим себе в ногу.

    dioteos, 12 Июля 2013

    Комментарии (24)
  8. Куча / Говнокод #13413

    +132

    1. 1
    У МИНЯ ЕСТЬ АЙФОН 5 И БАЛЬШОЙ ДОМ В МАЙНКРАВТЕ А ЧИВО ДАБИЛСЯ ТЫ?

    Я БАГАТ И УСПЕШОН

    PragramistOtBoga, 12 Июля 2013

    Комментарии (6)
  9. Pascal / Говнокод #13412

    +86

    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
    procedure TForm1.Colorize;
    procedure SetStr(var s: string; const Style: TFontStyles; const Color, BackColor: Byte);
    var
      Format: TCharFormat2;
    begin
     If s<>'' Then
      begin
        FillChar(Format, SizeOf(Format), 0);
        Format.cbSize := SizeOf(Format);
        Format.dwMask:= CFM_BACKCOLOR or CFM_COLOR;
        Format.crBackColor:= GetIRCColor(BackColor, True);
        Format.crTextColor:= GetIRCColor(Color);
        if RichEdit1.HandleAllocated then
        SendMessage(RichEdit1.Handle, EM_SETCHARFORMAT, SCF_SELECTION,
          LPARAM(@Format));
        RichEdit1.SelAttributes.Style:= Style;
        RichEdit1.SelText:= s;
        RichEdit1.SelAttributes.Assign(RichEdit1.DefAttributes);
        s:= ''
      end;
    end;
    var
      Color, BackColor: Byte;
      Style: TFontStyles;
      CurrStr: string;
      I: Integer;
    begin
      Style:= [];
      Color:= DefForeColor;
      BackColor:= DefBackColor;
      CurrStr:= '';
      I:= 0;
      While (I<Length(Str))do
       begin
        Inc(I);
        case Str[I] of
          #31:
              begin
               SetStr(CurrStr, Style, Color, BackColor);
               If fsUnderLine in Style Then
                Exclude(Style, fsUnderLine)
               else
                Include(Style, fsUnderLine);
             end;
          #2:
             begin
               SetStr(CurrStr, Style, Color, BackColor);
               If fsBold in Style Then
                Exclude(Style, fsBold)
               else
                Include(Style, fsBold);
             end;
           #15, #13:
               begin
                SetStr(CurrStr, Style, Color, BackColor);
                Color:= DefForeColor;
                BackColor:= DefBackColor;
               end;
           #3:
              begin
                SetStr(CurrStr, Style, Color, BackColor);
                Inc(I);
                Color:= DefForeColor;
                If (Str[I] in ['0', '1'..'9'])Then
                 begin
                  Color:= StrToInt(Str[I]);
                  Inc(I);
                  If (Str[I] in ['0', '1'..'9'])Then
                   begin
                     Color:= StrToInt(IntToStr(Color)+Str[I]);
                     Inc(I)
                   end;
                 end;
                 If Str[I] = ',' Then                         //BackColor
                   begin
                    BackColor:= DefBackColor;
                    Inc(I);
                    If (Str[I] in ['0', '1'..'9'])Then
                     begin
                      BackColor:= StrToInt(Str[I]);
                      Inc(I);
                      If (Str[I] in ['0', '1'..'9'])Then
                       begin
                        BackColor:= StrToInt(IntToStr(BackColor)+Str[I]);
                        Inc(I)
                       end;
                      end;
                    end;
                   Dec(I) 
              end;
           else
            CurrStr:= CurrStr+Str[I]
        end;
       end;
       SetStr(CurrStr, Style, Color, BackColor);
    end;

    Процедура раскрашивающая текст из Log-файла mIRC загруженого в TRichEdit на форме.

    function GetIRCColor(const Color: Byte; const Back: Boolean=False): TColor;
    begin
    case Color of
    0: Result:= clWhite;
    1: Result:= clBlack;
    2: Result:= clNavy;
    3: Result:= clGreen;
    4: Result:= clRed;
    5: Result:= clMaroon;
    6: Result:= clPurple;
    7: Result:= $000080FF;
    8: Result:= clYellow;
    9: Result:= clLime;
    10: Result:= clTeal;
    11: Result:= clAqua;
    12: Result:= clBlue;
    13: Result:= clFuchsia;
    14: Result:= clGray;
    15: Result:= clSilver;
    else
    begin
    If Back Then
    Result:= clWhite
    else
    Result:= clBlack
    end;
    end;
    end;

    ASNightingale, 12 Июля 2013

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

    +11

    1. 1
    2. 2
    const listee* const nullablya = static_cast<listee*>(lst1);
    if(nullablya == NULL)

    LispGovno, 12 Июля 2013

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