Advertisement
Perlamado

Untitled

Feb 24th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. // esame 170714
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <string.h>
  7. struct piano{
  8. int h;
  9. int nStanza;
  10. int altezzaStanza[10];
  11. int lunghezzaStanza[10];
  12. };
  13. struct edificio{
  14. struct piano v[10];
  15. int numPiani;
  16.  
  17. };
  18. struct piano *letturaFile(FILE *f,int *n,struct edificio *e);
  19. void stampaStruct(struct piano *p,int n);
  20. void funzioneRiempiStructEdificio(struct piano *p, int np,struct edificio *e);
  21. int funzioneAreaPiano(struct piano p );
  22. int funzioneVolume(struct piano p);
  23. int volumeEdificio(struct edificio e);
  24.  
  25.  
  26. int main (int argc,char *argv[]){
  27. int n,area,volume,volumeE;
  28. FILE *f;
  29. struct piano *piano;
  30. struct edificio edificio;
  31.  
  32. if(argc!=2){
  33. printf("paramentri errati\n");
  34. return 1;
  35. }
  36. f=fopen(argv[1],"r");
  37. if(f==NULL){
  38. printf("file non trovato\n");
  39. return 1;
  40. }
  41. // funzione lettura e stampa
  42. piano=letturaFile(f,&n,&edificio);
  43. fclose(f);
  44. stampaStruct(piano,n);
  45. funzioneRiempiStructEdificio(piano,n,&edificio);
  46. area=funzioneAreaPiano(piano[0]);
  47. printf("area:%d\n",area);
  48. volume=funzioneVolume(piano[0]);
  49. printf("volume:%d\n",volume);
  50. volumeE=volumeEdificio(edificio);
  51. printf("volume edificio:%d\n",volumeE);
  52. return 0;
  53. }
  54. struct piano *letturaFile(FILE *f,int *n, struct edificio *e){
  55.  
  56. int nConv;
  57. int size=10;
  58. int i1,i2;
  59. int i=0;// con i conto il n di stanze
  60. char buffer[100];
  61. int countRiga=0;
  62. struct piano *p1,*p2;
  63. *n=0;
  64.  
  65. p2=malloc(size*sizeof(struct piano));
  66. while(fgets(buffer,sizeof(buffer),f)){
  67. p1=p2 +*n;
  68.  
  69. if(countRiga==0){
  70.  
  71. sscanf(buffer,"%d",&e->numPiani);
  72.  
  73. }else {
  74.  
  75. nConv=sscanf(buffer,"%d %d",&i1,&i2);
  76.  
  77. if(i1>10){ // per far capire che non รจ la riga che riguarda la stanza.le stanze sono 10, quindi i>10 oro controllo le grndezze del piano
  78. p1->lunghezzaStanza[i]=i1;
  79. p1->altezzaStanza[i]=i2;
  80. i++;
  81. if(i==p1->nStanza){
  82. (*n)++;
  83. if(*n>=size){
  84. size=2*size;
  85. p2=realloc(p2,size*sizeof(struct piano));
  86. }
  87. }
  88.  
  89. }else{
  90. i=0;
  91. p1->nStanza=i1;
  92. p1->h=i2;
  93. }
  94. }
  95. countRiga++;
  96. }
  97. p2=realloc(p2,*n*sizeof(struct piano));
  98. return p2;
  99. }
  100. void stampaStruct(struct piano *p,int n){
  101. int i,j;
  102. for(i=0;i<n;i++){
  103. printf("PIANO %d ALTEZZA PIANO: %d NUMERO STANZE: %d\n", i+1, p[i].h, p[i].nStanza);
  104. for(j=0;j<p[i].nStanza;j++){
  105. printf("%d %d\n ",p[i].altezzaStanza[j],p[i].lunghezzaStanza[j]);
  106. }
  107. }
  108. }
  109. void funzioneRiempiStructEdificio(struct piano *p, int np,struct edificio *e){
  110. int i;
  111. for(i=0;i<np;i++){
  112. e->v[i]=p[i];
  113. }
  114. }
  115. int funzioneAreaPiano(struct piano p ){ // cosi gli passo solo i valori
  116. int i;
  117. int area=0;
  118. for(i=0;i<p.nStanza;i++){
  119. area+=p.altezzaStanza[i] *p.lunghezzaStanza[i];
  120. }
  121. return area;
  122. }
  123. int funzioneVolume(struct piano p){
  124. int area,volume;
  125.  
  126. area=funzioneAreaPiano(p);
  127. volume=area*p.h;
  128. return volume;
  129. }
  130. int volumeEdificio(struct edificio e){
  131. int i;
  132. int volume=0;
  133. for(i=0;i<e.numPiani;i++){
  134. volume+=funzioneVolume(e.v[i]);
  135. }
  136. return volume;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement