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

    +22

    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
    // поверка наличия указазанного флага в набора флагов
    bool __fastcall TfrmFieldsEditor::HasFlag(int nAFlag, int nAFlagsCollection)
    {
        bool bRetVal = false;
        std::bitset<8> bsFlagBits;
        bsFlagBits.reset();
        bsFlagBits = nAFlagsCollection;
        int nBitsCount = bsFlagBits.size();
        for(int i= 0 ; i < nBitsCount; ++i)
            {
            if(bsFlagBits[i]==1)
                {
                bsFlagBits.reset();
                bsFlagBits[i] = 1;
                if (bsFlagBits.to_ulong() == nAFlag)
                    {
                    bRetVal = true;
                    break;
                    }
                else
                    bsFlagBits = nAFlagsCollection;
                }
     
     
     
            }
        return bRetVal;
    }

    Ulysses, 13 Мая 2013

    Комментарии (8)
  2. Java / Говнокод #12988

    +69

    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
    package javaapplication4;
    import java.util.Scanner;
    
    public class JavaApplication4 {    
        public static void main(String[] args) {            
            int powerOfTwo[] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};
            int lengthOfHammingCode = 0;      
            Scanner in = new Scanner(System.in);
            System.out.print("Введите двоичное число, которое хотите закодировать: "); String code = in.nextLine();
            in.close();
            if (code.length() < 2) System.out.println("Слишком маленькое число.");
            
            boolean controlBits[] = new boolean[code.length()*2+1]; 
            int i = 0;
            
            //---------------ВЫЧИСЛЯЕМ ДЛИНУ КХ И КОНТРОЛЬНЫЕ БИТЫ------------------
             do {        
                 controlBits[powerOfTwo[i]] = true;
                 i++;
            } while (powerOfTwo[i] <= (code.length()+1)); // Цикл ищет такую НАИМЕНЬШУЮ степень двойки, которая > либо = длине входного кода.
             
            lengthOfHammingCode = code.length() + i;   
            //----------------------------------------------------------------------
            
            //-------------ЗАПИСЫВАЕМ ВВЕДЁННЫЙ КОД С КОНТРОЛЬНЫМИ БИТАМИ-----------
            boolean hammingCode[] = new boolean[lengthOfHammingCode];       
            int j = 0;
            for (i = 0; i < lengthOfHammingCode; i++) { // Устанавливаем биты для промежуточного кода. 
                if (!controlBits[i+1]) {
                    hammingCode[i] = (code.charAt(j) == '1');
                    j++;
                }
            }
            //----------------------------------------------------------------------
            
            System.out.print("Промежуточный код с контрольными битами: [");
            for (int k = 0; k < lengthOfHammingCode; k++) {
                char c = (hammingCode[k]) ? '1' : '0';
                if (controlBits[k+1]) System.out.print("!");
                System.out.print(c);
            }
            System.out.print("]\n");
            
            //---------------------ВЫЧИСЛЯЕМ ЗНАЧЕНИЕ КОНТРОЛЬНЫХ БИТ----------------
            for (i = 0; i < (lengthOfHammingCode - code.length()); i++) { // Цикл проходится по всем контрольным битам.
                boolean nextBit = false;         
                for (j = powerOfTwo[i]-1; j < lengthOfHammingCode; j += powerOfTwo[i]*2) {
                    for (int n = j; n < j + powerOfTwo[i]; n++) {
                        if (n > lengthOfHammingCode - 1) break;
                        nextBit ^= hammingCode[n]; // Ксорим со следующим битом
                    }             
                }           
                hammingCode[powerOfTwo[i]-1] = nextBit; // Записываем значение i-ного бита.
            }
            //----------------------------------------------------------------------
            
            System.out.print("Код Хемминга с контрольными битами: [");
            for (int k = 0; k < lengthOfHammingCode; k++) {
                char c = (hammingCode[k]) ? '1' : '0';
                System.out.print(c);
            }
            System.out.print("]\n"); 
        }
    }

    Реализация вычисления кода Хемминга по заданному двоичному коду.

    Govnocoder#0xFF, 13 Мая 2013

    Комментарии (6)
  3. 1C / Говнокод #12987

    −167

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    МассивКоэффициентов = Новый Структура;
    -------------------------------------------------------------------------------------------------------
    Если Элемент.Имя = "КнопкаПерсонал" тогда
    	СписокВидоврасчета = 1;
    иначе
    	СписокВидоврасчета = 2;
    конецЕсли;

    "Принцип наименьшего удивления" при выборе имен переменных в работе

    doom2good, 13 Мая 2013

    Комментарии (4)
  4. JavaScript / Говнокод #12986

    +157

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    (function() {
        (function init() {
            document.addEventListener("DOMContentLoaded", DOMContentLoaded, false);
        })();
    })();
    
    function DOMContentLoaded() {
        //...
    }

    И да, jQuery подключена на странице.

    madhead, 13 Мая 2013

    Комментарии (7)
  5. ActionScript / Говнокод #12985

    −163

    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
    public function algorithmChanged(arg1:flash.events.Event):*
            {
                var loc1:*;
                loc1 = null;
                if (currentAlgorithm != null)
                    removeChild(currentAlgorithm);
                loc1 = arg1.target.selectedLabel;
                currentAlgorithm = null;
                animationManager.resetAll();
                if (loc1 != "Heap")
                    if (loc1 != "Comparison Sort")
                        if (loc1 != "Counting Sort")
                            if (loc1 != "Bucket Sort")
                                if (loc1 != "Radix Sort")
                                    if (loc1 != "Heap Sort")
                                        if (loc1 != "Binary Search Tree")
                                            if (loc1 != "AVL Tree")
                                                if (loc1 != "Open Hashing")
                                                    if (loc1 != "Closed Hashing")
                                                        if (loc1 != "Graph")
                                                            if (loc1 != "DFS")
                                                                if (loc1 != "BFS")
                                                                    if (loc1 != "Connected Components")
                                                                        if (loc1 != "Dijkstra")
                                                                            if (loc1 != "Prim")
                                                                                if (loc1 != "Kruskal")
                                                                                    if (loc1 != "Topological Sort")
                                                                                        if (loc1 != "Floyd-Warshall")
                                                                                            if (loc1 != "B Tree")
                                                                                                if (loc1 != "Binomial Queue")
                                                                                                    if (loc1 != "Disjoint Sets")
                                                                                                        if (loc1 != "Array Stack")
                                                                                                            if (loc1 != "Array Queue")
                                                                                                                if (loc1 != "Linked List Stack")
                                                                                                                    if (loc1 != "Linked List Queue")
                                                                                                                        if (loc1 != "Red Black Tree")
                                                                                                                            if (loc1 != "Closed Hashing (buckets)")
                                                                                                                                if (loc1 == "B+ Tree")
                                                                                                                                {
                                                                                                                                    currentAlgorithm = new BPlusTree(animationManager);
                                                                                                                                    addChildAt(currentAlgorithm, 0);
                                                                                                                                }
                                                                                                                            else 
                                                                                                                            {
                                                                                                                                currentAlgorithm = new ClosedHash2(animationManager);
                                                                                                                                addChildAt(currentAlgorithm, 0);
                                                                                                                            }
                                                                                                                        else 
                                                                                                                        {
                                                                                                                            currentAlgorithm = new RedBlackTree(animationManager);
                                                                                                                            addChildAt(currentAlgorithm, 0);
                                                                                                                        }
    и т.д. пока не закроются все if'ы

    Визуализатор сортировок университета Сан-Франциско

    alexprey, 12 Мая 2013

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

    +127

    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
    //top edge
                if (normal)
                    for (int i = y; i > 0; i--)
                        if (_map[x, i] == 0)
                            if ((i - 1) > minDist) { mnt.Corners[0] = new Point(x, i - 1 + minDist); break; }
                            else { normal = false; mnt.Height = -1; }
    
                //top-right edge
                if (normal)
                    for (int i = 0; ((x + i) < MAP_SIZE) & ((y - i) > 0); i++)
                        if (_map[x + i, y - i] == 0)
                            if (Math.Sqrt(2) * (MAP_SIZE - i) > minDist) { mnt.Corners[1] = new Point(x + i - 1 - minDist, y - i + 1 + minDist); break; }
                            else { normal = false; mnt.Height = -1; }
    
                //right edge
                if (normal)
                    for (int i = x; i < MAP_SIZE; i++)
                        if (_map[i, y] == 0)
                            if ((MAP_SIZE - i - 1) > minDist) { mnt.Corners[2] = new Point(i - 1 - minDist, y); break; }
                            else { normal = false; mnt.Height = -1; }
    
                //bottom-right edge
                if (normal)
                    for (int i = 0; ((x + i) < MAP_SIZE) & ((y + i) < MAP_SIZE); i++)
                        if (_map[x + i, y + i] == 0)
                            if (Math.Sqrt(2) * (MAP_SIZE - i) > minDist) { mnt.Corners[3] = new Point(x + i - 1 - minDist, y + i - 1 - minDist); break; }
                            else { normal = false; mnt.Height = -1; }
    
                //bottom edge
                if (normal)
                    for (int i = y; i > 0; i++)
                        if (_map[x, i] == 0)
                            if ((i - 1) > minDist) { mnt.Corners[4] = new Point(x, i - 1 - minDist); break; }
                            else { normal = false; mnt.Height = -1; }
    
                //bottom-left edge
                if (normal)
                    for (int i = 0; ((x - i) < MAP_SIZE) & ((y + i) > 0); i++)
                        if (_map[x - i, y + i] == 0)
                            if (Math.Sqrt(2) * (MAP_SIZE - i) > minDist) { mnt.Corners[5] = new Point(x - i + 1 + minDist, y + i - 1 - minDist); break; }
                            else { normal = false; mnt.Height = -1; }
    
                //left edge
                if (normal)
                    for (int i = x; i > 0; i--)
                        if (_map[i, y] == 0)
                            if ((i + 1) > minDist) { mnt.Corners[6] = new Point(i + 1 + minDist, y); break; }
                            else { normal = false; mnt.Height = -1; }
    
                //top-left edge
                if (normal)
                    for (int i = 0; ((x - i) > 0) & ((y - i) > 0); i++)
                        if (_map[x - i, y - i] == 0)
                            if (Math.Sqrt(2) * (MAP_SIZE - i) > minDist) { mnt.Corners[7] = new Point(x - i + 1 + minDist, y - i + 1 + minDist); break; }
                            else { normal = false; mnt.Height = -1; }

    Мне было лень думать. Очень лень.

    RaZeR, 11 Мая 2013

    Комментарии (1)
  7. Си / Говнокод #12983

    +132

    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    void usage(char *progname)
    {
        fprintf(stderr,"Usage: %s [-i <interval>]\n",progname);
        exit(1);
    }
    
    int main(int argc, char *argv[])
    {
        int arg;
        size_t alloc = 1000;
        uid_t userid = getuid();
    
        while ((arg = getopt(argc,argv,"")) != -1) {
            switch(arg) {
                default:
                    usage(argv[0]);
                    break;
            }
        }
    
        //const char *basecmd = "sudo turbostat -s -i 1 2>&1";
        char ghz[8];
        char *junkbuffer = malloc(sizeof(char)*1000);
        const char *basecmd = "turbostat -s -i 1 2>&1";
    
        seteuid(0);
        setuid(0);
        FILE *turboPipe = popen(basecmd,"r");
        seteuid(userid);
        setuid(userid);
    
        /* ignore first line */
        getline(&junkbuffer,&alloc,turboPipe);
        fscanf(turboPipe,"%*s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n",ghz);
        free(junkbuffer);
    
        fprintf(stdout,"%s\n",ghz);
        pclose(turboPipe);
    
        return 0;
    }

    Установил новый Debian на ноут. Искал инфу по Intel TurboBoost.
    Наткунлся на это
    http://technicallyliving.blogspot.com/2012/06/intel-turboboost-and-linux.html

    Улыбнул вайл-свитч. Что мешало автору обойтись без свитча?

    denis90, 09 Мая 2013

    Комментарии (2)
  8. Java / Говнокод #12982

    +79

    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
    package testapp;
    
    public class TestApp {
        
        public static void main(String[] args) {
            int count = 0;
            for (int i = 0; i < 1000000; i++) {
                if (isLucky(i)) {
                    System.out.println(i);
                    count++;
                }
            }
            System.out.println("Total: " + count);
        }
        
        static boolean isLucky(final int n) {
            String num = Integer.toString(n);
            switch(6 - num.length()) {
                case 1: 
                    num = "0".concat(num);
                    break;
                case 2:
                    num = "00".concat(num);
                    break;
                case 3:
                    num = "000".concat(num);
                    break;
                case 4: 
                    num = "0000".concat(num);
                    break;
                case 5: 
                    num = "00000".concat(num);
                    break;
            }
            
            return (((getDigitAt(num, 0) + getDigitAt(num, 1) == getDigitAt(num, 2) + getDigitAt(num, 3))) & ((getDigitAt(num, 2) + getDigitAt(num, 3) == getDigitAt(num, 4) + getDigitAt(num, 5))));
        }
        
        static int getDigitAt(String number, int index) {
            return Character.getNumericValue(number.charAt(index));
        }
    }

    Моё. Проверяет числа на "счастливость". Число счастливое, если в 6-значном числе сумма каждой пары соседних цифр одинакова. Т.е 211221 - счастливое, т.к. (2+1) = (1+2) = (2+1)

    Govnocoder#0xFF, 09 Мая 2013

    Комментарии (29)
  9. Java / Говнокод #12981

    +89

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    public void render(){
    	for(int y = 0; y < height; y++){
    		if(y < 0 || y >= height) break;
    		for(int x = 0; x < width; x++){
    			if(x < 0 || x >= width) break;
    			int tileIndex = (x >> 4) + (y >> 4) * 64;
    			pixels[x + y * width] = tiles[tileIndex];
    		}
    	}
    }

    чтоб уж наверняка
    http://www.youtube.com/watch?v=uPkXZmOuqGI

    lowercase, 08 Мая 2013

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

    −103

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    sub exdef {
        my ($hash, $key) = @_;
    
        if (exists $hash->{$key} && defined $hash->{$key}) {
            return 1;
        }
    
        return 0;
    }

    No comments.

    BTW.
    http://perldoc.perl.org/functions/exists.html
    http://perldoc.perl.org/functions/defined.html

    Elvenfighter, 08 Мая 2013

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