Está en la página 1de 15

INDEXADO

/* Estructura del registro de datos*/


struct Reg_Dx
{
int NR;
char Nombre[15];
char Apellido[15];
int ARE;
} rd,ad;
int lrd=sizeof(struct Reg_Dx);
/* Encabezado del registro de datos*/
struct Encabezado_Dx
{
int NRS;
int BAND;
int URE;
} ed;
int led=sizeof(struct Encabezado_Dx);
/* Estructura del registro indice */
struct Reg_Ind
{
int NR;
char Clave_N[15];
int PTR_Nom;
char Clave_A[15];
int PTR_Ape;
} ri;
int lri=sizeof(struct Reg_Ind);
/* Encabezado del registro indice */
struct Encabezado_Ind
{
int NRS;
} ei;
int lei=sizeof(struct Encabezado_Ind);
int fdd; /*fichero de datos*/
int fdi; /*fichero de indices*/
void Reindexar()
{
if((fdd=open("Archivo.txt", O_RDWR))<0)
{
cout<<"\n\n\t\t\t\t\t\tNo existe ningun archivo "<<endl;
cout<<"\t\t\t\t\t ";

system("PAUSE");
return;
}
read(fdd,&ed,led);
if(ed.BAND==1)
{
lseek(fdd,0,0);
read(fdd,&ed,led);
ed.BAND=0;
lseek(fdd,0,0);
write(fdd,&ed,led);
Actualizar_Ind();
close(fdd);
close(fdi);
}
}
void Inicia_Seccion()
{
int band=1;
while(band==1)
{
system("color 0e");
system("cls");
char inic_ses[10];
char pass[10];
int ii=0;
cout<<"\n\n\n\n\t\t\t ORGANIZACION DE ARCHIVOS - METODO INDEXADO\n";
cout<<
"\t\t\t
==========================================\n\n\n\n";
cout<<"\n\n\t\t\t\t * Iniciar sesion : ";
gets(inic_ses);
cout<<"\n\n\t\t\t\t * Contrasenia : ";
while(ii!=6)
{
pass[ii]=getch();
cout<<"*";cout.flush();
ii++;
}
pass[ii]=0;
if(strcmp(pass,"wicked")==0)
{ delay(1); cargando(0); band=0;}
else
{ system("color 4C"); cout<<endl<<"\n\n\n\t\t\t
****"<<endl; band=1; delay(3);}
}
Reindexar();
void Datos(int cont)
{

**** Contrasea Incorrecta

fflush(stdin);
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<" * Nombre : ";
gets(rd.Nombre);
cout<<"\n\n\t\t\t\t\t";
cout<<" * Apellido : ";
gets(rd.Apellido);
cout<<"\n\n\t\t\t\t";
rd.ARE=0;
Alm_Dx_BF(rd.Nombre, rd.NR , rd.Apellido, cont);
}
struct Buffer
{
char Clave[15];
int PTR;
} BN[100], BA[100], AUX[100],aux2,s;

/* Se almacenan los datos ingresados en un vector de estructuras


el cual se usara como buffer */
void Alm_Dx_BF(char N[15], int nr , char A[15] , int i)
{
strcpy(BN[i].Clave,N);
BN[i].PTR=nr;
strcpy(BA[i].Clave,A);
BA[i].PTR=nr;
}
void reconstruirheap( struct Buffer, int n , int i)
{
int hijoizq;
int hijoder;
int j=0;
// char aux[100];
if(i<(n/2))
{
hijoizq = 2*i+1;
hijoder = 2*i+2;
j=hijoizq;
if((hijoder<n) && (strcmp(AUX[hijoder].Clave,AUX[j].Clave)>0) ) /*cambiar signo en
segundo parentesis ! para ke el se arregle descendentement =D! */
{
j=hijoder;
}
if((strcmp(AUX[j].Clave,AUX[i].Clave)>0)) /* cambiar signo! */
{
aux2=AUX[j];
AUX[j]=AUX[i];
AUX[i]=aux2;
reconstruirheap(*AUX,n,j);
}

}
}
void construirheap( struct Buffer , int n )
{
for( int i=(n/2)-1;i>=0;i--) {reconstruirheap(*AUX,n,i); }
}
void heapsort (struct Buffer, int n)
{
int m;
// char aux[100];
construirheap(*BN,n);
m=n;
while ( m > 1 )
{
aux2=AUX[0];
AUX[0]=AUX[m-1];
AUX[m-1]=aux2;
reconstruirheap(*AUX,m-1,0);
m=m-1;
}
}
void Copiar(struct Buffer, struct Buffer,int iden)
{
if(iden==1) { for(int i=1;i<=rd.NR;i++){ AUX[i]=BN[i]; } }
if(iden==2) { for(int i=1;i<=rd.NR;i++){ BN[i]=AUX[i]; } }
if(iden==3) { for(int i=1;i<=rd.NR;i++){ AUX[i]=BA[i]; } }
if(iden==4) { for(int i=1;i<=rd.NR;i++){ BA[i]=AUX[i]; } }
}
void ActualizarLista(int nr)
{
Copiar(*BN, *AUX,1);
heapsort(*AUX,nr);
Copiar(*AUX, *BN,2);
Copiar(*BA, *AUX,3);
heapsort(*AUX,nr);
Copiar(*AUX, *BA,4);
}
void Escribir_Ind(struct Buffer, struct Buffer, int n)
{
for(int i=1;i<n;i++)
{
fflush(stdin);
ri.NR=++ei.NRS;
strcpy(ri.Clave_N,BN[i].Clave);
ri.PTR_Nom=BN[i].PTR;
strcpy(ri.Clave_A,BA[i].Clave);
ri.PTR_Ape=BA[i].PTR;

write(fdi,&ri,lri);
}
lseek(fdi,0,0);
write(fdi,&ei,lei);
system("PAUSE");
}
void Escribir()
{
char rpta = 's';
if((fdd=creat("Archivo.txt",S_IWRITE | S_IREAD))<0) {return;}
if((fdi=creat("Archivo.ind",S_IWRITE | S_IREAD))<0) {return;}
lseek(fdd,led,0);
lseek(fdi,lei,0);
rd.NR=0;
ed.NRS=0;
ed.BAND=0;
/*si la bandera esta en cero significa archivo indice
actualizado*/
ed.URE=-1;
int cont=0;
while(rpta == 's')
{
system("cls");
rd.NR=++ed.NRS;
cont++;
Datos(cont);
write(fdd,&rd,lrd);
cout<<" * Seguir Registrando : ";
cin>>rpta;
}
ActualizarLista(rd.NR+1);
lseek(fdd, 0 , 0);
write(fdd, &ed, led);
Escribir_Ind(*BN, *BA, rd.NR+1);
close(fdd);
close(fdi);
}
int Buscar(char Busca[], int iden)
{
int inicio;
int final;
int medio;
int band;
int pos;
if((fdi=open("Archivo.ind",O_TEXT))<0)
{
cout<<"No se pudo abrir el fichero \n\n";
return -1;
}

read(fdi,&ei,lei);
inicio=1;
final=ei.NRS+1;
band=0;
while(final > inicio)
{
medio= int((inicio+final)/2);
pos=(medio-1)*lri+lei;
lseek(fdi,pos,0);
read(fdi,&ri,lri);
if(iden == 0) /* 0 busca nombres */
{
if((strcmp(Busca,ri.Clave_N))>0)
{
inicio=medio;
continue;
}
if((strcmp(Busca,ri.Clave_N))<0)
{
final=medio;
continue;
}
band=1;
return ri.PTR_Nom;
}
if(iden = 1 ) /* 1 busca apellido*/
{=
if((strcmp(Busca,ri.Clave_A))>0)
{
inicio=medio;
continue;
}
if((strcmp(Busca,ri.Clave_A))<0)
{
final=medio;
continue;
}
band=1;
return ri.PTR_Ape;
}
}
if(band==0)
{
return 0;
}
}
void Leer()
{
char Nombre[15];

char Apellido[15];
int nr;
int pos;
int op=0;
if((fdd=open("Archivo.txt", O_TEXT))<0)
{
cout<<"Archivo no se puede Abrir"<<endl;
system("PAUSE");
return;
}
read(fdd,&ed,led);
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"Busqueda por : \n\n\n";
cout<<"\t\t\t\t [1] Nombre \n\n\n";
cout<<"\t\t\t\t [2] Apellido \n\n\n";
cout<<"\t\t\t * Opcion : ";
cin>>op;
if(op==1)
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Nombre :" ;
gets(Nombre);
nr=Buscar(Nombre,0);
}
if(op==2)
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Apellido :" ;
gets(Apellido);
nr=Buscar(Apellido,1);
}
if( nr == -1) { cout<<"Archivo no se puede Abrir"<<endl; system("PAUSE");return;}
if( nr == 0 ) { cout<<"Registro no existe \n\n";system("PAUSE");}
else
{
pos=(nr-1)*lrd+led;
lseek(fdd,pos,0);
read(fdd,&rd,lrd);
cout<<"\n\n\n\n\n\n";
cout<<"\t\t\t\t\t\t * Nombre : "<<rd.Nombre<<endl;
cout<<"\t\t\t\t\t\t * Apellido : "<<rd.Apellido<<endl;
cout<<"\n\n\n\n\n\n";
cout<<"\t\t\t\t\t ";
system("PAUSE");

}
close(fdd);
close(fdi);
}
void Mostrar_Dx()
{
system("cls");
if((fdd=open("Archivo.txt", O_TEXT))<0){return ;}
if((fdi=open("Archivo.ind", O_TEXT))<0){return ;}
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
read(fdd, &ed, led);
cout<<"\n\t\t***********************Archivo.txt*********************** ";
cout<<"\n\n\t\tURE: "<<ed.URE<<"\tNRS: "<<ed.NRS<<"\tBAND:
"<<ed.BAND<<endl;
cout<<"\t\tNR\tNombre\tApellido\tAre\n";
cout<<"\t\t----------------------------------------\n";
while( (read(fdd, &rd, lrd))>0)
{
cout<<"\t\t"<<rd.NR<<"\t"<<rd.Nombre<<"\t"<<rd.Apellido<<"\t"<<rd.ARE<<endl;
}
cout<<"\n\t\t*********************************************************\n\n";
read(fdi, &ei, lei);
cout<<"\n\t\t***********************Archivo.ind*********************** ";
cout<<"\n\n\t\tNRS: "<<ei.NRS<<endl;
cout<<"\t\tNR\tClave_N\tPtr_N\tClave_A\tPtr_A\n";
cout<<"\t\t--------------------------------------\n";
while( (read(fdi, &ri, lri))>0)
{
cout<<"\t\t"<<ri.NR<<"\t"<<ri.Clave_N<<"\t"<<ri.PTR_Nom<<"\t"<<ri.Clave_A<<"\t"<<ri.
PTR_Ape<<endl;
}
cout<<"\n\t\t*********************************************************\n\n";
close(fdd);
close(fdi);
cout<<"\t\t\t\t\t ";
system("PAUSE>NULL");
}

void Actualizar_Ind() /* a copiar todos los que no estan eliminados al buffer y luego*/
{
/* crea el nuevo archivo indice :D */
int cont=0;
lseek(fdd,0,0);
lseek(fdd,led,0);
while(read(fdd,&rd,lrd)>0)
{
if(rd.ARE==0)
{

cont++;
Alm_Dx_BF(rd.Nombre,rd.NR,rd.Apellido,cont);
}
}
ActualizarLista(cont+1); /*ordenamos el buffer*/
system("del Archivo.ind");
if(( fdi = creat("Archivo.ind",S_IWRITE | S_IREAD))<0) {return;}
ei.NRS=0;
ri.NR=0;
lseek(fdi,lei,0);
Escribir_Ind(*BN, *BA, cont+1);
}
void Eliminar()
{
char Nombre[15];
char Apellido[15];
int nr;
int pos;
int op=0;
char rpta='s';
if((fdd=open("Archivo.txt", O_RDWR))<0)
{
cout<<"Archivo no se puede Abrir"<<endl;
system("PAUSE");
return;
}
read(fdd,&ed,led);
ed.BAND=1;
lseek(fdd,0,0);
write(fdd,&ed, led);
lseek(fdd,0,0);
read(fdd,&ed,led);
while(rpta == 's')
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"Eliminar por : \n\n\n";
cout<<"\t\t\t\t [1] Nombre \n\n\n";
cout<<"\t\t\t\t [2] Apellido \n\n\n";
cout<<"\t\t\t * Opcion : ";
cin>>op;
if(op==1)
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Nombre :" ;

gets(Nombre);
nr=Buscar(Nombre,0);
}
if(op==2)
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Apellido :" ;
gets(Apellido);
nr=Buscar(Apellido,1);
}
pos=(nr-1)*lrd+led;
lseek(fdd, pos, 0);
read(fdd, &rd, lrd);
rd.ARE=ed.URE;
ed.URE=rd.NR;
lseek(fdd,pos,0);
write(fdd,&rd,lrd);
cout<<"\n\n\t\t\t\t * Seguir Eliminando (s/n) : ";
cin>>rpta;
close(fdi);
}
Actualizar_Ind();
ed.BAND=0;
lseek(fdd,0,0);
write(fdd,&ed,led);
close(fdd);
close(fdi);
}
void Agregar()
{
system("cls");
int nr;
int pos;
int op=0;
char rpta='s';
if((fdd=open("Archivo.txt", O_RDWR))<0)
{
cout<<"Archivo no se puede Abrir"<<endl;
system("PAUSE");
return;
}
read(fdd,&ed,led);
ed.BAND=1;
lseek(fdd,0,0);
write(fdd,&ed, led);
lseek(fdd,0,0);
read(fdd,&ed,led);
while(rpta=='s')

{
if(ed.URE==-1)
{
rd.NR=++ed.NRS;
lseek(fdd,0,2);
}
else
{
pos=(ed.URE-1)*lrd+led;
lseek(fdd,pos,0);
read(fdd,&rd,lrd);
ed.URE=rd.ARE;
lseek(fdd,pos,0);
}
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<" * Nombre : ";
gets(rd.Nombre);
cout<<"\n\n\t\t\t\t\t";
cout<<" * Apellido : ";
gets(rd.Apellido);
cout<<"\n\n\t\t\t\t";
rd.ARE=0;
write(fdd,&rd,lrd);
cout<<"\n\n\t\t\t\t * Seguir Agregando (s/n) : ";
cin>>rpta;
}
Actualizar_Ind();
ed.BAND=0;
lseek(fdd,0,0);
write(fdd,&ed,led);
close(fdd);
close(fdi);
}
void Deseliminar()
{
system("cls");
int nr;
int pos;
int op=0;
char rpta='s';
char Nombre[15];
char Apellido[15];
int temp;
if((fdd=open("Archivo.txt", O_RDWR))<0)
{
cout<<"Archivo no se puede Abrir"<<endl;
system("PAUSE");
return;
}

read(fdd,&ed,led);
ed.BAND=1;
lseek(fdd,0,0);
write(fdd,&ed, led);
lseek(fdd,0,0);
read(fdd,&ed,led);
temp=ed.URE;
while(rpta == 's')
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"Recuperar Datos por : \n\n\n";
cout<<"\t\t\t\t [1] Nombre \n\n\n";
cout<<"\t\t\t\t [2] Apellido \n\n\n";
cout<<"\t\t\t * Opcion : ";
cin>>op;
if(op==1)
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Nombre :" ;
gets(Nombre);
while(temp!=-1)
{
pos=(temp-1)*lrd+led;
lseek(fdd,pos,0);
read(fdd, &rd, lrd);
if(strcmp(rd.Nombre,Nombre)==0)
{
if(rd.NR==ed.URE)
{
ed.URE=rd.ARE;
rd.ARE=0;
lseek(fdd,pos,0);
write(fdd,&rd,lrd);
break;
}
else
{
ad.ARE=rd.ARE;
rd.ARE=0;
lseek(fdd,pos,0);
write(fdd,&rd,lrd);
pos=(ad.NR-1)*lrd+led;
lseek(fdd,pos,0);
write(fdd,&ad,lrd);
break;

}
}
ad=rd;
temp=rd.ARE;
}
}
if(op==2)
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Apellido :" ;
gets(Apellido);
while(temp!=-1)
{
pos=(temp-1)*lrd+led;
lseek(fdd,pos,0);
read(fdd, &rd, lrd);
if(strcmp(rd.Apellido,Apellido)==0)
{
if(rd.NR==ed.URE)
{
ed.URE=rd.ARE;
rd.ARE=0;
lseek(fdd,pos,0);
write(fdd,&rd,lrd);
break;
}
else
{
ad.ARE=rd.ARE;
rd.ARE=0;
lseek(fdd,pos,0);
write(fdd,&rd,lrd);
pos=(ad.NR-1)*lrd+led;
lseek(fdd,pos,0);
write(fdd,&ad,lrd);
break;
}
}
ad=rd;
temp=rd.ARE;
}
}
cout<<"\n\n\t\t\t\t * Seguir Recuperando Datos (s/n) : ";
cin>>rpta;
}
Actualizar_Ind();
ed.BAND=0;
lseek(fdd,0,0);
write(fdd,&ed,led);

close(fdd);
close(fdi);
}
void Actualizar()
{
char Nombre[15];
char Apellido[15];
int nr;
int pos;
int op=0;
char rpta='s';
if((fdd=open("Archivo.txt", O_RDWR))<0)
{
cout<<"Archivo no se puede Abrir"<<endl;
system("PAUSE");
return;
}
read(fdd,&ed,led);
ed.BAND=1;
lseek(fdd,0,0);
write(fdd,&ed, led);
lseek(fdd,0,0);
read(fdd,&ed,led);
while(rpta == 's')
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"Actualizar: \n\n\n";
cout<<"\t\t\t\t [1] Nombre \n\n\n";
cout<<"\t\t\t\t [2] Apellido \n\n\n";
cout<<"\t\t\t * Opcion : ";
cin>>op;
if(op==1)
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Nombre :" ;
gets(Nombre);
nr=Buscar(Nombre,0);
pos=(nr-1)*lrd+led;
lseek(fdd, pos, 0);
read(fdd, &rd, lrd);
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Nuevo Nombre :" ;
gets(rd.Nombre);

lseek(fdd, pos, 0);


write(fdd, &rd, lrd);
}
if(op==2)
{
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Apellido :" ;
gets(Apellido);
nr=Buscar(Apellido,1);
pos=(nr-1)*lrd+led;
lseek(fdd, pos, 0);
read(fdd, &rd, lrd);
fflush(stdin);
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t";
cout<<"[*] Ingrese Nuevo Apellido :" ;
gets(rd.Apellido);
lseek(fdd, pos, 0);
write(fdd, &rd, lrd);
}
cout<<"\n\n\t\t * Seguir Actualizando (s/n) : ";
cin>>rpta;
close(fdi);
}
Actualizar_Ind();
ed.BAND=0;
lseek(fdd,0,0);
write(fdd,&ed,led);
close(fdd);
close(fdi);
}

También podría gustarte