Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: luiszoket en 19 Octubre 2017, 05:23 am



Título: Estructuras Dinamicas
Publicado por: luiszoket en 19 Octubre 2017, 05:23 am
¿Cómo puedo hacer, este código a Memoria Dinámica?

Código
  1. //********************************************************************/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. //********************************************************************//
  8.  
  9. //********************************************************************//
  10. #define TRUE 1
  11. #define FALSE 0
  12. #define ENTER 13
  13. #define ESC 27
  14. #define ESPACIO 32
  15. #define A 65
  16. #define Z 90
  17. #define N 5000
  18. //********************************************************************//
  19.  
  20. //********************************************************************//
  21. typedef struct nombre{
  22. char nombre[30];
  23. char appat[30];
  24. char apmat[30];
  25. }Tnombre;
  26.  
  27. typedef struct alumnos{
  28. Tnombre NombC;
  29. long matri;
  30. int sexo;
  31. }Talum;
  32.  
  33. //********************************************************************//
  34. void menu(void);
  35. Talum LlenarAut(Talum reg, long vect[], int pos);
  36. void ImprimirVect(Talum vect[], int tam);
  37. void Imprimir(Talum reg[], int pos);
  38. int BusqSec(Talum vect[], int tam, long matri);
  39. int BusquedaBin(Talum vect[], int tam, long matri);
  40. void OrdenacionShell(Talum vect[], int tam);
  41. void LlenarVect(long vect[], int tam, long ri, long rf);
  42. int Buscar(long vect[], int tam, long matri);
  43.  
  44.  
  45. int main(void){
  46. menu();
  47. srand(time(NULL));
  48. return 0;
  49. }
  50.  
  51. //********************************************************************//
  52.  
  53. void menu(void)
  54. {
  55. int op, i, j=0, x, pos=0, lleno=FALSE, orde=FALSE;
  56. long mat[N], matri;
  57. Talum reg, temp, vect[N];
  58.  
  59. LlenarVect(mat, N, 300000, 399999);
  60.  
  61. do{
  62. system("cls");
  63. printf("\n1.- Llenar Registros");
  64. printf("\n2.- Buscar");
  65. printf("\n3.- Ordenar");
  66. printf("\n4.- Imprimir");
  67. printf("\n5.- Salir");
  68. printf("\nSelecciona una opcion: ");
  69. scanf("%d",&op);
  70. switch(op)
  71. {
  72. case 1:
  73. system("cls");
  74. if(j<N)
  75. {
  76. system("cls");
  77. lleno=TRUE;
  78. if(j<400)
  79. {
  80. for(i=0; i<4000; i++)
  81. {
  82. temp=LlenarAut(reg, mat, pos);
  83. vect[j]=temp;
  84. j++;
  85. pos++;
  86. }
  87. printf("Los primeros %d registros han sido generados\n",i);
  88. system("pause");
  89. }
  90. else
  91. {
  92. system("cls");
  93. for(i=0; i<10; i++)
  94. {
  95. temp=LlenarAut(reg, mat, pos);
  96. vect[j]=temp;
  97. j++;
  98. pos++;
  99. }
  100. printf("%d registros mas agregados\n",i);
  101. orde=FALSE;
  102. system("pause");
  103. }
  104. }
  105. else
  106. {
  107. printf("Vector Lleno\n");
  108. system("pause");
  109. }
  110. break;
  111. case 2:
  112. system("cls");
  113. if(lleno==TRUE)
  114. {
  115. if(orde==TRUE)
  116. {
  117. system("cls");
  118. printf("Metodo Binario\n");
  119. printf("Digita la matricula: ");
  120. scanf("%ld",&matri);
  121. x=BusquedaBin(vect, j, matri);
  122. if(x!=-1)
  123. {
  124. Imprimir(vect, x);
  125. }
  126. else
  127. {
  128. system("cls");
  129. printf("Matricula no encontrada\n");
  130. system("pause");
  131. }
  132. }
  133. else
  134. {
  135. system("cls");
  136. printf("Metodo Secuencial\n");
  137. printf("Digita la matricula: ");
  138. scanf("%ld",&matri);
  139. x=BusqSec(vect, j, matri);
  140. if(x!=-1)
  141. {
  142. Imprimir(vect, x);
  143. }
  144. else
  145. {
  146. system("cls");
  147. printf("Matricula no encontrada\n");
  148. system("pause");
  149. }
  150. }
  151. }
  152. else
  153. {
  154. system("cls");
  155. printf("Vector Vacio\n");
  156. system("pause");
  157. }
  158. // system("pause");
  159. break;
  160. case 3:
  161. system("cls");
  162. if(lleno==TRUE)
  163. {
  164. if(orde==FALSE)
  165. {
  166. OrdenacionShell(vect, j);
  167. orde=TRUE;
  168. printf("Datos Ordenados\n");
  169. }
  170. else
  171. {
  172. system("cls");
  173. printf("El vector ya esta ordenado\n");
  174. }
  175. }
  176. else
  177. {
  178. system("cls");
  179. printf("Vector Vacio\n");
  180. }
  181.  
  182. system("pause");
  183. break;
  184. case 4:
  185. ImprimirVect(vect, j);
  186. break;
  187. }
  188. }while(op!=5);
  189. }
  190.  
  191. Talum LlenarAut(Talum reg, long vect[], int pos)
  192. {
  193. // Talum reg;
  194. int x=0;
  195.  
  196. char NombreH[40][40]={"LUIS","ALBERTO","HUGO","PEDRO","JUAN","JOSE","GOKU","GOHAN","MIGUEL","ANGEL",
  197.  "DIEGO","RAUL","ROBERTO","MARIO","RAFAEL","JAVIER","CARLOS","ARTURO","EMILIANO","ENRIQUE",
  198.  "FRANCISCO","RONALDO","PAUL","OSVALDO","SANTIAGO","THIAGO","ESTEBAN","NOEL","LEONEL","MARTIN",
  199.  "SAMUEL", "LUCAS", "MARCOS", "XAVI","ALONSO","RICARDO", "OMAR", "RENATO", "JONATHAN", "RUBEN"};
  200.  
  201. char NombreM[40][40]={"EMILY","JASMIN","LUCERO","NICOL","MARIA","CARMEN","ROCIO","CASANDRA","YESICA","VERONICA",
  202.  "PAOLA","KAREN","ALEJANDRA","ALEXANDRA","ENID","EDITH","CARLA","KARLA","FABIOLA","YOHANA",
  203.  "MONICA","JENIFER","LUCIA","KIMBERLY","AMANDA","PAULA","PERLA","MIA","STHEPHANIE","ALONDRA",
  204.  "CLEOPATRA","ANA","CLARA","DIANA","FRIDA","HELENA","JENA","LUCIA","NADIA","SILBIA"};
  205.  
  206. char Apellidos[40][40]={"PEREZ","LOPEZ","SANCHEZ","FERNANDEZ","MARQUEZ","ESPINOZA","RAMIREZ","PACHECO","PALCIOS","DONARUMA",
  207.    "DIAZ","PERALTA","AGUILAR","ALLI","ARTETA","ALARCON","DRAXLER","CAZORLA","SANTOS","JIMENEZ",
  208. "VIDAL","CISNEROS","MARCHESIN","SAMUDIO","TREJO","RAMOS","MARQUISIO","BONAVENTURA","ASENSIO","MODRIC",
  209.    "ETO", "ASENSIO", "VELA", "LAINEZ", "SAMBUEZA", "VOLPI", "IBARRA", "LEON", "SUAREZ", "VIETO"};
  210. x=1+rand()%2;
  211. reg.sexo=x;
  212. if(x==1)
  213. {
  214. x=1+rand()%40;
  215. strcpy(reg.NombC.nombre,NombreH[x]);
  216. }
  217. else
  218. {
  219. x=1+rand()%40;
  220. strcpy(reg.NombC.nombre,NombreM[x]);
  221. }
  222. x=1+rand()%40;
  223. strcpy(reg.NombC.appat,Apellidos[x]);
  224. x=1+rand()%40;
  225. strcpy(reg.NombC.apmat,Apellidos[x]);
  226.  
  227. reg.matri=vect[pos];
  228.  
  229. return reg;
  230. }
  231.  
  232. void ImprimirVect(Talum vect[], int tam)
  233. {
  234. int i=0, j, tecla;
  235.  
  236. do{
  237. system("cls");
  238. printf("\t|----------------------------------------------------------------------------------------------|\n");
  239. printf("\t|   N  |   Apellido paterno   |   Apellido materno   | Nombre(s)    | Matricula | Sexo |");
  240. printf("\n\t|----------------------------------------------------------------------------------------------|");
  241. for(j=0; j<40 && i<tam; j++, i++)
  242. {
  243. printf("\n\t|%5d |   %-18s |   %-18s |   %-18s |  %-8ld |  %-3d |", i+1, vect[i].NombC.appat, vect[i].NombC.apmat, vect[i].NombC.nombre, vect[i].matri, vect[i].sexo);
  244. }
  245. printf("\n\t|----------------------------------------------------------------------------------------------|");
  246. printf("\n\t|        Presione ESC para salir cualquie otra tecla para ver los siguientes registros         |");
  247. printf("\n\t|----------------------------------------------------------------------------------------------|");
  248. tecla=getch();
  249. }while(i<tam && tecla!=ESC);
  250. }
  251.  
  252. void Imprimir(Talum reg[], int pos)
  253. {
  254. system("cls");
  255. printf("Apellido paterno: %s\n",reg[pos].NombC.appat);
  256. printf("Apellido materno: %s\n",reg[pos].NombC.apmat);
  257. printf("Nombre(s): %s\n",reg[pos].NombC.nombre);
  258. printf("Matricula: %ld\n",reg[pos].matri);
  259. printf("Sexo: %d\n",reg[pos].sexo);
  260. printf("\n");
  261. system("pause");
  262. }
  263.  
  264. int BusqSec(Talum vect[], int tam, long matri)
  265. {
  266. int i;
  267. for(i = 0; i < tam; i++)
  268. {
  269. if(vect[i].matri == matri)
  270. {
  271. return i;
  272. }
  273. }
  274. return -1;
  275. }
  276.  
  277. int BusquedaBin(Talum vect[], int tam, long matri)
  278. {
  279. int central, bajo, alto;
  280. int valorcentral;
  281. bajo=0;
  282. alto=tam-1;
  283. while(bajo <= alto)
  284. {
  285. central = (bajo+alto)/2;
  286. valorcentral = vect[central].matri;
  287. if(matri == valorcentral)
  288. {
  289. return central;
  290. }
  291. else
  292. {
  293. if(matri < valorcentral)
  294. {
  295. alto=central-1;
  296. }
  297. else
  298. {
  299. bajo=central+1;
  300. }
  301. }
  302. }
  303. return -1;
  304. }
  305.  
  306. void OrdenacionShell(Talum vect[], int tam)
  307. {
  308. int intervalo, i, j , k;
  309.  
  310. intervalo = tam/2;
  311.  
  312. while(intervalo > 0)
  313. {
  314. for(i = intervalo; i < tam; i++)
  315. {
  316. j=i-intervalo;
  317. while(j >= 0)
  318. {
  319. k=j+intervalo;
  320. if(vect[j].matri<=vect[k].matri)
  321. {
  322. j=-1;
  323. }
  324. else
  325. {
  326. Talum temp;
  327. temp=vect[j];
  328. vect[j]=vect[k];
  329. vect[k]=temp;
  330. j-=intervalo;
  331. }
  332. }
  333. }
  334. intervalo=intervalo/2;
  335. }
  336. }
  337.  
  338. void LlenarVect(long vect[], int tam, long ri, long rf)
  339. {
  340. int i=0,x;
  341. long num;
  342.  
  343. do{
  344. num=ri+(rand()%(rf-ri)+1);
  345. x=Buscar(vect, tam, num);
  346. if(x == -1)
  347. {
  348. vect[i]=num;
  349. i++;
  350. }
  351. }while(i<tam);
  352.  
  353. // return vect;
  354. }
  355.  
  356. int Buscar(long vect[], int tam, long matri)
  357. {
  358. int i;
  359. for(i = 0; i < tam; i++)
  360. {
  361. if(vect[i] == matri)
  362. {
  363. return i;
  364. }
  365. }
  366. return -1;
  367. }
  368.  











· Los códigos deben ir en etiquetas GeSHi
>aquí las reglas del foro (http://foro.elhacker.net/reglas.htm)
-Engel Lex


Título: Re: Estructuras Dinamicas
Publicado por: srWhiteSkull en 19 Octubre 2017, 16:37 pm
Entiendo que ya has alcanzado un buen nivel de C, como aprecio en el código que muestras, ahora ya entiendes y tienes un dominio básico del lenguaje, y el siguiente paso es el divertido mundo de los punteros  ::)

Consiste en reservar memoria mediante malloc y compañía (en C++ se usa el new y el delete). Lee :

http://sopa.dis.ulpgc.es/fso/cpp/intro_c/introc75.htm

Es necesario que entiendas bien los conceptos de la memoria en el programa, que es algo que al parecer no pareces conocer, estudia eso antes de meterte en los punteros, es un consejo. Lee :

http://www.lcc.uma.es/~galvez/ftp/tci/tictema8.pdf