Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #define PI 3.14159265358979323846
- #define MAX 30
- #define MAXTRACKPOINTS 10000
- //DEFINICION DE ESTRUCTURAS
- typedef struct{
- char nombre[MAX];
- char apellido1[MAX];
- char apellido2[MAX];
- int edad;
- int dorsal;
- char club[MAX];
- char sexo;
- char nom_categoria[MAX];
- int posicion_total;
- int posicion_categoria;
- int horas;
- int minutos;
- int segundos;
- }corredor;
- typedef struct{
- char nom_categoria[MAX];
- char sexo;
- int edad_min;
- int edad_max;
- float precio;
- }categoria;
- //FUNCIONES DEL PROGRAMA
- int cargar_categorias(categoria categorias[]);
- int cargar_corredores(corredor corredores[],categoria categorias[],int ncategorias);
- int menu();
- int buscar_corredor_por_dorsal(corredor corredores[],int ncorredores, int dorsal);
- int buscar_categoria(categoria categorias[],int ncategorias, char nombrecategoria[]);
- int cargar_tracks(int dorsal,float lat[],float lon[],int alt[]);
- float distancia(float lat1, float lon1, float lat2, float lon2);
- float velocidad_media(corredor corredores[],int ncorredores, int dorsal);
- void mostrar_abandonos(corredor corredores[],int ncorredores);
- //FUNCION MAIN
- int main (void){
- int i,ncategorias,ncorredores,eleccion,dorsal,ncorredor,ncategoria,ntracks,alt[30];
- corredor corredores[5000];
- categoria categorias[5000];
- char nombrecategoria[30],auxdorsal[5];
- float lat[30],lon[30];
- float lat1 = 41.3879169,lon1 = 2.1699187, lat2 = 40.4167413, lon2 = -3.7032498;
- float mostrar_distancia,velocidad;
- ncategorias=cargar_categorias(categorias);
- // printf("\nNumero cats %d",ncategorias);
- ncorredores=cargar_corredores(corredores,categorias,ncategorias);
- // printf("\nNumero corredores %d",ncorredores);
- //Prueba de la funcion: buscar_corredor_por_dorsal
- /* printf("\nIntroduce dorsal: ");
- scanf("%d",&dorsal);
- ncorredor=buscar_corredor_por_dorsal(corredores,ncorredores,dorsal);
- printf("\nCorredor nº: %d",ncorredor);*/
- //Prueba de la funcion: buscar_categoria
- /*printf("\nIntroduce categoria: ");
- fflush(stdin);
- gets(nombrecategoria);
- ncategoria=buscar_categoria(categorias,ncategorias,nombrecategoria);
- printf("\nCategoria nº: %d",ncategoria);*/
- ntracks=cargar_tracks(auxdorsal[5],lat,lon,alt);
- // printf("\n %d",ntracks);
- // printf("%f", distance(lat1, lon1, lat2, lon2));
- do{
- eleccion=menu();
- }while(eleccion>10 || eleccion<1);
- if(eleccion==1){
- //OPCION 1 DEL MENU
- printf("\nIntroduce el dorsal del corredor que deseas ver: ");
- scanf("%d",&dorsal);
- ncorredor=buscar_corredor_por_dorsal(corredores,ncorredores,dorsal);
- printf("\nDatos del corredor con dorsal: %d ",dorsal);
- printf("\nNombre: %s ",corredores[ncorredor].nombre);
- printf("\nApellidos: %s %s ",corredores[ncorredor].apellido1,corredores[ncorredor].apellido2);
- printf("\nEdad: %d ",corredores[ncorredor].edad);
- printf("\nClub: %s ",corredores[ncorredor].club);
- printf("\nSexo: %c ",corredores[ncorredor].sexo);
- printf("\nCategoria: %s",corredores[ncorredor].nom_categoria);
- }else if(eleccion==2){
- //OPCION 2 DEL MENU
- printf("\nIntroduce el dorsal del corredor que deseas borrar: ");
- scanf("%d",&dorsal);
- ncorredor=buscar_corredor_por_dorsal(corredores,ncorredores,dorsal);
- for(i=ncorredor;i<=ncorredores;i++){
- strcpy(corredores[i].nombre,corredores[i+1].nombre);
- strcpy(corredores[i].apellido1,corredores[i+1].apellido1);
- strcpy(corredores[i].apellido2,corredores[i+1].apellido2);
- corredores[i].edad=corredores[i+1].edad;
- corredores[i].dorsal=corredores[i+1].dorsal;
- strcpy(corredores[i].club,corredores[i+1].club);
- corredores[i].sexo=corredores[i+1].sexo;
- strcpy(corredores[i].nom_categoria,corredores[i+1].nom_categoria);
- corredores[i].horas=corredores[i+1].horas;
- corredores[i].minutos=corredores[i+1].minutos;
- corredores[i].segundos=corredores[i+1].segundos;
- }
- ncorredores--;
- //PRUEBAS
- //printf("\nNº corredores: %d ",ncorredores);
- //printf("\nCorredores: \n");
- //for(i=0;i<ncorredores;i++){
- // printf("%s %s %s %d %d %s %c %s %d %d %d\n",corredores[i].nombre,corredores[i].apellido1,corredores[i].apellido2,corredores[i].edad,corredores[i].dorsal,corredores[i].club,corredores[i].sexo,corredores[i].nom_categoria,/*corredores[i].posicion_total,corredores[i].posicion_categoria,*/corredores[i].horas,corredores[i].minutos,corredores[i].segundos);
- //}
- }
- else if(eleccion==3){
- //OPCION 3 DEL MENU
- printf ("Calcular velocidad media de un corredor, dado su dorsal: ");
- scanf ("%d", &dorsal);
- velocidad=velocidad_media(corredores,ncorredores,dorsal);
- printf("\n%.5f",velocidad);
- }
- else if(eleccion==7){
- mostrar_abandonos(corredores,ncorredores);
- }
- printf("\n\n");
- system("pause");
- }
- //DESRROLLO DE LAS FUNCIONES
- int cargar_categorias(categoria categorias[]){
- FILE * fichero_categorias;
- int i = 0;
- fichero_categorias = fopen("categorias.txt", "r");
- if (fichero_categorias == NULL){
- printf ( " Error en la apertura. Es posible que el fichero no exista \n ");
- }else{
- while (fscanf(fichero_categorias, "%s %c %d %d %f",categorias[i].nom_categoria, &categorias[i].sexo, &categorias[i].edad_min, &categorias[i].edad_max, &categorias[i].precio)==5)
- {
- // printf("Nombre %s sexo %c edad1 %d edad2 %d precio %.2f\n", categorias[i].nom_categoria,categorias[i].sexo,categorias[i].edad_min,categorias[i].edad_max,categorias[i].precio);
- i++;
- }
- }
- return(i);
- }
- int cargar_corredores(corredor corredores[],categoria categorias[],int ncategorias){
- FILE * fichero_corredores;
- int i = 0,j;
- fichero_corredores = fopen("corredores.txt", "r");
- if (fichero_corredores == NULL){
- printf ( " Error en la apertura. Es posible que el fichero no exista \n ");
- }else{
- while (fscanf(fichero_corredores, "%s %s %s %d %d %s %c %d %d %d",corredores[i].nombre,corredores[i].apellido1,corredores[i].apellido2,&corredores[i].edad,&corredores[i].dorsal,corredores[i].club,&corredores[i].sexo,/*corredores[i].nom_categoria,&corredores[i].posicion_total,&corredores[i].posicion_categoria,*/&corredores[i].horas,&corredores[i].minutos,&corredores[i].segundos)==10)
- {
- for(j=0;j<ncategorias;j++){
- if(corredores[i].sexo==categorias[j].sexo && corredores[i].edad>=categorias[j].edad_min && corredores[i].edad<=categorias[j].edad_max){
- strcpy(corredores[i].nom_categoria,categorias[j].nom_categoria);
- }
- }
- //printf("%s %s %s %d %d %s %c %s %d %d %d\n",corredores[i].nombre,corredores[i].apellido1,corredores[i].apellido2,corredores[i].edad,corredores[i].dorsal,corredores[i].club,corredores[i].sexo,corredores[i].nom_categoria,/*corredores[i].posicion_total,corredores[i].posicion_categoria,*/corredores[i].horas,corredores[i].minutos,corredores[i].segundos);
- i++;
- }
- }
- return(i);
- }
- int menu(){
- int eleccion;
- printf("\nMenu de la Bilbao Night Marathon\n");
- printf("\n1. Mostrar la informacion de un corredor, dado su dorsal. ");
- printf("\n2. Borrar un corredor, dado su dorsal.");
- printf("\n3. Calcular la distancia recorrida y la velocidad media de un corredor, dado su dorsal.");
- printf("\n4. Exportar el recorrido de un corredor a .kml, dado su dorsal.");
- printf("\n5. Gestionar el transporte de los corredores de un club.");
- printf("\n6. Calcular el coste de las inscripciones de los corredores de un club.");
- printf("\n7. Mostrar los corredores que han abandonado.");
- printf("\n8. Obtener la clasificación total.");
- printf("\n9. Obtener la clasificación de una categoría.");
- printf("\n10. Salir.\n");
- printf("\nElija una opcion: ");
- scanf("%d",&eleccion);
- return(eleccion);
- }
- int buscar_corredor_por_dorsal(corredor corredores[],int ncorredores, int dorsal){
- int i;
- for(i=0;i<=ncorredores;i++){
- if(corredores[i].dorsal==dorsal){
- return(i);
- }
- }
- return(-1);
- }
- int buscar_categoria(categoria categorias[],int ncategorias, char nombrecategoria[]){
- int i;
- for(i=0;i<=ncategorias;i++){
- if(strcmp(categorias[i].nom_categoria,nombrecategoria)==0){
- return(i);
- }
- }
- return(-1);
- }
- int cargar_tracks(int dorsal,float lat[],float lon[],int alt[])
- {
- int i=0;
- char auxdorsal[5];
- sprintf(auxdorsal,"%d",dorsal);
- FILE * fichero_tracks;
- fichero_tracks = fopen(auxdorsal, "r");
- if (fichero_tracks==NULL)
- {
- printf ( " Error en la apertura. Es posible que el fichero no exista \n ");
- }
- else
- {
- while(fscanf(fichero_tracks, "%f %f %d" ,&lat[i],&lon[i],&alt[i])==3)
- {
- //printf("\n %.5f %.5f %d",lat[i],lon[i],alt[i]);
- i++;
- }
- }
- printf("%d\n",i);
- fclose(fichero_tracks);
- return(i);
- }
- void mostrar_abandonos(corredor corredores[],int ncorredores){
- int i,cont=0;
- printf("\nHan abandonado los sigueintes corredores:\n");
- for(i=0;i<ncorredores;i++){
- if(corredores[i].horas==0 && corredores[i].minutos==0 && corredores[i].segundos==0){
- printf("%s %s %s <dorsal %d>\n",corredores[i].nombre,corredores[i].apellido1,corredores[i].apellido2,corredores[i].dorsal);
- cont++;
- }
- }
- printf("\nEn total ha habido %d abandonos.",cont);
- }
- float distancia (float lat1, float lon1, float lat2, float lon2) {
- float distance,radius,dlon,deg2radMultiplier;
- deg2radMultiplier = PI / 180;
- lat1 = lat1 * deg2radMultiplier;
- lon1 = lon1 * deg2radMultiplier;
- lat2 = lat2 * deg2radMultiplier;
- lon2 = lon2 * deg2radMultiplier;
- radius = 6378.137;
- dlon = lon2 - lon1;
- distance = acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlon)) * radius;
- //printf("%f",distance);
- return (distance);
- }
- float velocidad_media (corredor corredores[], int ncorredores, int dorsal)
- {
- int n,i,track,alt[MAXTRACKPOINTS];
- float segundos_horas,minutos_horas,horas_total,velocidad,dist_total=0,dist,lon1[MAXTRACKPOINTS],lat2[MAXTRACKPOINTS],lon2[MAXTRACKPOINTS],lat1[MAXTRACKPOINTS];
- float lon[MAXTRACKPOINTS],lat[MAXTRACKPOINTS];
- char auxdorsal;
- n=buscar_corredor_por_dorsal(corredores,ncorredores,dorsal);
- if(n==-1)
- {
- printf("dorsal no existe");
- }
- else
- {
- track=cargar_tracks(dorsal,lat,lon,alt);
- for(i=0;i<track-1;i++)
- {
- dist=distancia(lat[i],lon[i],lat[i+1],lon[i+1]);
- dist_total=dist_total+dist;
- }
- segundos_horas=corredores[n].segundos/3600.0;
- minutos_horas=corredores[n].minutos/60.0;
- horas_total=segundos_horas+minutos_horas+corredores[n].horas;
- velocidad=dist_total/horas_total;
- printf("%f km\n",dist_total);
- printf("%f horas",horas_total);
- }
- return(velocidad);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement