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 Mensajes
Páginas: [1] 2
1  Programación / Programación C/C++ / Re: Separa char para usar en otra variable en: 22 Marzo 2017, 00:38 am
Un ejemplo con tus datos:
Código
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. char nombreEdad[] = "Luis 20";
  5.    char nombre[20];
  6.    int edad;
  7.  
  8.    sscanf(nombreEdad, "%s %i", nombre, &edad);
  9.  
  10.    printf("Nombre: %s\n", nombre);
  11.    printf("Edad: %i\n", edad);
  12. }

Gracias por su ayuda me sirvió de mucho, pero se me presento otro problema no puedo leer numeros con 10 digitos, me lee perfecto hasta 9 numeros pero con 10 me sale un numero diferente al que leo, como podría solucionar esto ? gracias

Código
  1. char datos[] = "1 2304872458";
  2.  
  3. int code;
  4. unsigned long code1;
  5.  
  6. sscanf(datos, "%i %i", &code, &code1);
  7.  
  8. printf("%i ",code);
  9. printf("%i ",code1);
  10.  

//Solución %u, Gracias por la ayuda.
2  Programación / Programación C/C++ / Re: Separa char para usar en otra variable en: 19 Marzo 2017, 01:40 am
Deberías conocer la biblioteca estándar. Así verías que muchas de las cosas ya están hechas.

sscanf: http://c.conclase.net/librerias/?ansifun=sscanf

Citar
Si puedes hacer un ejemplo con lo que yo publique te lo agradecería
3  Programación / Programación C/C++ / Re: Separa char para usar en otra variable en: 19 Marzo 2017, 00:32 am
Si sabes que la cadena a separar siempre tiene el mismo formato puedes usar sscanf.

Citar
No entiendo lo que me quieres decir.
scanf ó cin son para pedir datos a los usuarios pero yo no necesito pedirselos ya que ya los tengo solo deseo separarlos y luego usarlos en variables diferentes.
4  Programación / Programación C/C++ / Separa char para usar en otra variable en: 19 Marzo 2017, 00:18 am
// Estoy separando la variable (char) nombreEdad y funciona bien
// El resultado por pantalla es :
// Luis
// 20
// Me gustaría saber como podría poner el nombre Luis en una variable char diferente, y el numero 20 en una variable de tipo int, espero me entiendan y puedan ayudar gracias.

Código
  1. char nombreEdad[] = "Luis 20";
  2. char* a;
  3.  
  4. a = strtok(nombre, " ");
  5. while(a != NULL) {
  6.  
  7.  
  8. cout << a << endl;
  9. a = strtok(NULL, " ");
  10.  
  11. }
  12.  
5  Programación / Programación C/C++ / ir chequeando numeros en una matriz en: 29 Abril 2016, 01:21 am
Tengo un problema con esto espero puedan ayudarme, si to tengo una matriz

116456
115456
234566
789123
Ejemplo si yo estoy en la casilla (0,0) & al parecer en este momento soy el numero "1" el cual esta rodeado por los numeros "2 , 3 , 4 , 5 , 6" Lo que yo quiero hacer es que cuando le de al numero EJEMPLO "6" mi matriz quede así, osea transforme todos los numeros que llevaba en el numero que voy a presionar.

666456
665456
234566
789123

& si le vuelvo a dar al numero 4 mi matriz quedaría así.

444456
445456
234566
789123 & si vuelvo a presionar el 5 quedaría así

555556
555556
234566
789123 Bueno así hasta llenar la matriz con 1 solo numero, el problema es que en mi código cuando yo presiono el numero cambia todos los números, no solo los que tengo alrededor.

Los números que llevo deberían transformarse en el nuevo que acabo de presionar. espero su ayuda gracias.

Código
  1. [HIGHLIGHT="C"]
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5.  
  6. #define filas 10
  7. #define columnas 10
  8. #define fico 10
  9.  
  10. void cargar_matriz(int matriz[][columnas]);
  11. void pintar_matriz(int matriz[][columnas]);
  12. void movimiento_matriz(int matriz[][columnas]);
  13. void jugar(int matriz[][columnas]);
  14.  
  15. int i,j;
  16.  
  17. int main() {
  18.  
  19. int matriz[filas][columnas];
  20.  
  21. jugar(matriz);
  22.  
  23. }
  24.  
  25. void cargar_matriz(int matriz[][columnas]) {
  26.  
  27. for(i = 0; i < filas; i++) {
  28. for(j = 0; j < columnas; j++) {
  29. matriz[i][j] = rand()%6+1;
  30. }
  31. }
  32. return;
  33. }
  34.  
  35. void pintar_matriz(int matriz[][columnas]) {
  36.  
  37. for(i = 0; i < filas; i++) {
  38. printf("\n\t\t\t");
  39. for(j = 0; j < columnas; j++) {
  40. printf("%d",matriz[i][j]);
  41. }
  42. }
  43. printf("\n\n");
  44. SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),7);
  45. printf("\t1 \t2 \t3 \t4 \t5 \t6 \n");
  46. SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),1);
  47. printf("\t%c",219);
  48. SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),2);
  49. printf("\t%c",219);
  50. SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),3);
  51. printf("\t%c",219);
  52. SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),4);
  53. printf("\t%c",219);
  54. SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),5);
  55. printf("\t%c",219);
  56. SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),6);
  57. printf("\t%c",219);
  58.  
  59.  
  60. movimiento_matriz(matriz);
  61. }
  62.  
  63. void movimiento_matriz(int matriz[][columnas]){
  64.  
  65. int mycolor;
  66.  
  67. mycolor = matriz[0][0];
  68. matriz[0][0] = 10;
  69.  
  70. char tecla;
  71.  
  72. tecla = getch();
  73. //matriz[0][0] = 10;
  74.  
  75. switch (tecla) {
  76.  
  77. case '1' : {
  78. for(i = 0; i < filas; i++) {
  79. for(j = 0; j < columnas; j++) {
  80. if(matriz[i][j] == mycolor ){
  81. matriz[i][j] = 10;
  82. }
  83. }
  84. }
  85. for(i = 0; i < fico; i++) {
  86. for (j = 0; j < fico; j++){
  87. if(matriz[i][j] == 10) {
  88. matriz[i][j] = 1;
  89. }
  90. }
  91. }
  92.  
  93. break;
  94. }
  95.  
  96. case '2' : {
  97. for(i = 0; i < filas; i++) {
  98. for(j = 0; j < columnas; j++) {
  99. if(matriz[i][j] == mycolor ) {
  100. matriz[i][j] = 10;
  101. }
  102. }
  103.  
  104. }
  105. for(i = 0; i < fico; i++) {
  106. for (j = 0; j < fico; j++){
  107. if(matriz[i][j] == 10) {
  108. matriz[i][j] = 2;
  109. }
  110. }
  111. }
  112. break;
  113. }
  114.  
  115. case '3' : {
  116. for(i = 0; i < filas; i++) {
  117. for(j = 0; j < columnas; j++) {
  118. if(matriz[i][j] == mycolor ) {
  119. matriz[i][j] = 10;
  120. }
  121. }
  122. }
  123.  
  124. for(i = 0; i < fico; i++) {
  125. for (j = 0; j < fico; j++){
  126. if(matriz[i][j] == 10) {
  127. matriz[i][j] = 3;
  128. }
  129. }
  130. }
  131. break;
  132. }
  133.  
  134. case '4' : {
  135. for(i = 0; i < filas; i++) {
  136. for(j = 0; j < columnas; j++) {
  137. if(matriz[i][j] == mycolor ) {
  138. matriz[i][j] = 10;
  139. }
  140. }
  141. }
  142. for(i = 0; i < fico; i++) {
  143. for (j = 0; j < fico; j++){
  144. if(matriz[i][j] == 10) {
  145. matriz[i][j] = 4;
  146. }
  147. }
  148. }
  149. break;
  150. }
  151.  
  152. case '5' : {
  153. for(i = 0; i < filas; i++) {
  154. for(j = 0; j < columnas; j++) {
  155. if(matriz[i][j] == mycolor ) {
  156. matriz[i][j] = 10;
  157. }
  158. }
  159. }
  160. for(i = 0; i < fico; i++) {
  161. for (j = 0; j < fico; j++){
  162. if(matriz[i][j] == 10) {
  163. matriz[i][j] = 5;
  164. }
  165. }
  166. }
  167. break;
  168. }
  169.  
  170. case '6' : {
  171. for(i = 0; i < filas; i++) {
  172. for(j = 0; j < columnas; j++) {
  173. if(matriz[i][j]== mycolor ) {
  174. matriz[i][j] = 10;
  175. }
  176. }
  177. }
  178. for(i = 0; i < fico; i++) {
  179. for (j = 0; j < fico; j++){
  180. if(matriz[i][j] == 10) {
  181. matriz[i][j] = 6;
  182. }
  183. }
  184. }
  185. break;
  186. }
  187.  
  188. default : {
  189.  
  190. printf("\nError\n");
  191. break;
  192. }
  193. }
  194.  
  195. system("cls");
  196. pintar_matriz(matriz);
  197.  
  198. }
  199.  
  200. void jugar(int matriz[][columnas]) {
  201.  
  202. cargar_matriz(matriz);
  203. pintar_matriz(matriz);
  204. movimiento_matriz(matriz);
  205.  
  206. }
  207.  
6  Programación / Programación C/C++ / Re: Ordenar una matriz sin repetir numeros. en: 24 Abril 2016, 22:27 pm
De entrada se me plantea una duda:
Si la matriz ordenada no puede repetir ningún número puede que sea de menor tamaño que la matriz original. Siendo así:
¿La matriz ordenada debe ser la misma matriz original? Si la matriz original es estática, eso es definida del tipo T[dimY][dimX], no se puede reducir su tamaño. ¿Cómo se marcan las casillas vacías? ¿Cómo se distribuye la información en la matriz?
Si la matriz original es dinámica se pueden reducir sus dimensiones pero de nuevo aparece el problema de como se distribuye el sistema si no se pueden cuadrar las dimensiones (número impar de elementos).

Entonces cuando cargo los números es que debería revisar si están repetido?
7  Programación / Programación C/C++ / Re: Ordenar una matriz sin repetir numeros. en: 24 Abril 2016, 20:54 pm
Un ejemplo como podría hacer para que no se repitieran los números debería agregar una función o agregarlo a una de las que ya tengo.

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define filas 5
  5. #define columnas 5
  6.  
  7. void cargar_matriz(int matriz[filas][columnas]);
  8. void ordenar_matriz(int matriz[filas][columnas]);
  9. void imprimir_matriz(int matriz[filas][columnas]);
  10.  
  11. int i,j,k,l;
  12.  
  13.    int main()  {
  14.  
  15.     int matriz[filas][columnas];
  16.  
  17.     cargar_matriz(matriz);
  18.     ordenar_matriz(matriz);
  19.     imprimir_matriz(matriz);
  20.  
  21.    return 0;  
  22.    }//Fin main
  23.  
  24.  
  25.    void cargar_matriz(int matriz[filas][columnas]){
  26.  
  27.     for(i = 0; i < filas; i++) {
  28.  
  29.         for(j = 0; j < columnas; j++) {
  30.         matriz[i][j] = rand()%25+1;
  31. }
  32. }
  33.  
  34. }//Fin función
  35.  
  36.  
  37. void ordenar_matriz(int matriz[filas][columnas]){
  38.  
  39. int mayor;
  40.  
  41. for(i = 0; i < filas;i++)   {
  42.  
  43.            for(j = 0; j < columnas;j++){
  44.  
  45.             for(k = 0; k < 5;k ++) {
  46.  
  47.             for(l = 0; l < 5; l++) {
  48.  
  49.             if(matriz[i][j] < matriz[k][l]) {
  50.  
  51.             mayor = matriz[i][j];
  52.             matriz[i][j] = matriz[k][l];
  53.             matriz[k][l] = mayor;
  54.  
  55. }
  56. }
  57. }
  58.  
  59.            }
  60.        }
  61. }//Fin Función.
  62.  
  63. void imprimir_matriz(int matriz[filas][columnas]){
  64.  
  65. for(i = 0; i < filas; i++) {
  66.         printf("\n");
  67.         for(j = 0; j < columnas; j++) {
  68.         printf(" %d ",matriz[i][j]);
  69. }
  70. }
  71. }//Fin función
  72.  
8  Programación / Programación C/C++ / Ordenar una matriz sin repetir numeros. en: 24 Abril 2016, 00:56 am
Hola a todos, una pregunta como podría ordenar esta matriz de números aleatorios & hacer que no se repitieran ? Gracias por cierto me recomiendan algun libro de C para leer o algo gracias.

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define filas 5
  5. #define columnas 5
  6.  
  7. int i,j;
  8.  
  9. int main() {
  10.  
  11. srand(time(NULL));
  12. int num;
  13. int matriz[filas][columnas];
  14. //num = rand()%1+9;
  15.  
  16. for(i = 0; i < filas;i++) {
  17. printf("\n");
  18. for(j = 0; j < columnas;j++){
  19. matriz[i][j] = rand()%9+1;
  20. printf("%d ",matriz[i][j]);
  21. }
  22. }
  23.  
  24. return 0;
  25. }
  26.  
9  Programación / Programación C/C++ / separar vectores & sumarlos? en: 16 Marzo 2016, 23:18 pm
Hola amigos en este momento se me esta presentando una complicación para realizar este código, mi problema es el siguiente debería hacer esto de la imagen:http://prnt.sc/ag69x6

Ya separe los números en vectores tengo su resultado pero no se como podria hacer para realizar las operaciones que salen en la foto seria cortar el vector en 2 como & efectuar las diagonales a este proceso se le llama la multiplicación arabe, si tienen idea de como podria hacer se los agradecería.

Llevo esto de código.

Código
  1. #include <stdio.h>
  2.  
  3. int main(){
  4.  
  5. int num,num2;
  6. int vector[20];
  7. int a,b,c,d,e,f,g,h;
  8.  
  9. printf("Ingrese el primero numero : ");
  10. scanf("%d",&num);
  11.  
  12. if (num >= 0000){
  13. if (num <= 9999){
  14. a = num / 1000;
  15. b = (num % 1000) / 100;
  16. c = (num % 1000) % 100 / 10;
  17. d = ((num % 1000) % 100) % 10 / 1;
  18. }
  19. }
  20.  
  21. printf("Ingrese el segundo numero : ");
  22. scanf("%d",&num2);
  23.  
  24. if (num2 >= 0000){
  25. if (num2 <= 9999){
  26. e = num2 / 1000;
  27. f = (num2 % 1000) / 100;
  28. g = (num2 % 1000) % 100 / 10;
  29. h = ((num2 % 1000) % 100) % 10 / 1;
  30. }
  31. }
  32.  
  33. //printf("Usted esta multiplicando %d * %d\n",num,num2);
  34.  
  35. vector[0] = d*e; vector[1] = c*e; vector[2] = b*e; vector[3] = a*e;
  36. vector[4] = d*f; vector[5] = c*f; vector[6] = b*f; vector[7] = a*f;
  37. vector[8] = d*g; vector[9] = c*g; vector[10] = b*g; vector[11] = a*g;
  38. vector[12] = d*h; vector[13] = c*h; vector[14] = b*h; vector[15] = a*h;
  39.  
  40. printf("El resultado es : %d %d %d %d\n",vector[0], vector[4], vector[8], vector[12]);
  41. printf("El resultado es : %d %d %d %d\n",vector[1], vector[5], vector[9], vector[13]);
  42. printf("El resultado es : %d %d %d %d\n",vector[2], vector[6], vector[10], vector[14]);
  43. printf("El resultado es : %d %d %d %d\n",vector[3], vector[7], vector[11], vector[15]);
  44.  
  45.  
  46.  
  47. return 0;
  48.  
  49. }
  50.  
10  Programación / Programación C/C++ / Mostrar Ganar tocar un número en la matriz. en: 25 Febrero 2016, 21:04 pm
Hola amigos, una pregunta como haria para que cuando toque el numero "3" salga un mensaje en pantalla Haz ganado, de ante mano gracias.

Código
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. #define maxfilas 10
  8. #define minfilas 3
  9. #define maxcolum 10
  10. #define mincolum 3
  11.  
  12. int main(){
  13.  
  14. int filas,columnas,i ,j,a,num,num2;
  15. int matriz[400][400];
  16. srand(time(NULL));
  17.  
  18. printf("Bienvenido.\n");
  19.  
  20. do {
  21. fflush(stdin);
  22. printf("Ingrese el numero de filas que tendra el juego\n");
  23. a = isdigit(filas);
  24. scanf("%d",&filas);
  25. }
  26.  
  27.  
  28. while(filas > maxfilas || filas < minfilas);
  29.  
  30. do {
  31. fflush(stdin);
  32. printf("Ingrese el numero de columnas para el juego\n");
  33. a = isdigit(columnas);
  34. scanf("%d",&columnas);
  35. }
  36. while(columnas > maxcolum || columnas < mincolum);
  37.  
  38.  
  39.  
  40. //Cargamos la matriz
  41. for(i = 0; i < filas; i++) {
  42. for(j = 0; j < columnas; j++) {
  43. matriz[i][j]=0;
  44. matriz[num][num2]=8;
  45.     matriz[filas][columnas]=3;
  46. }
  47. }
  48. imprimir(filas,columnas,matriz);
  49. movimiento(filas,columnas,matriz);
  50.  
  51.  
  52. return 0;
  53. }
  54.  
  55. void imprimir(int filas,int columnas,int matriz[maxfilas][maxcolum]) {
  56.  
  57. system("cls");
  58. //int matriz[filas][columnas];
  59. int num,num2,i,j;
  60.  
  61. //Imprimiendo la matriz
  62. for(i = 0; i < filas; i++) {
  63. printf("\n");
  64. for(j = 0; j < columnas; j ++) {
  65. if(matriz[i][j]==0) {
  66. printf("0",matriz[i][j]);
  67. }
  68. if(matriz[i][j]==8) {
  69. printf("\1",matriz[i][j]);
  70. }
  71. if(matriz[i][j]==3) {
  72. printf("3",matriz[i][j]);
  73. }
  74. }
  75. }
  76.  
  77.  
  78.  
  79.  
  80. }// cierre funcion imprimir.
  81.  
  82. void movimiento(int filas,int columnas,int matriz[maxfilas][maxcolum]) {
  83.  
  84.  
  85. int num,num2;
  86. char tecla;
  87.  
  88. //int matriz[filas][columnas];
  89. num = rand()%2+1;
  90. num2 = rand()%2+1;
  91. matriz[num][num2]=8;
  92. matriz[filas-1][columnas-1]=3;
  93.  
  94. do {
  95.  
  96. tecla = getch();
  97.  
  98.  
  99.  
  100. matriz[filas][columnas]=0;
  101. matriz[num][num2]=0;
  102.  
  103. switch (tecla) {
  104.  
  105.  
  106. case 'w': {
  107. if (matriz[num-1][num2]==0) {
  108. num--;
  109. if(matriz[num][num2]==3){
  110. break;
  111. }
  112. }
  113. break;
  114. }
  115. case 's': {
  116. if (matriz[num+1][num2]==0) {
  117. num++;
  118. if(matriz[num][num2]==3){
  119. break;
  120. }
  121. }
  122. break;
  123. }
  124. case 'a': {
  125. if (matriz[num][num2-1]==0) {
  126. num2--;
  127. if(matriz[num][num2]==3){
  128. break;
  129. }
  130. }
  131. break;
  132. }
  133. case 'd': {
  134. if (matriz[num][num2+1]==0){
  135. num2++;
  136. if(matriz[num][num2]==3){
  137. break;
  138. }
  139. }
  140. break;
  141. }
  142.  
  143. }//cierre switch
  144. system("cls");
  145. matriz[num][num2]=8;
  146. matriz[filas][columnas]=0;
  147. matriz[filas][columnas]=3;
  148.  
  149. imprimir(filas,columnas,matriz);
  150.  
  151.  
  152. }while(tecla != 'x');
  153.  
  154. }
  155.  
  156.  
Páginas: [1] 2
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines