Estructuras Dinamicas

Iniciado por luiszoket, 19 Octubre 2017, 05:23 AM

0 Miembros y 1 Visitante están viendo este tema.

luiszoket

¿Cómo puedo hacer, este código a Memoria Dinámica?

//********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

//********************************************************************//

//********************************************************************//
#define TRUE 1
#define FALSE 0
#define ENTER 13
#define ESC 27
#define ESPACIO 32
#define A 65
#define Z 90
#define N 5000
//********************************************************************//

//********************************************************************//
typedef struct nombre{
char nombre[30];
char appat[30];
char apmat[30];
}Tnombre;

typedef struct alumnos{
Tnombre NombC;
long matri;
int sexo;
}Talum;

//********************************************************************//
void menu(void);
Talum LlenarAut(Talum reg, long vect[], int pos);
void ImprimirVect(Talum vect[], int tam);
void Imprimir(Talum reg[], int pos);
int BusqSec(Talum vect[], int tam, long matri);
int BusquedaBin(Talum vect[], int tam, long matri);
void OrdenacionShell(Talum vect[], int tam);
void LlenarVect(long vect[], int tam, long ri, long rf);
int Buscar(long vect[], int tam, long matri);


int main(void){
menu();
srand(time(NULL));
return 0;
}

//********************************************************************//

void menu(void)
{
int op, i, j=0, x, pos=0, lleno=FALSE, orde=FALSE;
long mat[N], matri;
Talum reg, temp, vect[N];

LlenarVect(mat, N, 300000, 399999);

do{
system("cls");
printf("\n1.- Llenar Registros");
printf("\n2.- Buscar");
printf("\n3.- Ordenar");
printf("\n4.- Imprimir");
printf("\n5.- Salir");
printf("\nSelecciona una opcion: ");
scanf("%d",&op);
switch(op)
{
case 1:
system("cls");
if(j<N)
{
system("cls");
lleno=TRUE;
if(j<400)
{
for(i=0; i<4000; i++)
{
temp=LlenarAut(reg, mat, pos);
vect[j]=temp;
j++;
pos++;
}
printf("Los primeros %d registros han sido generados\n",i);
system("pause");
}
else
{
system("cls");
for(i=0; i<10; i++)
{
temp=LlenarAut(reg, mat, pos);
vect[j]=temp;
j++;
pos++;
}
printf("%d registros mas agregados\n",i);
orde=FALSE;
system("pause");
}
}
else
{
printf("Vector Lleno\n");
system("pause");
}
break;
case 2:
system("cls");
if(lleno==TRUE)
{
if(orde==TRUE)
{
system("cls");
printf("Metodo Binario\n");
printf("Digita la matricula: ");
scanf("%ld",&matri);
x=BusquedaBin(vect, j, matri);
if(x!=-1)
{
Imprimir(vect, x);
}
else
{
system("cls");
printf("Matricula no encontrada\n");
system("pause");
}
}
else
{
system("cls");
printf("Metodo Secuencial\n");
printf("Digita la matricula: ");
scanf("%ld",&matri);
x=BusqSec(vect, j, matri);
if(x!=-1)
{
Imprimir(vect, x);
}
else
{
system("cls");
printf("Matricula no encontrada\n");
system("pause");
}
}
}
else
{
system("cls");
printf("Vector Vacio\n");
system("pause");
}
// system("pause");
break;
case 3:
system("cls");
if(lleno==TRUE)
{
if(orde==FALSE)
{
OrdenacionShell(vect, j);
orde=TRUE;
printf("Datos Ordenados\n");
}
else
{
system("cls");
printf("El vector ya esta ordenado\n");
}
}
else
{
system("cls");
printf("Vector Vacio\n");
}

system("pause");
break;
case 4:
ImprimirVect(vect, j);
break;
}
}while(op!=5);
}

Talum LlenarAut(Talum reg, long vect[], int pos)
{
// Talum reg;
int x=0;

char NombreH[40][40]={"LUIS","ALBERTO","HUGO","PEDRO","JUAN","JOSE","GOKU","GOHAN","MIGUEL","ANGEL",
 "DIEGO","RAUL","ROBERTO","MARIO","RAFAEL","JAVIER","CARLOS","ARTURO","EMILIANO","ENRIQUE",
 "FRANCISCO","RONALDO","PAUL","OSVALDO","SANTIAGO","THIAGO","ESTEBAN","NOEL","LEONEL","MARTIN",
 "SAMUEL", "LUCAS", "MARCOS", "XAVI","ALONSO","RICARDO", "OMAR", "RENATO", "JONATHAN", "RUBEN"};
 
char NombreM[40][40]={"EMILY","JASMIN","LUCERO","NICOL","MARIA","CARMEN","ROCIO","CASANDRA","YESICA","VERONICA",
 "PAOLA","KAREN","ALEJANDRA","ALEXANDRA","ENID","EDITH","CARLA","KARLA","FABIOLA","YOHANA",
 "MONICA","JENIFER","LUCIA","KIMBERLY","AMANDA","PAULA","PERLA","MIA","STHEPHANIE","ALONDRA",
 "CLEOPATRA","ANA","CLARA","DIANA","FRIDA","HELENA","JENA","LUCIA","NADIA","SILBIA"};

char Apellidos[40][40]={"PEREZ","LOPEZ","SANCHEZ","FERNANDEZ","MARQUEZ","ESPINOZA","RAMIREZ","PACHECO","PALCIOS","DONARUMA",
   "DIAZ","PERALTA","AGUILAR","ALLI","ARTETA","ALARCON","DRAXLER","CAZORLA","SANTOS","JIMENEZ",
"VIDAL","CISNEROS","MARCHESIN","SAMUDIO","TREJO","RAMOS","MARQUISIO","BONAVENTURA","ASENSIO","MODRIC",
   "ETO", "ASENSIO", "VELA", "LAINEZ", "SAMBUEZA", "VOLPI", "IBARRA", "LEON", "SUAREZ", "VIETO"};
x=1+rand()%2;
reg.sexo=x;
if(x==1)
{
x=1+rand()%40;
strcpy(reg.NombC.nombre,NombreH[x]);
}
else
{
x=1+rand()%40;
strcpy(reg.NombC.nombre,NombreM[x]);
}
x=1+rand()%40;
strcpy(reg.NombC.appat,Apellidos[x]);
x=1+rand()%40;
strcpy(reg.NombC.apmat,Apellidos[x]);

reg.matri=vect[pos];

return reg;
}

void ImprimirVect(Talum vect[], int tam)
{
int i=0, j, tecla;

do{
system("cls");
printf("\t|----------------------------------------------------------------------------------------------|\n");
printf("\t|   N  |   Apellido paterno   |   Apellido materno   | Nombre(s)    | Matricula | Sexo |");
printf("\n\t|----------------------------------------------------------------------------------------------|");
for(j=0; j<40 && i<tam; j++, i++)
{
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);
}
printf("\n\t|----------------------------------------------------------------------------------------------|");
printf("\n\t|        Presione ESC para salir cualquie otra tecla para ver los siguientes registros         |");
printf("\n\t|----------------------------------------------------------------------------------------------|");
tecla=getch();
}while(i<tam && tecla!=ESC);
}

void Imprimir(Talum reg[], int pos)
{
system("cls");
printf("Apellido paterno: %s\n",reg[pos].NombC.appat);
printf("Apellido materno: %s\n",reg[pos].NombC.apmat);
printf("Nombre(s): %s\n",reg[pos].NombC.nombre);
printf("Matricula: %ld\n",reg[pos].matri);
printf("Sexo: %d\n",reg[pos].sexo);
printf("\n");
system("pause");
}

int BusqSec(Talum vect[], int tam, long matri)
{
int i;
for(i = 0; i < tam; i++)
{
if(vect[i].matri == matri)
{
return i;
}
}
return -1;
}

int BusquedaBin(Talum vect[], int tam, long matri)
{
int central, bajo, alto;
int valorcentral;
bajo=0;
alto=tam-1;
while(bajo <= alto)
{
central = (bajo+alto)/2;
valorcentral = vect[central].matri;
if(matri == valorcentral)
{
return central;
}
else
{
if(matri < valorcentral)
{
alto=central-1;
}
else
{
bajo=central+1;
}
}
}
return -1;
}

void OrdenacionShell(Talum vect[], int tam)
{
int intervalo, i, j , k;

intervalo = tam/2;

while(intervalo > 0)
{
for(i = intervalo; i < tam; i++)
{
j=i-intervalo;
while(j >= 0)
{
k=j+intervalo;
if(vect[j].matri<=vect[k].matri)
{
j=-1;
}
else
{
Talum temp;
temp=vect[j];
vect[j]=vect[k];
vect[k]=temp;
j-=intervalo;
}
}
}
intervalo=intervalo/2;
}
}

void LlenarVect(long vect[], int tam, long ri, long rf)
{
int i=0,x;
long num;

do{
num=ri+(rand()%(rf-ri)+1);
x=Buscar(vect, tam, num);
if(x == -1)
{
vect[i]=num;
i++;
}
}while(i<tam);

// return vect;
}

int Buscar(long vect[], int tam, long matri)
{
int i;
for(i = 0; i < tam; i++)
{
if(vect[i] == matri)
{
return i;
}
}
return -1;
}












· Los códigos deben ir en etiquetas GeSHi
>aquí las reglas del foro
-Engel Lex

srWhiteSkull

#1
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