#include "funciones.h"
#include "struct.h"
#include <stdio.h>
#include <stdlib.h>

int contarNumReg(char* nombreFich){
    int nReg;
    FILE *f;
    if ((f=fopen(nombreFich, "rb"))==NULL){exit(-1);}

    if(fseek(f,0,SEEK_END)){exit(-1);}
    nReg=ftell(f);
    rewind(f);
    nReg=nReg/sizeof(struct alumno);

    fclose(f);
    printf("hola %d \n", nReg);
    return nReg;
}

void rellenarVectorBin(char* nombreFich, struct alumno **alumnos, int tam){
    FILE *f;
    if ((*alumnos=(struct alumno*)malloc(tam*sizeof(struct alumno)))==NULL){exit(-1);}
    if ((f=fopen(nombreFich, "rb"))==NULL){exit(-1);}
    fread(*alumnos,sizeof(struct alumno),tam,f);
    
}

int ordAsc(const struct alumno *a, const struct alumno *b) { //menos a mas, menores izq
    printf("hola as\n");
    if (a->dni > b->dni){
        return 1;
    }else if(a->dni < b->dni)
        return -1;
    else{
        return 0;
    }
}

int ordDes(const struct alumno *a, const struct alumno *b) { //mas a menores, mayores izq
    printf("hola des\n");
    if (a->dni > b->dni){
        return -1;
    }else if(a->dni < b->dni)
        return 1;
    else{
        return 0;
    }
}

void ordenacion(struct alumno *alumnos, int tam, int(*funcion)(const struct alumno *, const struct alumno*)){
    struct alumno aux;
    
    for (int i=0; i<tam; i++){
        for (int j=tam-1; j>0; j--){
            if((*funcion)(&alumnos[j-1],&alumnos[j])==1){
                aux=alumnos[j-1];
                alumnos[j-1]=alumnos[j];
                alumnos[j]=aux;
            }
        }
    }

    printf("{\n");
    for (int k=0; k<tam; k++){
        printf("%d. %d \n", k+1
        alumnos[k].dni);
    }
    printf("}\n");
}
