Advertisement
Perlamado

Untitled

Feb 4th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. //esame 19-09-2019
  2. //file190919.txt
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <math.h>
  8.  
  9. struct dati{
  10.     int h;
  11.     int battiti;
  12.     int fMin;
  13.     int fMax;
  14.     int fMean;
  15.     int besv;
  16.     int sonno;
  17.     int pasto;
  18.     int corsa;
  19.     int sintomo;
  20. };
  21.  
  22.  
  23. struct dati *letturaFile(FILE *f, int *n);
  24. void stampaStructDati(struct dati *dati,int n);
  25. int totBesvTest(struct dati *d,int n,int *oraTot);
  26.  
  27.  
  28. int main(int argc, char *argv[]){
  29.     FILE *f;
  30.     struct dati *dati;
  31.     //struct dati2 *d2;
  32.     int nDati;
  33.     int totBesv;
  34.     int oreTot;
  35.     //int nD2;
  36.  
  37.     if(argc!=2){
  38.         return 1;
  39.     }
  40.  
  41.     f=fopen(argv[1],"r");
  42.  
  43.     if(f==NULL){
  44.         printf("file non trovato\n");
  45.         return 1;
  46.     }
  47.  
  48.     // funzioni per la lettura dei file
  49.     dati=letturaFile(f,&nDati);
  50.     stampaStructDati(dati,nDati);
  51.     //richiesta 1
  52.    
  53.     totBesv=totBesvTest(dati,nDati,&oreTot);
  54.  
  55.     printf("%d\n%d\n",totBesv,oreTot);
  56.     return 0;
  57. }
  58.  
  59.  
  60. struct dati *letturaFile(FILE *f, int *n){
  61.  
  62.     int nConv;
  63.     int size=10;
  64.     char buffer[100];
  65.     char s[5];
  66.     struct dati *d1,*d2;
  67.     *n=0;
  68.     d2=malloc(size*sizeof(struct dati));
  69.     while(fgets(buffer,sizeof(buffer),f)){
  70.        
  71.         d1=d2+ *n;
  72.         nConv=sscanf(buffer,"%d\t%d\t%d\t%d\t%d\t%d\t%s",&d1->h,&d1->battiti,&d1->fMin,&d1->fMax,&d1->fMean,&d1->besv,s);
  73.         if(s[0]=='S'){
  74.             d1->sonno=1;
  75.         }else{
  76.             d1->sonno=0;
  77.         }
  78.         if(s[1]=='P'){
  79.             d1->pasto=1;   // tutto questo per riempire le ultime 4 variabili della strcut
  80.         }else{
  81.             d1->pasto=0;
  82.         }
  83.         if(s[2]=='C'){
  84.             d1->corsa=1;
  85.         }else{
  86.             d1->corsa=0;
  87.         }
  88.         if(s[3]=='D'){
  89.             d1->sintomo=1;
  90.         }else{
  91.             d1->sintomo=0;
  92.         }
  93.         (*n)++;
  94.         if(*n>size){
  95.         size=2*size;
  96.         d2=realloc(d2,size*sizeof(struct dati));
  97.        
  98.         }
  99.     }
  100.     d2=realloc(d2,(*n)*sizeof(struct dati));
  101.     return d2;
  102. }
  103.  
  104. void stampaStructDati(struct dati *dati,int n){
  105.  
  106.     int i;
  107.     for(i=0;i<n;i++){
  108.         //printf("%d\t%d\t%d\t%d\t%d\t%d\t%s\n",dati[i].h,dati[i].battiti,dati[i].fMin,dati[i].fMax,dati[i].fMean,dati[i].besv,dati[i].listaAttivita);
  109.         printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",dati[i].h,dati[i].battiti,dati[i].fMin,dati[i].fMax,dati[i].fMean,dati[i].besv,dati[i].sonno,dati[i].pasto,dati[i].corsa,dati[i].sintomo);
  110.     }
  111. }
  112. int totBesvTest(struct dati *d,int n,int *oreTot){
  113.  
  114.     int i;
  115.     int totBesv=0; // count da incrementare
  116.     for(i=0;i<n;i++){
  117.         totBesv+= d[i].besv;
  118.        
  119.     }
  120.     *oreTot=d[n-1].h-d[0].h+1;
  121.     return totBesv;
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement