Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.94 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #define PI 3.14159265358979323846
  6. #define MAX 30
  7. #define MAXTRACKPOINTS 10000
  8.  
  9. //DEFINICION DE ESTRUCTURAS
  10.  
  11. typedef struct{
  12. char nombre[MAX];
  13. char apellido1[MAX];
  14. char apellido2[MAX];
  15. int edad;
  16. int dorsal;
  17. char club[MAX];
  18. char sexo;
  19. char nom_categoria[MAX];
  20. int posicion_total;
  21. int posicion_categoria;
  22. int horas;
  23. int minutos;
  24. int segundos;
  25. }corredor;
  26.  
  27. typedef struct{
  28.  
  29. char nom_categoria[MAX];
  30. char sexo;
  31. int edad_min;
  32. int edad_max;
  33. float precio;
  34. }categoria;
  35.  
  36.  
  37. //FUNCIONES DEL PROGRAMA
  38.  
  39. int cargar_categorias(categoria categorias[]);
  40. int cargar_corredores(corredor corredores[],categoria categorias[],int ncategorias);
  41. int menu();
  42. int buscar_corredor_por_dorsal(corredor corredores[],int ncorredores, int dorsal);
  43. int buscar_categoria(categoria categorias[],int ncategorias, char nombrecategoria[]);
  44. int cargar_tracks(int dorsal,float lat[],float lon[],int alt[]);
  45. float distancia(float lat1, float lon1, float lat2, float lon2);
  46. float velocidad_media(corredor corredores[],int ncorredores, int dorsal);
  47. void mostrar_abandonos(corredor corredores[],int ncorredores);
  48.  
  49. //FUNCION MAIN
  50.  
  51. int main (void){
  52.  
  53. int i,ncategorias,ncorredores,eleccion,dorsal,ncorredor,ncategoria,ntracks,alt[30];
  54. corredor corredores[5000];
  55. categoria categorias[5000];
  56. char nombrecategoria[30],auxdorsal[5];
  57. float lat[30],lon[30];
  58. float lat1 = 41.3879169,lon1 = 2.1699187, lat2 = 40.4167413, lon2 = -3.7032498;
  59. float mostrar_distancia,velocidad;
  60. ncategorias=cargar_categorias(categorias);
  61. // printf("\nNumero cats %d",ncategorias);
  62.  
  63. ncorredores=cargar_corredores(corredores,categorias,ncategorias);
  64. // printf("\nNumero corredores %d",ncorredores);
  65.  
  66.  
  67. //Prueba de la funcion: buscar_corredor_por_dorsal
  68. /* printf("\nIntroduce dorsal: ");
  69. scanf("%d",&dorsal);
  70. ncorredor=buscar_corredor_por_dorsal(corredores,ncorredores,dorsal);
  71. printf("\nCorredor nº: %d",ncorredor);*/
  72.  
  73.  
  74. //Prueba de la funcion: buscar_categoria
  75. /*printf("\nIntroduce categoria: ");
  76. fflush(stdin);
  77. gets(nombrecategoria);
  78. ncategoria=buscar_categoria(categorias,ncategorias,nombrecategoria);
  79. printf("\nCategoria nº: %d",ncategoria);*/
  80.  
  81.  
  82. ntracks=cargar_tracks(auxdorsal[5],lat,lon,alt);
  83. // printf("\n %d",ntracks);
  84.  
  85.  
  86. // printf("%f", distance(lat1, lon1, lat2, lon2));
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. do{
  97. eleccion=menu();
  98. }while(eleccion>10 || eleccion<1);
  99.  
  100.  
  101. if(eleccion==1){
  102.  
  103. //OPCION 1 DEL MENU
  104.  
  105. printf("\nIntroduce el dorsal del corredor que deseas ver: ");
  106. scanf("%d",&dorsal);
  107. ncorredor=buscar_corredor_por_dorsal(corredores,ncorredores,dorsal);
  108.  
  109. printf("\nDatos del corredor con dorsal: %d ",dorsal);
  110. printf("\nNombre: %s ",corredores[ncorredor].nombre);
  111. printf("\nApellidos: %s %s ",corredores[ncorredor].apellido1,corredores[ncorredor].apellido2);
  112. printf("\nEdad: %d ",corredores[ncorredor].edad);
  113. printf("\nClub: %s ",corredores[ncorredor].club);
  114. printf("\nSexo: %c ",corredores[ncorredor].sexo);
  115. printf("\nCategoria: %s",corredores[ncorredor].nom_categoria);
  116. }else if(eleccion==2){
  117.  
  118. //OPCION 2 DEL MENU
  119.  
  120. printf("\nIntroduce el dorsal del corredor que deseas borrar: ");
  121. scanf("%d",&dorsal);
  122. ncorredor=buscar_corredor_por_dorsal(corredores,ncorredores,dorsal);
  123.  
  124. for(i=ncorredor;i<=ncorredores;i++){
  125.  
  126. strcpy(corredores[i].nombre,corredores[i+1].nombre);
  127. strcpy(corredores[i].apellido1,corredores[i+1].apellido1);
  128. strcpy(corredores[i].apellido2,corredores[i+1].apellido2);
  129. corredores[i].edad=corredores[i+1].edad;
  130. corredores[i].dorsal=corredores[i+1].dorsal;
  131. strcpy(corredores[i].club,corredores[i+1].club);
  132. corredores[i].sexo=corredores[i+1].sexo;
  133. strcpy(corredores[i].nom_categoria,corredores[i+1].nom_categoria);
  134. corredores[i].horas=corredores[i+1].horas;
  135. corredores[i].minutos=corredores[i+1].minutos;
  136. corredores[i].segundos=corredores[i+1].segundos;
  137.  
  138. }
  139. ncorredores--;
  140.  
  141.  
  142. //PRUEBAS
  143.  
  144. //printf("\nNº corredores: %d ",ncorredores);
  145. //printf("\nCorredores: \n");
  146.  
  147.  
  148. //for(i=0;i<ncorredores;i++){
  149. // 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);
  150. //}
  151.  
  152.  
  153. }
  154. else if(eleccion==3){
  155.  
  156. //OPCION 3 DEL MENU
  157.  
  158. printf ("Calcular velocidad media de un corredor, dado su dorsal: ");
  159. scanf ("%d", &dorsal);
  160.  
  161. velocidad=velocidad_media(corredores,ncorredores,dorsal);
  162. printf("\n%.5f",velocidad);
  163.  
  164.  
  165.  
  166. }
  167.  
  168. else if(eleccion==7){
  169. mostrar_abandonos(corredores,ncorredores);
  170. }
  171.  
  172.  
  173.  
  174. printf("\n\n");
  175. system("pause");
  176. }
  177.  
  178.  
  179.  
  180. //DESRROLLO DE LAS FUNCIONES
  181.  
  182. int cargar_categorias(categoria categorias[]){
  183. FILE * fichero_categorias;
  184. int i = 0;
  185. fichero_categorias = fopen("categorias.txt", "r");
  186. if (fichero_categorias == NULL){
  187.  
  188. printf ( " Error en la apertura. Es posible que el fichero no exista \n ");
  189.  
  190. }else{
  191.  
  192. 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)
  193. {
  194. // 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);
  195. i++;
  196. }
  197.  
  198. }
  199. return(i);
  200. }
  201.  
  202.  
  203. int cargar_corredores(corredor corredores[],categoria categorias[],int ncategorias){
  204. FILE * fichero_corredores;
  205. int i = 0,j;
  206.  
  207. fichero_corredores = fopen("corredores.txt", "r");
  208. if (fichero_corredores == NULL){
  209.  
  210. printf ( " Error en la apertura. Es posible que el fichero no exista \n ");
  211.  
  212. }else{
  213.  
  214. 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)
  215. {
  216.  
  217.  
  218. for(j=0;j<ncategorias;j++){
  219.  
  220. if(corredores[i].sexo==categorias[j].sexo && corredores[i].edad>=categorias[j].edad_min && corredores[i].edad<=categorias[j].edad_max){
  221. strcpy(corredores[i].nom_categoria,categorias[j].nom_categoria);
  222. }
  223.  
  224. }
  225. //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);
  226. i++;
  227. }
  228. }
  229. return(i);
  230. }
  231.  
  232.  
  233.  
  234. int menu(){
  235. int eleccion;
  236.  
  237.  
  238. printf("\nMenu de la Bilbao Night Marathon\n");
  239. printf("\n1. Mostrar la informacion de un corredor, dado su dorsal. ");
  240. printf("\n2. Borrar un corredor, dado su dorsal.");
  241. printf("\n3. Calcular la distancia recorrida y la velocidad media de un corredor, dado su dorsal.");
  242. printf("\n4. Exportar el recorrido de un corredor a .kml, dado su dorsal.");
  243. printf("\n5. Gestionar el transporte de los corredores de un club.");
  244. printf("\n6. Calcular el coste de las inscripciones de los corredores de un club.");
  245. printf("\n7. Mostrar los corredores que han abandonado.");
  246. printf("\n8. Obtener la clasificación total.");
  247. printf("\n9. Obtener la clasificación de una categoría.");
  248. printf("\n10. Salir.\n");
  249.  
  250. printf("\nElija una opcion: ");
  251. scanf("%d",&eleccion);
  252.  
  253. return(eleccion);
  254.  
  255. }
  256.  
  257.  
  258. int buscar_corredor_por_dorsal(corredor corredores[],int ncorredores, int dorsal){
  259. int i;
  260.  
  261. for(i=0;i<=ncorredores;i++){
  262. if(corredores[i].dorsal==dorsal){
  263. return(i);
  264. }
  265. }
  266. return(-1);
  267. }
  268.  
  269.  
  270. int buscar_categoria(categoria categorias[],int ncategorias, char nombrecategoria[]){
  271. int i;
  272.  
  273. for(i=0;i<=ncategorias;i++){
  274. if(strcmp(categorias[i].nom_categoria,nombrecategoria)==0){
  275. return(i);
  276. }
  277. }
  278. return(-1);
  279.  
  280. }
  281.  
  282.  
  283. int cargar_tracks(int dorsal,float lat[],float lon[],int alt[])
  284. {
  285. int i=0;
  286. char auxdorsal[5];
  287. sprintf(auxdorsal,"%d",dorsal);
  288. FILE * fichero_tracks;
  289. fichero_tracks = fopen(auxdorsal, "r");
  290. if (fichero_tracks==NULL)
  291. {
  292. printf ( " Error en la apertura. Es posible que el fichero no exista \n ");
  293. }
  294. else
  295. {
  296. while(fscanf(fichero_tracks, "%f %f %d" ,&lat[i],&lon[i],&alt[i])==3)
  297. {
  298. //printf("\n %.5f %.5f %d",lat[i],lon[i],alt[i]);
  299. i++;
  300. }
  301.  
  302. }
  303. printf("%d\n",i);
  304. fclose(fichero_tracks);
  305. return(i);
  306. }
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315. void mostrar_abandonos(corredor corredores[],int ncorredores){
  316. int i,cont=0;
  317.  
  318. printf("\nHan abandonado los sigueintes corredores:\n");
  319.  
  320. for(i=0;i<ncorredores;i++){
  321.  
  322. if(corredores[i].horas==0 && corredores[i].minutos==0 && corredores[i].segundos==0){
  323. printf("%s %s %s <dorsal %d>\n",corredores[i].nombre,corredores[i].apellido1,corredores[i].apellido2,corredores[i].dorsal);
  324. cont++;
  325. }
  326.  
  327.  
  328. }
  329.  
  330. printf("\nEn total ha habido %d abandonos.",cont);
  331.  
  332. }
  333.  
  334. float distancia (float lat1, float lon1, float lat2, float lon2) {
  335.  
  336. float distance,radius,dlon,deg2radMultiplier;
  337. deg2radMultiplier = PI / 180;
  338.  
  339. lat1 = lat1 * deg2radMultiplier;
  340. lon1 = lon1 * deg2radMultiplier;
  341. lat2 = lat2 * deg2radMultiplier;
  342. lon2 = lon2 * deg2radMultiplier;
  343.  
  344. radius = 6378.137;
  345. dlon = lon2 - lon1;
  346. distance = acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlon)) * radius;
  347. //printf("%f",distance);
  348. return (distance);
  349. }
  350.  
  351. float velocidad_media (corredor corredores[], int ncorredores, int dorsal)
  352. {
  353. int n,i,track,alt[MAXTRACKPOINTS];
  354. float segundos_horas,minutos_horas,horas_total,velocidad,dist_total=0,dist,lon1[MAXTRACKPOINTS],lat2[MAXTRACKPOINTS],lon2[MAXTRACKPOINTS],lat1[MAXTRACKPOINTS];
  355. float lon[MAXTRACKPOINTS],lat[MAXTRACKPOINTS];
  356. char auxdorsal;
  357.  
  358. n=buscar_corredor_por_dorsal(corredores,ncorredores,dorsal);
  359. if(n==-1)
  360. {
  361. printf("dorsal no existe");
  362. }
  363. else
  364. {
  365.  
  366. track=cargar_tracks(dorsal,lat,lon,alt);
  367. for(i=0;i<track-1;i++)
  368. {
  369. dist=distancia(lat[i],lon[i],lat[i+1],lon[i+1]);
  370. dist_total=dist_total+dist;
  371.  
  372. }
  373. segundos_horas=corredores[n].segundos/3600.0;
  374. minutos_horas=corredores[n].minutos/60.0;
  375. horas_total=segundos_horas+minutos_horas+corredores[n].horas;
  376. velocidad=dist_total/horas_total;
  377. printf("%f km\n",dist_total);
  378. printf("%f horas",horas_total);
  379. }
  380.  
  381. return(velocidad);
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement