elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a la Factorización De Semiprimos (RSA)


  Mostrar Temas
Páginas: [1] 2
1  Programación / Programación C/C++ / Operadores con bits - consulta en: 5 Noviembre 2012, 19:14 pm
Hola foreros. Tengo este codigo y lo que quiero es que devuelva 1 si el simbolo pasado a la funcion es alguno de los definidos arriba. Habia pensado en hacerle un corrimiento de bits dependiendo la posicion del bit encendido. por ej >> (int)sqrt(0x3FF % sym + 1) pero el problema es que este metodo no funciona con algunas constantes.. como puedo saber la posicion del bit encendido para moverlo y que si el argumento concuerde con alguna constante devuelva 1?

Código
  1.  
  2. #define BACKSLASH 0x0001
  3. #define SLASH     0x0002
  4. #define COLON     0x0004
  5. #define ASTERISK  0x0008
  6. #define QUESTION  0x0010
  7. #define QUOTES    0x0020
  8. #define LESS      0x0040
  9. #define GREATER   0x0080
  10. #define PIPE      0x0100
  11.  
  12. int is_symbol(int sym)
  13. {
  14.    return ((BACKSLASH | SLASH | COLON | ASTERISK | QUESTION | QUOTES | LESS | GREATER | PIPE) & sym);
  15. }
  16.  
  17.  

Gracias de antemano!  :D
2  Programación / Programación C/C++ / Problema con realloc - c en: 5 Noviembre 2012, 01:51 am
Hola foreros! asi como dice el titulo, estoy teniendo problemas al usar esta funcion, cada vez que ejecuto el programa el sistema lo aborta ya que alega que le estoy pasando un puntero invalido a realloc, he intentado varias cosas pero no hay caso, el error persiste.. Aca el codigo:

Código
  1. char *borrar_espacios(const char *s)
  2. {
  3. char *temp = NULL;
  4. int i;
  5.  
  6. temp = (char *) malloc(sizeof(char));
  7. for (i = 0; *s != '\0'; ++s)
  8. if (!isspace(*s)){
  9. temp = (char *) realloc(temp, sizeof(char));
  10. *temp = *s, ++temp, ++i;
  11. }
  12. *temp = '\n';
  13. return temp - i;
  14. }
  15.  

Gracias de antemano!  :D
3  Programación / Programación C/C++ / Consulta punteros (C) en: 28 Agosto 2012, 06:37 am
Hola gente! he aquí la consulta:
Tengo un sector de n unsigned char en la memoria reservado con malloc y para manejar los datos del sector uso otro puntero q apunte al inicio pero lo declaro como unsigned short pq quiero extraer los datos de a 2 bytes, he aqui la cuestion, cuando hago avanzar el puntero obviamente lo hace de a dos bytes, en ciertos casos quiero hacerlo avanzar de a un byte por lo que hago lo sig p = (unsigned char *)p + 1 , que funciona, pero el compilador me lanza la advertencia de que estoy asignando punteros de tipo incompatible lo cual es razonable. Hay alguna manera de hacerlo correctamente??
4  Programación / Programación C/C++ / Problema para liberar memoria - C en: 18 Abril 2012, 22:22 pm
Hola foreros!! Subo aqui un problema que tengo en un programa que estoy haciendo (no se si puede hacerse lo que quiero, hasta el momento no pude). El tema es el sig. desde una funcion llamo a otra que dentro declara un array estatico de punteros y muestre el dato relacionado con la posicion que le estoy pasando, el por que de declararlo estatico es para que no lo tenga que crear cada vez q vuelvo a entrar ya que la necesito en un bucle. Ahora quiero borrar el array estatico de la memoria ya q una vez salido del bucle no lo necesito mas, por eso devuelvo la direccion del array, pero cuando uso free() con la direccion de memoria devuelta en la funcion llamadora me tira error de memoria y aborta el programa. Repito, no se si se puede hacer lo que pretendo, por eso queria consultarlo con uds. Dejo aqui un ejemplo de lo que estoy tratando de lograr (No es el programa que estoy haciendo). Espero rtas y Gracias!! :)

Código
  1.  
  2. void funcion1()
  3. {
  4.     int i;
  5.     void *ptr = NULL;
  6.  
  7.     for (i = 0; i < 5; i++){
  8.         if (ptr == NULL)
  9.                 ptr = funcion2(i);
  10.         else
  11.                 funcion2(i);
  12.     }
  13.  
  14.     free(ptr);
  15. }
  16.  
  17. void *funcion2(int par)
  18. {
  19.     static char *str[] = {
  20.     "UNO",
  21.     "DOS",
  22.     "TRES",
  23.     "CUATRO",
  24.     "CINCO"
  25.     };
  26.  
  27.     printf("%s\n", str[par]);
  28.  
  29.     return &str;
  30. }
  31.  
  32.  
5  Programación / Programación C/C++ / Graficos de Tortuga (C++) en: 3 Marzo 2012, 21:41 pm
Hola foreros! dejo aqui un programa q hice basado en los graficos de tortuga. El programa recibe comandos q luego interpreta para dibujar sobre una matriz. Espero comentarios y sugerencias.
Saludos!

Código
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. const int floor_size = 20;
  7.  
  8. void mainMenu(void);
  9. void turtleGraphics(int [][floor_size], int []);
  10.  
  11. int main(void)
  12. {
  13.    const int max_command = 256;
  14.  
  15.    int floor[floor_size][floor_size] = {{false},{false}};
  16.    int command[max_command] = {false};
  17.  
  18. mainMenu();
  19.  
  20.    cout << "\nEnter commands(max " << max_command << "): ";
  21.    int i = 0;
  22.    while (command[i - 1] != 9){
  23.        cin >> command[i];
  24.        i++;
  25.    }
  26. cout << "\n" << endl;
  27. turtleGraphics(floor, command);
  28.  
  29.    return 0;
  30. }
  31.  
  32. void mainMenu(void)
  33. {
  34. cout << "*****" << setw(20) << "Turtle Graphics" << setw(10) << "*****" << endl;
  35. cout << "\nCommand" << setw(20) << "Meaning" << endl;
  36. cout << setw(7) << "1" << setw(20) << "Pen up" << endl;
  37. cout << setw(7) << "2" << setw(20) << "Pen down" << endl;
  38. cout << setw(7) << "3" << setw(20) << "Turn right" << endl;
  39. cout << setw(7) << "4" << setw(20) << "Turn left" << endl;
  40. cout << setw(7) << "5,10" << setw(20) << "Move forward 10"
  41. << "\n" << setw(27) << "spaces (or a number" << "\n"
  42. << setw(27) << "other than 10)" << endl;
  43. cout << setw(7) << "6" << setw(20) << "Print the" << "\n"
  44. << setw(15) << floor_size << "-by-" << floor_size
  45. << " array" << endl;
  46. cout << setw(7) << "9" << setw(20) << "End of data" << "\n"
  47. << setw(27) << "(sentinel)" << endl;
  48. }
  49.  
  50. void turtleGraphics(int flr[][floor_size], int cmd[])
  51. {
  52. int coord_x = 0, coord_y = 0, move = 0,
  53. row, column, i = 0;
  54. bool pen = false;
  55.  
  56. while (cmd[i] != 9){
  57. //Uncoment next line to show coord_x and coord_y
  58. //cout << "X: " << coord_x << "\nY: " << coord_y << endl;
  59. switch(cmd[i]){
  60. case 1: //Don't write
  61. pen = false;
  62. break;
  63. case 2: //Write
  64. pen = true;
  65. break;
  66. case 3: //Turn rigth
  67. if (move == 3)
  68. move = 0;
  69. else
  70. move++;
  71. break;
  72. case 4: //Turn left
  73. if (move == 0)
  74. move = 3;
  75. else
  76. move--;
  77. break;
  78. case 5: // Move turtle
  79. i++;
  80. switch(move){ //Nested switch (control direction)
  81. case 0: //Move down
  82. if ((cmd[i] + coord_y) > floor_size) //Check if it is in floor limits
  83. cmd[i] = floor_size - coord_y - 1; //If not, set move to the limit
  84.  
  85. for (row = coord_y; row < (cmd[i] + coord_y); row++)
  86. for (column = coord_x; column < coord_x + 1; column++)
  87. flr[row][column] = pen;
  88. coord_y = row;
  89. break;
  90. case 1: //Move rigth
  91. if ((cmd[i] + coord_x) > floor_size) //Check if it is in floor limits
  92. cmd[i] = floor_size - coord_x - 1; //If not, set move to the limit
  93.  
  94. for (row = coord_y; row < coord_y + 1; row++)
  95. for (column = coord_x; column < (cmd[i] + coord_x); column++)
  96. flr[row][column] = pen;
  97. coord_x = column;
  98. break;
  99. case 2: //Move up
  100. if (cmd[i] > coord_y) //Check if it is in floor limits
  101. cmd[i] = floor_size - 1; //If not, set move to the limit
  102.  
  103. for (row = coord_y; row > (coord_y - cmd[i]); row--)
  104. for (column = coord_x; column < coord_x + 1; column++)
  105. flr[row][column] = pen;
  106. coord_y = row;
  107. break;
  108. case 3: //Move left
  109. if (cmd[i] > coord_x) //Check if it is in floor limits
  110. cmd[i] = floor_size - 1; //If not, set move to the limit
  111.  
  112. for (row = coord_y; row < coord_y + 1; row++)
  113. for (column = coord_x; column > (coord_x - cmd[i]); column--)
  114. flr[row][column] = pen;
  115. coord_x = column;
  116. break;
  117. default:
  118. ;
  119. } //End nested switch
  120. break;
  121. case 6: // Print floor
  122. for (int i = 0; i < 20; i++){
  123. for (int j = 0; j < 20; j++){
  124. if (flr[i][j] == true)
  125. cout << setw(2) << "*";
  126. else
  127. cout << setw(2) << " ";
  128. }
  129. cout << endl;
  130. }
  131. break;
  132. default:
  133. ;
  134. }
  135. i++;
  136. }
  137. }
  138.  
  139.  
6  Programación / Programación C/C++ / AYUDA ORDENAR LISTA SIMPLE[C] en: 23 Abril 2011, 04:22 am
Hola gente del foro!! Estoy teniendo un problema para ordenar una lista simple, se q directamente podria insertar los nodos en forma ordenada, pero lo q quiero es ordenarlo al final para hacer un algoritmo q me ordene la lista.. El problema es q no se pq no me lo ordena correctamente.. No puedo encontrar el error.. usando un puntero auxiliar quiero extraer numeros de la lista e ir insertandolos ordenadamente en otra.. Espero q puedan ayudarme pq estoy volviendome loco..
aqui el codigo..

Código
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct nodo{
  5.    char data;
  6.    struct nodo *nextPtr;
  7. };
  8.  
  9. typedef struct nodo NODO;
  10. typedef NODO *NODOPTR;
  11.  
  12. void insertNodo(NODOPTR *, char);
  13. char deleteNodo(NODOPTR *, char);
  14. int listLength(NODOPTR);
  15. void ordenarAscendente(NODOPTR *, NODOPTR *);
  16. void printLista(NODOPTR);
  17.  
  18. main()
  19. {
  20.    NODOPTR startPtr = NULL, startPtr2 = NULL;
  21.    char value;
  22.    int choice;
  23.  
  24.    printf("Ingrese una opcion: ");
  25.    scanf("%d", &choice);
  26.  
  27.    while (choice != 4){
  28.        switch (choice){
  29.            case 1:
  30.                printf("Ingrese una letra: ");
  31.                scanf("%s", &value);
  32.                insertNodo(&startPtr, value);
  33.                printLista(startPtr);
  34.                break;
  35.            case 2:
  36.                printf("Ingrese una letra para borrar: ");
  37.                scanf("%s", &value);
  38.                deleteNodo(&startPtr, value);
  39.                printLista(startPtr);
  40.                break;
  41.            case 3:
  42.                if(listLength(startPtr) == NULL)
  43.                    printf("La lista no contiene elementos\n");
  44.                else
  45.                    printf("La lista contiene %d elementos\n", listLength(startPtr));
  46.                break;
  47.            default:
  48.                printf("Opcion inexistente");
  49.        }
  50.        printf("Ingrese una opcion: ");
  51.        scanf("%d", &choice);
  52.    }
  53.  
  54.    ordenarAscendente(&startPtr, &startPtr2);
  55.    printLista(startPtr2);
  56.  
  57.    return 0;
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. void insertNodo(NODOPTR *sPtr, char val)
  65. {
  66.    NODOPTR newPtr, previousPtr, currentPtr;
  67.  
  68.    newPtr = (NODO*)malloc(sizeof(NODO));
  69.  
  70.    if (newPtr != NULL){
  71.        newPtr -> data = val;
  72.        newPtr -> nextPtr = NULL;
  73.  
  74.        previousPtr = NULL;
  75.        currentPtr = *sPtr;
  76.  
  77.        while (currentPtr != NULL){
  78.            previousPtr = currentPtr;
  79.            currentPtr = currentPtr -> nextPtr;
  80.        }
  81.  
  82.        if (previousPtr == NULL){
  83.            newPtr -> nextPtr = *sPtr;
  84.            *sPtr = newPtr;
  85.        }
  86.  
  87.        else{
  88.            previousPtr -> nextPtr = newPtr;
  89.            newPtr -> nextPtr = currentPtr;
  90.        }
  91.    }
  92.    else
  93.        printf("%s not inserted. Insuficient space", val);
  94. }
  95.  
  96. char deleteNodo(NODOPTR *sPtr, char val)
  97. {
  98.    NODOPTR previousPtr, currentPtr, tempPtr;
  99.  
  100.    if (val == (*sPtr) -> data){
  101.        tempPtr = *sPtr;
  102.        *sPtr = (*sPtr) -> nextPtr;
  103.        free(tempPtr);
  104.        return val;
  105.    }
  106.    else{
  107.        previousPtr = *sPtr;
  108.        currentPtr = (*sPtr) -> nextPtr;
  109.  
  110.        while(currentPtr != NULL && currentPtr -> data != val){
  111.            previousPtr = currentPtr;
  112.            currentPtr = currentPtr -> nextPtr;
  113.        }
  114.        if(currentPtr != NULL){
  115.            tempPtr = currentPtr;
  116.            previousPtr -> nextPtr = currentPtr -> nextPtr;
  117.            free(tempPtr);
  118.            return val;
  119.        }
  120.    }
  121.  
  122.    return '\0';
  123. }
  124.  
  125. int listLength(NODOPTR currentPtr)
  126. {
  127.    int cont = 0;
  128.  
  129.    if (currentPtr == NULL)
  130.        return NULL;
  131.    else{
  132.        while (currentPtr != NULL){
  133.            cont++;
  134.            currentPtr = currentPtr -> nextPtr;
  135.        }
  136.    }
  137.  
  138.    return cont;
  139. }
  140.  
  141. void ordenarAscendente(NODOPTR *sPtr, NODOPTR *sPtr2)
  142. {
  143.    NODOPTR auxPtr, previousPtr, currentPtr, tempPtr;
  144.  
  145.    //auxPtr = *sPtr;
  146.  
  147.    if (*sPtr == NULL)
  148.        printf("No hay elementos en la lista\n");
  149.    else{
  150.        while (*sPtr != NULL){
  151.            tempPtr = *sPtr;
  152.  
  153.            previousPtr = NULL;
  154.            currentPtr = *sPtr2;
  155.  
  156.            while (currentPtr != NULL && tempPtr -> data > currentPtr -> data){
  157.                previousPtr = currentPtr;
  158.                currentPtr = currentPtr -> nextPtr;
  159.                }
  160.  
  161.            if (previousPtr == NULL){
  162.                tempPtr -> nextPtr = *sPtr2;
  163.                *sPtr2 = tempPtr;
  164.            }
  165.            else{
  166.                previousPtr -> nextPtr = tempPtr;
  167.                tempPtr -> nextPtr = currentPtr;
  168.            }
  169.  
  170.            free(tempPtr);
  171.            *sPtr = (*sPtr) -> nextPtr;
  172.        }
  173.    }
  174. }
  175.  
  176. void printLista(NODOPTR currentPtr)
  177. {
  178.    if (currentPtr == NULL)
  179.        printf("La lista esta vacia");
  180.    else{
  181.        while (currentPtr != NULL){
  182.            printf("|%c|->", currentPtr -> data);
  183.            currentPtr = currentPtr -> nextPtr;
  184.        }
  185.    }
  186.    printf("NULL\n");
  187. }
  188.  

Saludos!!
7  Programación / Programación C/C++ / Simulacion la tortuga y la liebre[C] en: 22 Marzo 2011, 05:21 am
Hola gente del foro!! Comparto un programita q hice q simula, como dice el titulo, la famosa carrera entre la tortuga y la liebre.. lo q represento en los vectores correspondientes a cada animal, son las acciones de los mismos en la carrera, q pueden ser avanzar, retroceder o quedarse dormido segun el num aleatorio q salga.. cuando ambos competidores compartan el mismo cuadro la tortuga mordera a la liebre.. y ovbiamente ganara el q 1ro llegue a la meta..
Bueno espero comentarios y sugerencias!!
Saludos!!

P.D: el codigo lo hice en linux por lo tanto el system("clear") tira error si no se ejecuta en ese OS

Código
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #define SIZE 71
  5.  
  6. int printRace(int [][SIZE], int, int, int);
  7.  
  8. main()
  9. {
  10.    srand(time(NULL));
  11.    int row, column, raceTrack[3][SIZE];
  12.    int tortoise = 1, hare = 1, finish;
  13.  
  14.    printf("BANG!!%63s%ds\nAND THEY 'RE OFF!!\n\n", "Time: ", 0);
  15.    finish = printRace(raceTrack, tortoise, hare, SIZE);
  16.  
  17.    if (finish == 1)
  18.        printf("\nTORTOISE WINS\n");
  19.    else if (finish == 2)
  20.        printf("\nHare wins\n");
  21.    else
  22.        printf("\nIt's a tie\n");
  23.  
  24.  
  25.    return 0;
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32. int printRace(int rt[][SIZE], int t, int h, int size)
  33. {
  34.    void wait(int);
  35.  
  36.    int tortoiseArray[10] = {3, 3, 3, 3, 3, -6, -6, 1, 1, 1};
  37.    int hareArray[10] = {0, 0, 9, 9, -12, 1, 1, 1, -2, -2};
  38.    int timer = 1, row, column;
  39.  
  40.    while (t < size - 1 && h < size - 1){
  41.        for (row = 1; row < 3; row++){
  42.            for (column = 1; column < size; column++){
  43.                if (t != h || t == 1){
  44.                    if (row == 1 && column == t)
  45.                        printf("%c", 'T');
  46.                    else if (row == 2 && column == h)
  47.                        printf("%c", 'H');
  48.                    else
  49.                        printf("_");
  50.                }
  51.                else{
  52.                    if (row == 1 && column == t)
  53.                        printf("%c", 'T');
  54.                    else if (row == 2 && column == h)
  55.                        printf("%s", "OUCH!!!");
  56.                    else
  57.                        printf("_");
  58.                }
  59.            }
  60.            printf("\n");
  61.        }
  62.        t += tortoiseArray[rand() % 10];
  63.        h += hareArray[rand() % 10];
  64.        if (t < 1)
  65.            t = 1;
  66.        if (h < 1)
  67.            h = 1;
  68.        if (t > size - 1)
  69.            t = size - 1;
  70.        if (h > size - 1)
  71.            h = size - 1;
  72.        wait(1);
  73.        printf("%69s%ds","Time: ", timer++);
  74.        system("clear");
  75.        printf("\n\n\n");
  76.    }
  77.  
  78.    for (row = 1; row < 3; row++){
  79.        for (column = 1; column < size; column++){
  80.            if (row == 1 && column == t)
  81.                printf("%c", 'T');
  82.            else if (row == 2 && column == h)
  83.                printf("%c", 'H');
  84.            else
  85.                printf("_");
  86.        }
  87.        printf("\n");
  88.    }
  89.  
  90.    if (t == size - 1 && h == size -1)
  91.        return 0;
  92.    else if (t == size -1)
  93.        return 1;
  94.    else
  95.        return 2;
  96. }
  97.  
  98. void wait(int seconds)
  99. {
  100.    clock_t endwait;
  101.    endwait = clock() + seconds * CLOCKS_PER_SEC;
  102.    while (clock() < endwait);
  103. }
  104.  
  105.  
8  Programación / Programación C/C++ / Poker[C] en: 21 Febrero 2011, 21:29 pm
Hola gente del foro!! Dejo un codigo de poker al que le puse todas las funciones para determinar el punto de la mano.. Espero comentarios!! Saludos!!

Código
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5.  
  6. void shuffle(int [][13]);
  7. void deal(int [][13], const char *[], const char *[]);
  8. void bubble(int n[], int size);
  9.  
  10. main()
  11. {
  12.    srand(time(NULL));
  13.    int i, j, desk[4][13] = {0};
  14.    const char *suit[4] = {"Corazones", "Diamantes", "Trebol", "Espadas"};
  15.    const char *faces[13] = {"As", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete",
  16.                             "Ocho", "Nueve", "Diez", "Jack", "Reina", "Rey"};
  17.  
  18.    shuffle(desk);
  19.    deal(desk, suit, faces);
  20.  
  21.    return 0;
  22. }
  23.  
  24.  
  25.  
  26.  
  27.  
  28. void shuffle(int d[][13])
  29. {
  30.    int row, column, card;
  31.  
  32.    for (card = 1; card < 53; card++){
  33.        row = rand() % 4;
  34.        column = rand() % 13;
  35.        while (d[row][column] != 0){
  36.            row = rand() % 4;
  37.            column = rand() % 13;
  38.        }
  39.        d[row][column] = card;
  40.    }
  41. }
  42.  
  43. void deal(int d[][13], const char *s[], const char *f[])
  44. {
  45.    int deck2[4][13] = {0};
  46.    int row, column, card, pnt = 0, i;
  47.    int royalFlush(const int [][13]);
  48.    int straightFlush(const int [][13]);
  49.    int poker(const int [][13]);
  50.    int fullHouse(const int [][13]);
  51.    int flush(const int [][13]);
  52.    int straight(const int [][13]);
  53.    int threeOfAKind(const int [][13]);
  54.    int twoPair(const int [][13]);
  55.    int pair(const int [][13]);
  56.    int (*pokerArray[9])(const int) = {royalFlush, straightFlush, poker, fullHouse, flush,
  57.                                       straight, threeOfAKind, twoPair, pair};
  58.    void point(int);
  59.  
  60.  
  61.    for (card = 1; card < 6; card++){
  62.        for (row = 0; row < 4; row++){
  63.            for (column = 0; column < 13; column++){
  64.                if (d[row][column] == card){
  65.                    printf("%-6s de %-8s\n", f[column], s[row]);
  66.                    deck2[row][column] = 1;
  67.                }
  68.            }
  69.        }
  70.    }
  71.  
  72.    for (i = 0; i < 9; i++){
  73.        pnt = (*pokerArray[i])(deck2);
  74.        if (pnt != 0)
  75.            break;
  76.    }
  77.    point(pnt);
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84. int royalFlush(const int d2[][13])
  85. {
  86.    int i, j, k = 0, cont = 0, card[5],
  87.        royal[5] = {0, 9, 10, 11, 12};
  88.  
  89.    if (flush(d2) == 5)
  90.        for (i = 0; i < 4; i++){
  91.            for (j = 0; j < 13; j++){
  92.                if (d2[i][j] == 1){
  93.                    card[k] = j;
  94.                    k++;
  95.                }
  96.            }
  97.        }
  98.    bubble(card, 5);
  99.  
  100.    for (i = 0; i < 5; i++)
  101.        if (card[i] == royal[i])
  102.            cont++;
  103.  
  104.    if (cont == 5)
  105.        return 1;
  106.  
  107.    return 0;
  108. }
  109.  
  110. int straightFlush(const int d2[][13])
  111. {
  112.    if (flush(d2) == 5)
  113.        if (straight(d2) == 6)
  114.            return 2;
  115.  
  116.    return 0;
  117. }
  118.  
  119. int poker(const int d2[][13])
  120. {
  121.    int i, j, incr, cont;
  122.  
  123.    for (incr = 0; incr < 13; incr++){
  124.        cont = 0;
  125.        for (i = 0; i < 4; i++){
  126.            for (j = incr; j <= incr; j++){
  127.                if (d2[i][j] == 1)
  128.                cont++;
  129.            }
  130.        }
  131.        if (cont == 4)
  132.            return 3;
  133.    }
  134.    return 0;
  135. }
  136.  
  137. int fullHouse(const int d2[][13])
  138. {
  139.    if (threeOfAKind(d2) == 7)
  140.        if (pair(d2) == 9)
  141.            return 4;
  142.  
  143.    return 0;
  144. }
  145.  
  146. int flush(const int d2[][13])
  147. {
  148.    int i, j, cont;
  149.  
  150.    for (i = 0; i < 4; i++){
  151.        cont = 0;
  152.        for (j = 0; j < 13; j++){
  153.            if (d2[i][j] == 1)
  154.                cont++;
  155.        }
  156.        if (cont == 5)
  157.            return 5;
  158.    }
  159.    return 0;
  160. }
  161.  
  162. int straight(const int d2[][13])
  163. {
  164.    int i, j, k = 0, temp, num[5];
  165.  
  166.    for (i = 0; i < 4; i++){
  167.        for (j = 0; j < 13; j++){
  168.            if (d2[i][j] == 1){
  169.                num[k] = j;
  170.                k++;
  171.            }
  172.        }
  173.    }
  174.    bubble(num, 5);
  175.    temp = num[0];
  176.    for (i = 1; i < 5; i++){
  177.        if (num[i] != temp + 1)
  178.            return 0;
  179.        temp = num[i];
  180.    }
  181.    return 6;
  182. }
  183.  
  184.  
  185. int threeOfAKind(const int d2[][13])
  186. {
  187.    int i, j, incr, cont;
  188.  
  189.    for (incr = 0; incr < 13; incr++){
  190.        cont = 0;
  191.        for (i = 0; i < 4; i++){
  192.            for (j = incr; j <= incr; j++){
  193.                if (d2[i][j] == 1)
  194.                cont++;
  195.            }
  196.        }
  197.        if (cont == 3)
  198.            return 7;
  199.    }
  200.    return 0;
  201. }
  202.  
  203. int twoPair(const int d2[][13])
  204. {
  205.    int i, j, incr, cont, pairCont = 0;
  206.  
  207.    for (incr = 0; incr < 13; incr++){
  208.        cont = 0;
  209.        for (i = 0; i < 4; i++){
  210.            for (j = incr; j <= incr; j++){
  211.                if (d2[i][j] == 1)
  212.                cont++;
  213.            }
  214.        }
  215.        if (cont == 2)
  216.            pairCont++;
  217.    }
  218.    if (pairCont == 2)
  219.        return 8;
  220.  
  221.    return 0;
  222. }
  223.  
  224. int pair(const int d2[][13])
  225. {
  226.    int i, j, incr, cont;
  227.  
  228.    for (incr = 0; incr < 13; incr++){
  229.        cont = 0;
  230.        for (i = 0; i < 4; i++){
  231.            for (j = incr; j <= incr; j++){
  232.                if (d2[i][j] == 1)
  233.                cont++;
  234.            }
  235.        }
  236.        if (cont == 2)
  237.            return 9;
  238.    }
  239.    return 0;
  240. }
  241.  
  242. void point(int p)
  243. {
  244.    switch (p){
  245.            case 1:
  246.                printf("\nROYAL FLUSH\n");
  247.                break;
  248.            case 2:
  249.                printf("\nSTRAIGHT FLUSH\n");
  250.                break;
  251.            case 3:
  252.                printf("\nPOKER\n");
  253.                break;
  254.            case 4:
  255.                printf("\nFULL HOUSE\n");
  256.                break;
  257.            case 5:
  258.                printf("\nFLUSH\n");
  259.                break;
  260.            case 6:
  261.                printf("\nSTRAIGHT\n");
  262.                break;
  263.            case 7:
  264.                printf("\nTHREE OF A KIND\n");
  265.                break;
  266.            case 8:
  267.                printf("\nTWO PAIR\n");
  268.                break;
  269.            case 9:
  270.                printf("\nPAIR\n");
  271.                break;
  272.            default:
  273.                printf("\nHIGH CARD\n");
  274.    }
  275. }
  276.  
  277. void bubble(int n[], int size)
  278. {
  279.    int i, j, temp;
  280.  
  281.    for (i = 0; i < size - 1; i++){
  282.        for (j = i + 1; j < size; j++){
  283.            if (n[i] > n[j]){
  284.                temp = n[i];
  285.                n[i] = n[j];
  286.                n[j] = temp;
  287.            }
  288.        }
  289.    }
  290. }
  291.  
  292.  
9  Programación / Programación C/C++ / Criba de Eratostenes[C] en: 29 Diciembre 2010, 19:27 pm
Hola gente! Dejo un algoritmo q devuelve numeros primos entre 1 y 1000..
Saludos!!

Código
  1. #include<stdio.h>
  2. #define SIZE 1000
  3.  
  4. void cribaDeEratostenes(int [], int , int);
  5.  
  6. main()
  7. {
  8.      int vectorPrimo[SIZE];
  9.      int i, num;
  10.  
  11.      for (i = 1; i <= SIZE; i++)
  12.          vectorPrimo[i] = 1;
  13.  
  14.      for (i = 2; i <= SIZE; i++)
  15.          if (vectorPrimo[i] == 1){
  16.             cribaDeEratostenes(vectorPrimo, i, SIZE);
  17.             printf("%-5d", i);
  18.          }
  19.  
  20.  
  21.      return 0;
  22. }
  23.  
  24.  
  25.  
  26.  
  27.  
  28. void cribaDeEratostenes(int v[], int n, int size)
  29. {
  30.     int i = n;
  31.  
  32.     for (i += n; i <= size; i += n)
  33.         v[i] = 0;
  34. }
  35.  
10  Programación / Programación C/C++ / Generar numeros aleatorios sin repeticion[C] en: 27 Diciembre 2010, 18:09 pm
Hola foreros!! Dejo un programa q genera numeros aleatorios y los almacena en un vector solo si no estan repetidos..
Saludos!!

Código
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #define SIZE 20
  5.  
  6. void generarRandom(int [], int);
  7. void ordenarVector(int [], int);
  8. void mostrarVector(int [], int);
  9.  
  10. main()
  11. {
  12.      srand(time(NULL));
  13.      int vector[SIZE] = {0};
  14.  
  15.      generarRandom(vector, SIZE);
  16.      ordenarVector(vector, SIZE);
  17.      mostrarVector(vector, SIZE);
  18.  
  19.  
  20.      return 0;
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27. void generarRandom(int v[], int size)
  28. {
  29.     int i, j, num, dupl;
  30.  
  31.     printf("RANDOM\n");
  32.     for (i = 0; i < size; i++){
  33.         num = 1 + rand() % size;
  34.         dupl = 0;
  35.         printf("%-4d", num);
  36.         for (j = 0; j <= i; j++){
  37.             if (num == v[j]){
  38.                dupl = 1;
  39.                break;
  40.             }
  41.         }
  42.         if (dupl == 1)
  43.            i--;
  44.         else
  45.            v[i] = num;
  46.     }
  47. }
  48.  
  49. void ordenarVector(int v[], int size)
  50. {
  51.     int i, j, temp;
  52.  
  53.     for (i = 0; i < size - 1; i++){
  54.         for (j = i+1; j < size; j++){
  55.             if (v[i] > v[j]){
  56.                   temp = v[i];
  57.                   v[i] = v[j];
  58.                   v[j] = temp;
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. void mostrarVector(int v[], int size)
  65. {
  66.     int i;
  67.  
  68.     printf("\n\nVECTOR\n");
  69.     for (i = 0; i < size; i++)
  70.         printf("%d\n", v[i]);
  71. }
  72.  
Páginas: [1] 2
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines