Advertisement
Kyrexar

avion_sol

Apr 18th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define ASIENTOS 230
  5.  
  6. #define LIBRE 0
  7. #define HOMBRE 1
  8. #define MUJER 2
  9.  
  10. /* Escribe la función lee_reservas */
  11.  
  12. int lee_reservas(int v[]){
  13.     FILE *df;
  14.     int a,t;
  15.  
  16.     df = fopen("reservas.txt","r");
  17.     if (df == NULL)
  18.     return -1;
  19.  
  20.     while(fscanf(df,"%d%d",&a,&t)!=EOF)
  21.     v[a]=t;
  22.  
  23.     fclose(df);
  24.  
  25.     return 0;
  26. }
  27.  
  28. /* Escribe la función reservar_plaza */
  29.  
  30. int reservar_plaza(int v[]){
  31.     int t, i;
  32.  
  33.   do{
  34.     printf("¿El viajero es hombre=%d o mujer=%d?\n",HOMBRE,MUJER);
  35.     scanf("%d",&t);
  36.   }while (t != HOMBRE && t != MUJER);
  37.  
  38.   i=0;
  39.   while(v[i] != LIBRE && i<ASIENTOS)
  40.       i++;
  41.  
  42.   if (i < ASIENTOS){
  43.       v[i] = t;
  44.       printf("La plaza %d ha quedado reservada\n",i);
  45.       return 1;
  46.   }
  47.  
  48.   return 0;
  49.  
  50. }
  51.  
  52. /* Escribe la función calcula_porcentajes */
  53.  
  54. void calcula_porcentajes(int v[]){
  55.     int i, l=0, h=0, m=0;
  56.  
  57.     for(i=0;i<ASIENTOS;i++)
  58.     if (v[i] == LIBRE)
  59.         l++;
  60.     else    if (v[i] == HOMBRE)
  61.         h++;
  62.     else if (v[i] == MUJER)
  63.         m++;
  64.  
  65.     printf("Libres= %.2f%%\n",l/(float)ASIENTOS*100);
  66.     printf("Hombres= %.2f%%\n",h/(float)ASIENTOS*100);
  67.     printf("Mujeres= %.2f%%\n",m/(float)ASIENTOS*100);
  68.  
  69. }
  70.  
  71. /* Función menu */
  72.  
  73. int menu(){
  74.     int opc;
  75.  
  76.     do{
  77.     printf("1. Reservar plaza\n");
  78.         printf("2. Calcular porcentajes\n");
  79.     printf("3. Terminar\n");
  80.     scanf("%d",&opc);
  81.     }while(opc<1 || opc>3);
  82.  
  83.     return opc;
  84. }
  85.  
  86. /* Función main */
  87.  
  88. int main ( ){
  89.     int asientos[ASIENTOS], err, opc;
  90.  
  91. /* Leemos las reservas -> Invoca a la función lee_reservas */
  92.  
  93.   err=lee_reservas(asientos);
  94.   if (err == -1){
  95.       printf("Error leyendo fichero de reservas\n");
  96.       return -1;
  97.   }
  98.  
  99. /* Si la función devuelve -1 -> Terminar el programa */
  100.  
  101.   do{
  102.       opc = menu();
  103.       switch(opc){
  104.       case 1:
  105. /* Reservar plaza -> Invoca a la función reservar_plaza */
  106.           err=reservar_plaza(asientos);
  107.           if (err == 0)
  108.           printf("El avión está lleno\n");
  109.           break;
  110. /* Si está lleno indicadlo por pantalla */
  111.       case 2:
  112. /* Calcular porcentajes -> Invoca la función calcula_porcentajes */
  113.           calcula_porcentajes(asientos);
  114.           break;
  115.       }
  116.   }while (opc != 3);
  117.  
  118.   system("PAUSE");
  119.   return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement