Guest User

Untitled

a guest
Oct 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #pragma pack(1) //mudar a forma de leitura de bytes (mais eficiente)
  3.  
  4. typedef struct {
  5.   int id;
  6.   short age;
  7.   float height;
  8.   float weight;
  9.   char gender;
  10. } Record;
  11.  
  12. abrir_arquivo(FILE **file, Record *ptr)
  13. {
  14.     if( !(*file = fopen("records.dat","rb")) ) // rb = read binary (abrir arquivo de binário para ler)
  15.     {
  16.         printf("\n\t\t*** ERROR: FILE NOT FOUND ***\n\n");
  17.         exit (1);
  18.     }
  19. }
  20.  
  21. mostrar_tudo(FILE **file, Record *ptr)
  22. {
  23.     int count = 0, i = 0;
  24.     abrir_arquivo(file, ptr);
  25.     system("cls");
  26.     printf("\n\t\tEXIBICAO DE TODOS OS DADOS");
  27.  
  28.     fread(&count, sizeof(int), 1, *file);
  29.  
  30.     printf("\n\n\tAGE     WEIGHT     HEIGHT     GENDER\n");
  31.     do
  32.     {
  33.         fread(ptr, sizeof(Record), 1, *file);
  34.         printf("\n     %5d      %5.2f      %4.2f      %4c\n", ptr->age, ptr->height, ptr->weight, ptr->gender); //exibindo dados do arquivo
  35.         i++;
  36.     }while(i < count);
  37.  
  38.     fclose(*file); //fechando meu arquivo
  39.     printf("\n\nAperte Enter para voltar ao Menu...");
  40.     getche();
  41. }
  42.  
  43. soma_pesos(FILE **file, Record *ptr)
  44. {
  45.     int count = 0, i = 0;
  46.     abrir_arquivo(file, ptr);
  47.     system("cls");
  48.     float sum_weight;
  49.  
  50.     fread(&count, sizeof(int), 1, *file);
  51.  
  52.     do
  53.     {
  54.         fread(ptr, sizeof(Record), 1, *file);
  55.         sum_weight += ptr->height;
  56.         i++;
  57.     }while(i < count);
  58.  
  59.     printf("\n\n\tSOMA DE TODOS OS PESOS: %.2f", sum_weight);
  60.  
  61.     fclose(*file); //fechando meu arquivo
  62.     printf("\n\nAperte Enter para voltar ao Menu...");
  63.     getche();
  64. }
  65.  
  66. int maior_altura(FILE **file, Record *ptr)
  67. {
  68.     int count = 0, i = 0;
  69.     abrir_arquivo(file, ptr);
  70.     system("cls");
  71.     float height;
  72.  
  73.     fread(&count, sizeof(int), 1, *file);
  74.  
  75.     do
  76.     {
  77.         fread(ptr, sizeof(Record), 1, *file);
  78.  
  79.         if(ptr->weight > height)
  80.             height = ptr->weight; //note que está invertida a leitura da altura em relação ao peso.
  81.         i++;
  82.     }while(i < count);
  83.  
  84.     printf("\n\n\tMAIOR ALTURA: %.2f", height);
  85.  
  86.     fclose(*file); //fechando meu arquivo
  87.     printf("\n\nAperte Enter para voltar ao Menu...");
  88.     getche();
  89.     return 0;
  90. }
  91.  
  92. maior_idade(FILE **file, Record *ptr)
  93. {
  94.     abrir_arquivo(file, ptr);
  95.     system("cls");
  96.     short age;
  97.  
  98.     do
  99.     {
  100.         fread(ptr, sizeof(Record), 1, *file);
  101.  
  102.         if(ptr->age > age)
  103.             age = ptr->age;
  104.     }while(!feof(*file));
  105.  
  106.     printf("\n\n\tMAIOR IDADE: %d", age);
  107.  
  108.     fclose(*file); //fechando meu arquivo
  109.     printf("\n\nAperte Enter para voltar ao Menu...");
  110.     getche();
  111. }
  112.  
  113. pessoas(FILE **file, Record *ptr)
  114. {
  115.     abrir_arquivo(file, ptr);
  116.     system("cls");
  117.     int men, women;
  118.  
  119.     do
  120.     {
  121.         fread(ptr, sizeof(Record), 1, *file);
  122.  
  123.         if(ptr->gender == 'M' || ptr->gender == 'm')
  124.             men++;
  125.  
  126.         else
  127.             women++;
  128.     }while(!feof(*file));
  129.  
  130.     printf("\n\n\td) QUANTIDADE DE HOMENS: %d", men);
  131.     printf("\n\t   QUANTIDADE DE MULHERES: %d\n\n", women);
  132.  
  133.     fclose(*file); //fechando meu arquivo
  134.     printf("\n\nAperte Enter para voltar ao Menu...");
  135.     getche();
  136. }
  137.  
  138. main()
  139. {
  140.     int opcao;
  141.  
  142.     Record *ptr;
  143.     FILE *file;
  144.     abrir_arquivo(&ptr, &file);
  145.  
  146.     do
  147.     {
  148.         system("cls");
  149.         printf("\n\t\tMENU DE OPCOES");
  150.         printf("\n\n1-Exibir todos os dados");
  151.         printf("\n2-Exibir soma de todos pesos");
  152.         printf("\n3-Exibir maior altura");
  153.         printf("\n4-Exibir maior idade");
  154.         printf("\n5-Exibir quantidade de homens e mulheres");
  155.         printf("\n0-Sair");
  156.         printf("\n\nEntre com a opcao: ");
  157.         scanf("%d", &opcao);
  158.  
  159.         if(opcao != 0)
  160.         {
  161.             switch(opcao)
  162.             {
  163.                 case 1: mostrar_tudo(file, ptr); break;
  164.                 case 2: soma_pesos(file, ptr); break;
  165.                 case 3: maior_altura(file, ptr); break;
  166.                 case 4: maior_idade(file, ptr); break;
  167.                 case 5: pessoas(file, ptr); break;
  168.                 default: printf("\nOpcao invalida"); break;
  169.             }
  170.         }
  171.     }while(opcao != 0);
  172. }
Add Comment
Please, Sign In to add comment