Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define ASIENTOS 230
- #define LIBRE 0
- #define HOMBRE 1
- #define MUJER 2
- /* Escribe la función lee_reservas */
- int lee_reservas(int v[]){
- FILE *df;
- int a,t;
- df = fopen("reservas.txt","r");
- if (df == NULL)
- return -1;
- while(fscanf(df,"%d%d",&a,&t)!=EOF)
- v[a]=t;
- fclose(df);
- return 0;
- }
- /* Escribe la función reservar_plaza */
- int reservar_plaza(int v[]){
- int t, i;
- do{
- printf("¿El viajero es hombre=%d o mujer=%d?\n",HOMBRE,MUJER);
- scanf("%d",&t);
- }while (t != HOMBRE && t != MUJER);
- i=0;
- while(v[i] != LIBRE && i<ASIENTOS)
- i++;
- if (i < ASIENTOS){
- v[i] = t;
- printf("La plaza %d ha quedado reservada\n",i);
- return 1;
- }
- return 0;
- }
- /* Escribe la función calcula_porcentajes */
- void calcula_porcentajes(int v[]){
- int i, l=0, h=0, m=0;
- for(i=0;i<ASIENTOS;i++)
- if (v[i] == LIBRE)
- l++;
- else if (v[i] == HOMBRE)
- h++;
- else if (v[i] == MUJER)
- m++;
- printf("Libres= %.2f%%\n",l/(float)ASIENTOS*100);
- printf("Hombres= %.2f%%\n",h/(float)ASIENTOS*100);
- printf("Mujeres= %.2f%%\n",m/(float)ASIENTOS*100);
- }
- /* Función menu */
- int menu(){
- int opc;
- do{
- printf("1. Reservar plaza\n");
- printf("2. Calcular porcentajes\n");
- printf("3. Terminar\n");
- scanf("%d",&opc);
- }while(opc<1 || opc>3);
- return opc;
- }
- /* Función main */
- int main ( ){
- int asientos[ASIENTOS], err, opc;
- /* Leemos las reservas -> Invoca a la función lee_reservas */
- err=lee_reservas(asientos);
- if (err == -1){
- printf("Error leyendo fichero de reservas\n");
- return -1;
- }
- /* Si la función devuelve -1 -> Terminar el programa */
- do{
- opc = menu();
- switch(opc){
- case 1:
- /* Reservar plaza -> Invoca a la función reservar_plaza */
- err=reservar_plaza(asientos);
- if (err == 0)
- printf("El avión está lleno\n");
- break;
- /* Si está lleno indicadlo por pantalla */
- case 2:
- /* Calcular porcentajes -> Invoca la función calcula_porcentajes */
- calcula_porcentajes(asientos);
- break;
- }
- }while (opc != 3);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement