Advertisement
Jvsierra

Exercicio IMC ED fprintf fscanf 20/02/2019

Feb 20th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4.  
  5. struct TpPessoa
  6. {
  7.     int Matricula;
  8.     float Altura, Peso;
  9.     char Sexo; 
  10. };
  11.  
  12. void GeraRelat(char NomeArq[100])
  13. {
  14.     float IMC;
  15.     char Situacao[50];
  16.     TpPessoa P;
  17.     FILE *PtrTxt = fopen(NomeArq, "r");
  18.  
  19.     if(PtrTxt != NULL)
  20.     {
  21.         FILE *PtrAux = fopen("IMC.txt", "a");
  22.        
  23.         fscanf(PtrTxt, "%d %f %f %c", &P.Matricula, &P.Altura, &P.Peso, &P.Sexo);
  24.        
  25.         while(!feof(PtrTxt))
  26.         {
  27.             IMC = P.Peso / pow(P.Altura, 2);
  28.            
  29.             if(P.Sexo == 'F')
  30.             {
  31.                 if(IMC >= 24)
  32.                     strcpy(Situacao, "Acima");
  33.                 else if(IMC >= 19)
  34.                     strcpy(Situacao, "Ideal");
  35.                 else
  36.                     strcpy(Situacao, "Abaixo");
  37.             }
  38.             else
  39.             {
  40.                 if(IMC >= 24)
  41.                     strcpy(Situacao, "Acima");
  42.                 else if(IMC >= 19)
  43.                     strcpy(Situacao, "Ideal");
  44.                 else
  45.                     strcpy(Situacao, "Abaixo");
  46.             }
  47.            
  48.             fprintf(PtrAux, "%d %.2f %s\n", P.Matricula, IMC, Situacao);
  49.            
  50.             fscanf(PtrTxt, "%d %f %f %c", &P.Matricula, &P.Altura, &P.Peso, &P.Sexo);
  51.         }      
  52.        
  53.         fclose(PtrTxt);
  54.         fclose(PtrAux);
  55.     }
  56. }
  57.  
  58. int main(void)
  59. {
  60.     GeraRelat("d.txt");
  61.    
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement