Files
Borland-C/CPP/ORDENA.CPP

130 lines
2.6 KiB
C++

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *maes, *mov, *movtmp;
struct nodo {
int n;
struct nodo *sig;
};
struct clientes{
char nombre[11], apellidos[21], direccion[21], telefono[11];
int codigo;
};
struct maestro {
int codigo;
char nombre[11], apellidos[21];
char direccion[21], telefono[11];
}regmaes;
struct movi {
char tipo;
int codigo;
char nombre[10], apellidos[20];
char direccion[20], telefono[10];
}regmov;
void INSERTAR (struct nodo **, int);
void ORDENAR (struct nodo **);
void LISTAR_INV (struct nodo *);
void LISTAR (struct nodo *);
int elementos=0;
struct nodo *p=NULL;
void main(void){
if ((mov=fopen("k:\\files\\clientes.mov","r+b"))==NULL)
{
printf("Error. No existe el fichero de movimientos");
exit (1);
}
else {
fread(&regmov,sizeof(regmov),1,mov);
while (!feof(mov))
{
INSERTAR(&p,regmov.codigo);
fread(&regmov,sizeof(regmov),1,mov);
}
fclose (mov);
ORDENAR(&p);
system ("rename k:\\files\\clientes.mov clientes.ord");
if ((movtmp=fopen("k:\\files\\clientes.ord","r+b"))==NULL)
{
printf("Error Muy Gordo. No existe el fichero temporal.");
exit (1);
}
mov=fopen("k:\\files\\clientes.mov","w+b");
struct nodo *q=p;
while (q) {
fread(&regmov,sizeof(regmov),1,movtmp);
if (regmov.codigo == q->n){
fwrite(&regmov,sizeof(regmov),1,mov);
fseek(movtmp,0,0);
q=q->sig;
}
}
//fclose(movtmp);
LISTAR(p);
getch();
free(p);
fcloseall();
unlink ("k:\\files\\clientes.ord");
}
}
//******************************************************************
void INSERTAR (struct nodo **p, int d){
struct nodo *aux;
aux = (struct nodo*) malloc (sizeof(struct nodo));
aux -> n = d;
aux -> sig = (*p);
(*p) = aux;
elementos+=1;
}
void ORDENAR (struct nodo **q){
int j=0,sw=0,n=0;
struct nodo *aux1=NULL, *aux2=NULL,aux;
int i=1;sw=1;
n=elementos-1;
while (i<=n && sw==1){
sw=0;aux2 = *q; aux1 = aux2->sig;
aux.sig=NULL;
aux.n=0;
for (j=1; j< elementos;j++) {
if ((aux2->n)>(aux1->n)){
aux.n=aux2->n;
aux2->n=aux1->n;
aux1->n=aux.n;
sw=1;
}
aux2 = aux1; aux1 = aux1 -> sig;
}
i++;
}
}
void LISTAR_INV (struct nodo *p){
if (p!=NULL) {
LISTAR_INV (p->sig);
printf(" %d ",p->n);
}
}
void LISTAR (struct nodo *p){
if (p!=NULL) {
printf(" %d ",p->n);
LISTAR (p->sig);
}
}